Android CheckBox和ImageView、Textview在ListView中显示...

为什么80%的码农都做不了架构师?>>>   hot3.png

1.首先创建一个适配器类CheckBoxListViewAdapter,此类继承BaseAdapter类:

 

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class CheckBoxListViewAdapter extends BaseAdapter  {
      //Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。LayoutInflater的作用类似于
      //findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!
      //而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
        private LayoutInflater mInflater;    
	private List> mData;    
	public static Map isSelected;   //记录checkbox状态的
	String  []string ={"Android","IOS","Windows Phone","WebOs","Blackberry"};
	int  [] img ={R.drawable.android,R.drawable.ios,R.drawable.windowshone,R.drawable.webos,R.drawable.blackberry};
	public CheckBoxListViewAdapter(Context context) {    
		mInflater = LayoutInflater.from(context);    
		init();    
		}    

	
	
	//初始化    
	private void init() {    
	mData=new ArrayList>();    
	   
	for (int i = 0; i < 5; i++) {    
	Map map = new HashMap();    
	map.put("img", img[i]);    
	map.put("title", string[i]);    
	mData.add(map);    
	}    
	  
	//这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。    
	isSelected = new HashMap();    
	   
	for (int i = 0; i < mData.size(); i++) {    
	isSelected.put(i, false);    
	}    
}    
	   
	public int getCount() {    
	return mData.size();    
	}    
	   
	public Object getItem(int position) {    
	return   
	null;    
	}    
	   
	public long getItemId(int position) {    
	return   
	0;    
	}    
	   
	public View getView(int position, View convertView, ViewGroup parent) {    
	ViewHolder holder = null;    
	//convertView为null的时候初始化convertView。    
	if (convertView == null) {    
	holder = new ViewHolder();    
	convertView = mInflater.inflate(R.layout.my_data, null);    
	holder.img = (ImageView) convertView.findViewById(R.id.img);    
	holder.title = (TextView) convertView.findViewById(R.id.title);    
	holder.cBox = (CheckBox) convertView.findViewById(R.id.cb);    
	convertView.setTag(holder);    
	} else {    
	holder = (ViewHolder) convertView.getTag();    
	}    
	holder.img.setBackgroundResource((Integer) mData.get(position).get( "img"));    
	holder.title.setText(mData.get(position).get("title").toString());    
	holder.cBox.setChecked(isSelected.get(position));    
	   
	return convertView;    
}    
	   
	public final class ViewHolder {    
	public ImageView img;    
	public TextView  title;    
	public CheckBox  cBox;    
	}    

}

2.接下来定义布局文件my_data.xml,此布局文件中定义是在listView显示的组件信息



				    
				    
				    

3.接下来定义my_list.xml显示布局文件



	  

4.接下来就是TestQActivity

package com.test;

import com.test.CheckBoxListViewAdapter.ViewHolder;

import android.app.Activity;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TestQActivity extends Activity {
    /** Called when the activity is first created. */
	private  ListView  listview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_list);
        listview =(ListView)findViewById(R.id.lv);
        CheckBoxListViewAdapter  checkBoxListViewAdapter  =new CheckBoxListViewAdapter(this);
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listview.setItemsCanFocus(false);
        listview.setAdapter((ListAdapter) checkBoxListViewAdapter);
        listview.setOnItemClickListener(clickListener);

        listview.getCheckedItemPositions();
    }
    
    private OnItemClickListener  clickListener =new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView parent, View view, int position,
				long id) {
			ViewHolder vHollder = (ViewHolder) view.getTag(); 
			//在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。    
			vHollder.cBox.toggle();    
			CheckBoxListViewAdapter.isSelected.put(position, vHollder.cBox.isChecked());    

		}
	};

}

运行效果如下:

Android CheckBox和ImageView、Textview在ListView中显示..._第1张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/amigos/blog/59174

你可能感兴趣的:(Android CheckBox和ImageView、Textview在ListView中显示...)