我有一个适配器来填充我的列表视图,(书籍列表)。我的列表视图包含一个复选框。一旦按下按钮,我希望在另一个列表视图(另一个活动)中显示所有选中的项目,我该怎么做?我正在尝试使用下面的代码。我怎样才能做到这一点?到现在为止,我到了祝酒列表视图中的项目数量,当我检查或取消选中一个项目以显示她的位置时,我祝酒。我想知道选中项目的所有位置是新项目其他listview。这里是适配器的代码:
public class MyAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
public ImageLoader imageLoader;
private ArrayList<Boolean> status_mag = new ArrayList<Boolean>();
public MyAdapter(Activity a,ArrayList<HashMap<String, String>> d) {
// TODO Auto-generated constructor stub
activity = a;
data=d;
imageLoader=new ImageLoader(activity.getApplicationContext());
for(int i=0;i<data.size();i++){status_mag.add(false);}
}
public ArrayList<Boolean> getStauts_mag(){
return status_mag;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(vi == null)
{
LayoutInflater inflater=getLayoutInflater();
vi=inflater.inflate(R.layout.list_row_megashore_mag, parent, false);
}
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView price = (TextView)vi.findViewById(R.id.prix); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
TextView catg = (TextView)vi.findViewById(R.id.catg_mag);
CheckBox acheter_mag = (CheckBox)vi.findViewById(R.id.checkBoxAchete_mag);
HashMap<String, String> mag = new HashMap<String, String>();
mag = data.get(position);
title.setText(mag.get(MainMagazines.KEY_TITLE));
price.setText(mag.get(MainMagazines.KEY_PRICE));
catg.setText(mag.get(MainMagazines.KEY_CATG));
imageLoader.DisplayImage(mag.get(MainMagazines.KEY_THUMB_URL), thumb_image);
acheter_mag.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
status_mag.set(position, true);
Toast.makeText(getApplicationContext(), "cheked" + position,
Toast.LENGTH_SHORT).show();
} else {
status_mag.set(position, false);
Toast.makeText(getApplicationContext(), "uncheked" + position,
Toast.LENGTH_SHORT).show();
}
}
});
acheter_mag.setChecked(status_mag.get(position));
return vi;
}
}
这里是主类中的代码,按钮尝试显示检查项目的总和。
final MyAdapter adapter_mag=new MyAdapter(this, booksList);
getListView().setAdapter(adapter_mag);
taille_mag=adapter_mag.getCount();
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
checkitems=(Button)findViewById(R.id.to_favoris);
checkitems.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i=1;i<=taille_mag;i++){if (adapter_mag.status_mag.get(i)==true){toast_somme+=1;}}
Toast.makeText(getApplicationContext(), "la somme de checked items" + toast_somme,
Toast.LENGTH_SHORT).show();
}
});
请帮助我,谢谢