3 回答
TA贡献1860条经验 获得超8个赞
在ListView上设置选择模式
//if using ListActivity or ListFragment
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//or
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//myListView is reference to your ListView
TA贡献1784条经验 获得超9个赞
我使用数组适配器为具有复选框的列表创建了一个示例示例。
在Android中使用ArrayAdatpter使用CheckBox实现ListView
您也可以找到示例代码。
这将被输出
这是我的代码片段
我有一个具有列表视图的活动
public class MainActivity extends Activity {
private ListView mainListView = null;
private Planet[] planets = null;
private ArrayAdapter<Planet> listAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainListView = (ListView) findViewById(R.id.mainListView);
// When item is tapped, toggle checked properties of CheckBox and
// Planet.
mainListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
Planet planet = listAdapter.getItem(position);
planet.toggleChecked();
PlanetViewHolder viewHolder = (PlanetViewHolder) item
.getTag();
viewHolder.getCheckBox().setChecked(planet.isChecked());
}
});
// Create and populate planets.
planets = (Planet[]) getLastNonConfigurationInstance();
if (planets == null) {
planets = new Planet[] { new Planet("Mercury"),
new Planet("Venus"), new Planet("Earth"),
new Planet("Mars"), new Planet("Jupiter"),
new Planet("Saturn"), new Planet("Uranus"),
new Planet("Neptune"), new Planet("Ceres"),
new Planet("Pluto"), new Planet("Haumea"),
new Planet("Makemake"), new Planet("Eris") };
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll(Arrays.asList(planets));
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter(listAdapter);
}
public Object onRetainNonConfigurationInstance() {
return planets;
}
}
这是我正在处理复选框内容的“自定义阵列适配器”
public class PlanetArrayAdapter extends ArrayAdapter<Planet> {
private LayoutInflater inflater;
public PlanetArrayAdapter(Context context, List<Planet> planetList) {
super(context, R.layout.simplerow, R.id.rowTextView, planetList);
//Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Planet planet = (Planet) this.getItem(position);
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
textView = (TextView) convertView.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
convertView.setTag(new PlanetViewHolder(textView, checkBox));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Planet planet = (Planet) cb.getTag();
planet.setChecked(cb.isChecked());
}
});
}
// Re-use existing row view
else {
PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
checkBox.setTag(planet);
// Display planet data
checkBox.setChecked(planet.isChecked());
textView.setText(planet.getName());
return convertView;
}
}
请!让我知道您是否有任何麻烦。
添加回答
举报