provare a utilizzare CheckedTextView per questo problema
principale.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" android:background="#ffffff">
<ListView
android:id="@+id/listView"
android:scrollbars="vertical" android:divider="#C0C0C0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:cacheColorHint="#00000000" android:dividerHeight="1dip">
</ListView>
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="All"></Button>
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="None"></Button>
<Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Done"></Button>
</LinearLayout>
</LinearLayout>
listrow.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#000000" android:padding="15dip"/>
CheckedTextVwActivity.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
public class CheckedTextVwActivity extends Activity {
/** Called when the activity is first created. */
ListView listView;
ListAdapter adapter;
ArrayList<String> strings = new ArrayList<String>();
Button button1,button2,button3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
TopicSelectionListAdapter topicSelectionListAdapter = new TopicSelectionListAdapter(
CheckedTextVwActivity.this, R.layout.listrow, strings);
listView.setAdapter(topicSelectionListAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
for(int i=0;i<strings.size();i++){
listView.setItemChecked(i, true);
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
CheckedTextView selectedItem = (CheckedTextView) view;
boolean isChecked = selectedItem.isChecked();
Log.e("TAG","item clicked position = " + position + " isChecked = " + isChecked);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<strings.size();i++){
listView.setItemChecked(i, true);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<strings.size();i++){
listView.setItemChecked(i, false);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = listView.getAdapter().getCount();
String savedItems = null;
for (int i = 0; i < count; i++) {
if (listView.isItemChecked(i)) {
savedItems = savedItems + listView.getItemAtPosition(i).toString() + ",";
}
}
Toast.makeText(CheckedTextVwActivity.this, ""+savedItems, Toast.LENGTH_LONG).show();
}
});
}
public class TopicSelectionListAdapter extends ArrayAdapter {
@SuppressWarnings("unchecked")
public TopicSelectionListAdapter(Context context,
int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
}
@Override
public long getItemId(int currentPosition) {
return super.getItemId(currentPosition);
}
@Override
public View getView(int currentPosition, View convertView,
ViewGroup parent) {
View v = convertView;
final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listrow, null);
final CheckedTextView textView = (CheckedTextView) v
.findViewById(R.id.text);
textView.setText(strings.get(currentPosition));
return v;
}
}
}
fonte
2011-12-07 07:21:15
Si dovrebbe controllare sul link qui sotto per risolvere ur problema .... Ecco il [link] (http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically -Generata-casella-through-list/7739006 # 7739006). – himanshu