【问题标题】:Get and use checked items from my ListView从我的 ListView 中获取和使用选中的项目
【发布时间】:2011-12-20 08:43:51
【问题描述】:

我正在开发一个 ListView。我想获取选定的元素并将它们储存起来,以便以后使用。

这可能是基本的,但作为一个初学者,我真的不知道如何处理它..

这是我的 List 的样子,以及与之相关的 Java 代码:

http://i.stack.imgur.com/MgKq9.jpg

public class PizzaActivity extends DashboardActivity {
    /** Called when the activity is first created. */
    // HashMap arraList taking objects
    private ArrayList <HashMap<String, Object>> myIng;
    private static final String INGREDIENT = "ingnom";
    private static final String PRIX = "ingprix";
    private static final String IMAGE = "iconfromraw";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_f2);
        ListView listView = (ListView)findViewById(R.id.list);

        myIng = new ArrayList<HashMap<String,Object>>();
        HashMap<String, Object> hm;

        // Créée une nouvelle HashMap et définis ses élements
        hm = new HashMap<String, Object>();
        hm.put(IMAGE, R.drawable.mozza);
        hm.put(INGREDIENT, "Mozzarella");
        hm.put(PRIX, "Prix : 0.60€");
        // Ajoute la HashMap créée à la liste
        myIng.add(hm);

        hm = new HashMap<String, Object>();
        hm.put(IMAGE, R.drawable.emmental);
        hm.put(INGREDIENT, "Emmental");
        hm.put(PRIX, "Prix : 0.50€");

        myIng.add(hm);

        hm = new HashMap<String, Object>();
        hm.put(INGREDIENT, "Chèvre");
        hm.put(PRIX, "Prix : 0.75€");
        hm.put(IMAGE, R.drawable.chevre);

        myIng.add(hm);

        hm = new HashMap<String, Object>();
        hm.put(INGREDIENT, "Roquefort");
        hm.put(PRIX, "Prix : 0.75€");
        hm.put(IMAGE, R.drawable.roquefort);

        myIng.add(hm);

       // Définis un SimpleAdapter et lie les valeurs aux lignes de la vue view R.layout.listview
       SimpleAdapter adapter = new SimpleAdapter(this, myIng, R.layout.listview,
                new String[]{IMAGE,INGREDIENT,PRIX}, new int[]{R.id.img,R.id.text1, R.id.text2});

        listView.setAdapter(adapter);

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    } 

}

任何建议都会很酷:)

谢谢!

【问题讨论】:

    标签: java android listview checkbox


    【解决方案1】:

    检查此example。你应该可以轻松做到。

    检查这个布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="horizontal">
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
    
            <ImageView android:id="@+id/friendsImage"
                android:layout_width="50dp" android:layout_height="50dp"
                android:layout_margin="10dp" android:src="@drawable/network" android:layout_alignParentLeft="true"/>
    
            <TextView android:id="@+id/textTitle" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:gravity="center_vertical"
                android:textColor="@color/white" android:text="Friend # 1"
                android:textSize="30px" android:textStyle="bold" android:layout_toRightOf="@id/friendsImage"
                android:layout_marginTop="20dp" />
            <CheckBox android:id="@+id/check" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginLeft="4px"
                android:layout_marginRight="10px" android:layout_alignParentRight="true"></CheckBox>
        </RelativeLayout>
    </LinearLayout>
    

    你的适配器的getView应该是这样的

    public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.friends_list_item, null);
    
                holder = new ViewHolder();
    
                holder.textTitle = (TextView) convertView
                        .findViewById(R.id.textTitle);
                holder.messageListTitle = (ImageView) convertView
                        .findViewById(R.id.friendsImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
                holder.checkbox
                        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            public void onCheckedChanged(CompoundButton buttonView,
                                    boolean isChecked) {
                                Log.d(TAG, "Oncheck clicked");
                                Friend element = (Friend) holder.checkbox
                                        .getTag();
                                element.isSelected = buttonView.isChecked();
                            }
                        });
                holder.checkbox.setTag(mData.get(position));
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();
                ((ViewHolder) convertView.getTag()).checkbox.setTag(mData.get(position));
    
            try {
                Friend curObj = mData.get(position);
                holder.textTitle.setText(curObj.fname);
                holder.checkbox.setChecked(mData.get(position).isSelected);
                //Add image
                convertView.setTag(R.id.tagFriendList, curObj);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return convertView;
        }
    

    【讨论】:

    • 谢谢,我查过了。由于我列表中的每个元素还包含一张图片和一个价格,我应该将它添加到“模型”类中吗?还是别的地方?
    • 为您编辑了答案以帮助您了解详细信息。
    • 再次感谢,我开始了解它是如何工作的。我不明白一件事,“getView”示例中的“mData”到底代表什么?
    • 另外,接近尾声,“R.id.tagFriendList”是什么类型的元素?
    • mData 是我用来设置适配器的arrayList。检查此链接以了解 setTag 和 getTag。 androidguys.com/2009/11/09/return-of-the-fancy-listviews
    猜你喜欢
    • 2012-08-09
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多