0
這是一個使用多選適配器的ListView。如何在添加和排序後保持listview項目的複選框選擇?
public class ExampleActivity extends ListActivity {
private ListView list;
private List<String> items;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_example);
this.list = this.getListView();
this.items = new ArrayList<String>();
this.items.add("A");
this.items.add("C");
this.items.add("D");
this.items.add("D");
this.adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items);
this.list.setAdapter(adapter);
this.user(); // simulating user selecting C
this.button(); // simulating user press some button who adds items to list
}
private void user() {
this.list.setItemChecked(1, true);
}
private void button() {
this.items.add("B");
Collections.sort(this.items);
this.adapter.notifyDataSetChanged();
}
}
如果用戶選擇C(索引1),並且如果用戶然後按下按鈕來添加項到列表和排序(如B),B(新索引1)將要被選擇的,而不是C. 的這個問題怎麼解決?
感謝後你,雖然我認爲我的問題不明確,現在編輯。 –
確定問題在哪裏? –