【问题标题】:populate an adapter with the checked items of a listview使用列表视图的选中项填充适配器
【发布时间】:2012-12-19 03:57:05
【问题描述】:

我有一个适配器来填充我的列表视图(书籍列表)。 我的列表视图包含一个复选框。一旦按下按钮,我希望在另一个列表视图(另一个活动)中显示所有选中的项目,我该怎么做?我正在尝试使用下面的代码。我怎样才能做到这一点? 到现在为止,我到了祝酒列表视图中的项目数量,当我检查或取消选中一个项目以显示她的位置时,我祝酒。我想知道选中项目的所有位置是新项目其他列表视图。这里是适配器的代码:

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();

        }
    });

请帮帮我,谢谢

【问题讨论】:

    标签: android listview checkbox adapter


    【解决方案1】:

    使用您当前的设置,只需循环通过status_mag 来检查是否检查了此索引。如果是这样,请将其保存在新列表中并将其绑定到您的新 ListView。

    或者,如果 list_row_megashore_mag.xml 中的根布局实现 Checkable,您可以调用 getCheckedItemPositions() 自动获取已检查行的 SparseBooleanArray。

    【讨论】:

    • 另请注意,如果您需要获取 ID 而不是位置,那么您可能必须在此处使用此代码:stackoverflow.com/a/6654119/832776
    • id 对于 CursorAdapters 或其他非位置派生索引非常有用,但这里 id 最多是位置的别名。
    • 哦,是的 - 我没有注意到 ArrayList - 实际上在这种情况下,ID 是无关紧要的。
    • 感谢大家。我到达了一点,以达到我的目标。我在吐司中得到了这个,检查了所有项目,但它显示了所有对象:{cat_liv=technologie,id_liv=12-23,prix_liv=23,pic_liv=192.168.1.100/prg_java111.png,titre_liv=java pour tout le monde} {cat_liv=technologie,id_liv=34-13,prix_liv=100,pic_liv=192.168.1.100/image27.png,titre_liv=Python} 那么我怎样才能只取 prix_liv 的值??
    • 我很高兴终于成功了,这是一个简单的解决方案,但没有及时看到它。所以我在适配器中添加了一个方法:** public String get_prix(int position){ return data.get(position).get(KEY_PRICE); }** 它运行 100% :)) 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2015-03-26
    • 2015-04-08
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多