一,Layout
1,main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
2,item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试">
</Button>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
二,Activity
1,MainActivity
package com.excetop.listradio;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private ListView listView;
private Map checkMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.list);
checkMap = new HashMap<String, Object>();
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
private String[] s = new String[]{"a","b","c","d","e","a","b","c","d","e","a","b","c","d","e","a","b","c","d","e"};
@Override
public int getCount() {
// TODO Auto-generated method stub
return s.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(int position, View convertView, ViewGroup parent) {
convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null); //解决办法: 每次都重新获取View
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(s[position]);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
checkBox.setId(position); //把position设为radioButton的id
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkMap.put(String.valueOf(checkBox.getId()), checkBox.getId());
// Toast.makeText(MainActivity.this, String.valueOf( checkBox.getId()), 0).show();
}else{
checkMap.remove(String.valueOf(checkBox.getId()));
// Toast.makeText(MainActivity.this, String.valueOf( checkBox.getId()), 0).show();
}
}
});
if(checkMap.get(String.valueOf(position)) != null){
checkBox.setChecked(true);
// Toast.makeText(MainActivity.this, String.valueOf(String.valueOf(position)), 0).show();
}
//这里实现单选框选的回显,解决了单选框移出屏幕范围未选中状态
return convertView;
}
}
}