要在ListView里实现多选,前面说过ListView加入CheckBox 自己实现多选MULTIPLE CHOICE,如果你的需求比较简单,用自带的simple_list_item_multiple_choice就可以了. 布局:
<?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"
>
<ListView android:id="@+id/list"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@null"
/>
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取选中ID"
/>
</LinearLayout>
一个ListView,一个Button Java代码:
package com.pocketdigi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Test2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv=(ListView)findViewById(R.id.list);
String[] Names=new String[]{"a","b","c"};
ArrayAdapter Adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice, Names);
//使用系统内置的layout
lv.setAdapter(Adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//设置多选模式
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
long[] ids=lv.getCheckItemIds();
//得到选中的itemId
String str="";
for(int i=0;i