Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用listview的选中项填充适配器_Android_Listview_Checkbox_Adapter - Fatal编程技术网

Android 使用listview的选中项填充适配器

Android 使用listview的选中项填充适配器,android,listview,checkbox,adapter,Android,Listview,Checkbox,Adapter,我有一个适配器,可以打开我的listview(一个书籍列表)。 我的listview包含一个复选框。我希望在另一个listview(另一个活动)中显示所有选中的项目。一旦按下一个按钮,我如何才能做到这一点?我正在尝试使用下面的代码。我怎样才能做到这一点? 到目前为止,我来这里是为了在toast中显示listview中的项目数,当我选中或取消选中某个项目以显示其位置时,我会干杯。我想知道选中项目的所有位置,以便在其他listview中成为的新项目。下面是适配器的代码: public class

我有一个适配器,可以打开我的listview(一个书籍列表)。 我的listview包含一个复选框。我希望在另一个listview(另一个活动)中显示所有选中的项目。一旦按下一个按钮,我如何才能做到这一点?我正在尝试使用下面的代码。我怎样才能做到这一点? 到目前为止,我来这里是为了在toast中显示listview中的项目数,当我选中或取消选中某个项目以显示其位置时,我会干杯。我想知道选中项目的所有位置,以便在其他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;

    }


}
公共类MyAdapter扩展了BaseAdapter
{
私人活动;
私有数组列表数据;
公共图像加载器;
private ArrayList status_mag=new ArrayList();
公共MyAdapter(活动a,ArrayList d){
//TODO自动生成的构造函数存根
活动=a;
数据=d;
imageLoader=新的imageLoader(activity.getApplicationContext());

对于(int i=0;i使用当前设置,只需在
status\u mag
中循环检查此索引是否已选中。如果已选中,请将其保存在新列表中,并将其绑定到新的ListView


或者,如果
list\u row\u megashore\u mag.xml中的根布局实现了Checkable,则可以调用以自动获取已检查行的SparseBoolean数组。

还要注意,如果需要获取ID而不是位置,则可能必须在此处使用此代码:ID对于游标适配器或其他非位置数据非常有用已验证的索引,但这里的id充其量只是该位置的别名。哦,是的-我没有注意到
ArrayList
-实际上在这种情况下id是不相关的。谢谢大家。我来了一点,以实现我的目标。我在toast中得到了这一点,检查了所有项目,但它显示了所有对象:{cat_-liv=technologie,id_-liv=12-23,prix_-liv=23,pic_-liv=pour tout le monde}{cat_-liv=technologie,id_-liv=34-13,prix_-liv=100,pic_-liv=}那么我怎么能接受prix_-liv的值呢?我很高兴终于成功了,这是一个简单的解决方案,但我没有及时看到它。所以我在适配器中添加了一个方法:*公共字符串get_-prix(int位置){返回data.get(position.get(KEY_PRICE)}**并且它运行100%:))非常感谢
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();

        }
    });