在写这篇文章之前,xxx已经写过了几篇关于改androidclass主题的文章,想要了解的朋友可以去翻一下之前的文章
main.xml如下:
listviewitem.xml如下:
ListViewAdapter如下:
import java.util.List;import java.util.Map;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.CheckBox;import android.widget.SimpleAdapter;import android.widget.TextView;public class ListViewAdapter extends SimpleAdapter { private List > mArrayList; private int resource; private LayoutInflater mLayoutInflater; public ListViewAdapter(Context context,List > data, int resource, String[] from,int[] to) { super(context, data, resource, from, to); this.mArrayList=data; this.resource=resource; mLayoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { if (mArrayList==null) { return 0; } else { return mArrayList.size(); } } @Override public Object getItem(int position) { if (mArrayList==null) { return null; } else { return mArrayList.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder=null; if(convertView==null){ viewHolder=new ViewHolder(); convertView=mLayoutInflater.inflate(resource, null); viewHolder.textView=(TextView) convertView.findViewById(R.id.textView); viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);; convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder) convertView.getTag(); } if (mArrayList!=null) { if (viewHolder.textView!=null) { viewHolder.textView.setText((String)(mArrayList.get(position).get("content"))); } if (viewHolder.checkBox!=null) { boolean isChecked=MainActivity.mCheckBoxStatusHashMap.get(position); viewHolder.checkBox.setChecked(isChecked); } } return convertView; } private class ViewHolder{ TextView textView; CheckBox checkBox; }}
每日一道理 时间好比一条小溪,它能招引我们奔向生活的海洋;时间如同一叶扁舟,它将帮助我们驶向理想的彼岸;时间犹如一支画笔,它会指点我们描绘人生的画卷。
MainActivity如下:
import java.util.ArrayList;import java.util.HashMap;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.CheckBox;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.app.Activity;/** * Demo描述: * ListView的Item中含有CheckBox的处置方法 * */public class MainActivity extends Activity { private ListView mListView; private HashMapmHashMap; private ArrayList > mArrayList; public static HashMap mCheckBoxStatusHashMap=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mCheckBoxStatusHashMap=new HashMap (); mArrayList=new ArrayList >(); mListView=(ListView) findViewById(R.id.listview); addDataForListView(); mListView.setAdapter (new ListViewAdapter(MainActivity.this, mArrayList, R.layout.listviewitem, new String []{"content"}, new int []{R.id.textView})); mListView.setOnItemClickListener(new ItemClickListenerImpl()); } private void addDataForListView(){ for (int i = 0; i < 20; i++) { mHashMap=new HashMap (); mHashMap.put("content", ""+i); mArrayList.add(mHashMap); mCheckBoxStatusHashMap.put(i, false); } } private class ItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position,long arg) { ListView lv = (ListView) parent; CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox); checkBox.toggle(); boolean itemIsCheck = checkBox.isChecked(); HashMap itemHashMap = (HashMap ) lv.getItemAtPosition(position); mCheckBoxStatusHashMap.put(position, itemIsCheck); } }}
文章结束给大家分享下程序员的一些笑话语录: Borland说我很有前途,Sun笑了;Sun说我很有钱,IBM笑了;IBM说我很专业,Sybase笑了;Sybase说我数据库很牛,Oracle笑了;Oracle说我是开放的,Linux笑了;Linux说我要打败Unix,微软笑了;微软说我的系统很稳定,我们都笑了。
--------------------------------- 原创文章 By
android和class---------------------------------