我正在尝试从主列表中获取所选项目,并将其更新到其他子列表,但是当我点击主列表项目时,列表的所有项目将变为不可见,除了我选择的项目,并且只有该元素是更新到子列表,可能是什么问题?...下面的代码片段显示我在做什么....
ListView list,selectedList;
LazyAdapter adapter, adapter1;
String mStrings[]={"item1","item2","item3","item4","item5","item6","item7"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(listener);
list.setOnItemClickListener(new OnItemClickListener(){
ArrayList<String> years = new ArrayList<String>();
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SparseBooleanArray checked = list.getCheckedItemPositions();
if(checked.get(arg2))
{
years.add(mStrings[arg2]);
}
else
{
years.remove(mStrings[arg2]);
}
if(years.size()!=0)
{
String selectedItems[]=new String[years.size()];
for(int i=0;i<years.size();i++)
selectedItems[i]=years.get(i);
selectedList=(ListView)findViewById(R.id.selectedList);
adapter1=new LazyAdapter(MainActivity.this, selectedItems);
selectedList.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
}
}});
}
答案 0 :(得分:0)
最好使用相同的适配器实例并更改其内容,然后通知数据更改(就像您一样)。
回收适配器而不是创建新适配器。