下面的代码为我工作
我的主要活动
public class MainActivity extends Activity {
private ListView llChb;
private String[] data = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z" };
private ArrayList<String> arrData=null;
private ArrayList<InfoRowdata> infodata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrData=new ArrayList<String>();
arrData.add("a");
arrData.add("b");
arrData.add("c");
arrData.add("d");
arrData.add("e");
arrData.add("f");
arrData.add("g");
arrData.add("h");
arrData.add("i");
arrData.add("j");
arrData.add("k");
arrData.add("l");
arrData.add("m");
arrData.add("n");
arrData.add("o");
arrData.add("p");
llChb = (ListView) findViewById(R.id.llChb);
infodata = new ArrayList<InfoRowdata>();
for (int i = 0; i < data.length; i++) {
infodata.add(new InfoRowdata(false, i));
// System.out.println(i);
//System.out.println("Data is == "+data[i]);
}
llChb.invalidate();
llChb.setAdapter(new MyAdapter());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
row = View.inflate(getApplicationContext(), R.layout.row, null);
TextView tvContent=(TextView) row.findViewById(R.id.tvContent);
//tvContent.setText(data[position]);
tvContent.setText(data[position]);
//System.out.println("The Text is here like.. == "+tvContent.getText().toString());
final CheckBox cb = (CheckBox) row
.findViewById(R.id.chbContent);
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (infodata.get(position).isclicked) {
infodata.get(position).isclicked = false;
} else {
infodata.get(position).isclicked = true;
}
for(int i=0;i<infodata.size();i++)
{
if (infodata.get(i).isclicked)
{
System.out.println("Selectes Are == "+ data[i]);
}
}
}
});
if (infodata.get(position).isclicked) {
cb.setChecked(true);
}
else {
cb.setChecked(false);
}
return row;
}
}
}
我的 InfoRowdata 模型类
public class InfoRowdata {
public boolean isclicked=false;
public int index;
/*public String fanId;
public String strAmount;*/
public InfoRowdata(boolean isclicked,int index/*,String fanId,String strAmount*/)
{
this.index=index;
this.isclicked=isclicked;
/*this.fanId=fanId;
this.strAmount=strAmount;*/
}
}
我的 MainXml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/llChb"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
这是 ListView 的自定义 RowXml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/rlInner"
android:layout_width="fill_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Medium Text"
android:textColor="#000000" />
<CheckBox
android:id="@+id/chbContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</RelativeLayout>
</RelativeLayout>