您可以使用 CheckedTextView 来实现相同的功能:
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple">
</CheckedTextView>
添加列表.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
<Button android:id="@+id/click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click"/>
</LinearLayout>
这是活动GetCheckedTextPositionActivity。单击按钮后,您将检查列表项的位置:
public class GetCheckedTextPositionActivity extends Activity {
private ListView myList;
private EfficientAdapter myAdapter;
private ArrayList<String> data;
private LayoutInflater mInflater;
private Button click;
private ArrayList<Integer> positions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addlist);
positions = new ArrayList<Integer>();
data = new ArrayList<String>();
data.add("Bangalore");
data.add("Delhi");
data.add("Patna");
data.add("Mumbai");
click = (Button)findViewById(R.id.click);
mInflater = LayoutInflater.from(getApplicationContext());
myList = (ListView) findViewById(R.id.mainListView);
myAdapter = new EfficientAdapter(data, getApplicationContext());
myList.setAdapter(myAdapter);
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(positions.size() != 0){
for(Integer pos : positions){
Log.e("Positions checked :"," Positions :"+pos);
}
}
}
});
}
public class EfficientAdapter extends BaseAdapter {
private ArrayList<String> myData;
public EfficientAdapter(ArrayList<String> mData, Context ctx) {
// TODO Auto-generated constructor stub
this.myData = mData;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myData.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int currentPos = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.main, null);
}
((CheckedTextView) convertView).setText(myData.get(position));
((CheckedTextView) convertView).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((CheckedTextView) v).toggle();
if(((CheckedTextView) v).isChecked()){
positions.add(currentPos);
}
}
});
return convertView;
}
}
}
希望这可以帮助。