如何在列表视图中添加/删除数量

时间:2013-10-29 05:44:40

标签: android listview android-listview

我想在单击添加按钮时添加数量,在这里单击子按钮时减去数量我没有显示我的setter / getter类(RowItem)和我的xml文件 (main.xml,item_details_view)

这是我的Adapter类

public class ItemListBaseAdapter extends ArrayAdapter<RowItem> {

            Context context;

            public ItemListBaseAdapter(Context context, int resourceId,
                    List<RowItem> items) {
                super(context, resourceId, items);
                this.context = context;
            }

            /*private view holder class*/
            private class ViewHolder {
                ImageView imageView;
                TextView txtTitle;
                TextView txtDesc;
                Button add,sub;

            }

            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                RowItem rowItem = getItem(position);

                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_details_view, null);
                    holder = new ViewHolder();
                    holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
                    holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
                    holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
                    holder.add = (Button) convertView
                            .findViewById(R.id.button1);
                    holder.sub = (Button) convertView
                            .findViewById(R.id.button2);
                    convertView.setTag(holder);
                } else
                    holder = (ViewHolder) convertView.getTag();

                holder.txtDesc.setText(rowItem.getDesc());
                holder.txtTitle.setText(rowItem.getTitle());
                holder.imageView.setImageResource(rowItem.getImageId());
                holder.add.setTag(position);
                holder.sub.setTag(position);



            holder.add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Adds 1 to the counter
                        int counter=0;
                  counter = counter + 1;


                    }
                });

             holder.sub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Subtract 1 from counter
                        int counter=0;
                     counter = counter - 1;

                    }
                });

                return convertView;
            }
        }

Thisi是我的Activity类

public class ListViewImagesActivity extends Activity implements
    OnItemClickListener {


public static final String[] titles = new String[] { "EggBurger",
        "cheesBurger", "KingBurger", "Mixed" };

public static final String[] descriptions = new String[] {"Select 0 Item"
        };

public static final Integer[] images = { R.drawable.burger1,
        R.drawable.burger2, R.drawable.burger3, R.drawable.burger4 };

ListView listView;
List<RowItem> rowItems;
Button add,sub;
int counter=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem(images[i], titles[i], descriptions[0]);
        rowItems.add(item);
    }


    listView = (ListView) findViewById(R.id.list);
    ItemListBaseAdapter adapter = new ItemListBaseAdapter(this,
            R.layout.item_details_view, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Toast toast = Toast.makeText(getApplicationContext(),
        "Item " + (position + 1) + ": " + rowItems.get(position),
        Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();
}
}

这是我的RowItem类

public class RowItem {
     private int imageId;
        private String title;
        private String desc;

        public RowItem(int imageId, String title, String desc) {
            this.imageId = imageId;
            this.title = title;
            this.desc = desc;
        }
        public int getImageId() {
            return imageId;
        }
        public void setImageId(int imageId) {
            this.imageId = imageId;
        }
        public String getDesc() {
            return desc;
        }
        public void setDesc(String desc) {
            this.desc = desc;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        @Override
        public String toString() {
            return title + "\n" + desc;
        }

}

这是我的main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     />

</LinearLayout>

这是我的item_details_view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="80dp"

        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:paddingBottom="10dp"
        android:textColor="#CC0033"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/icon"
        android:paddingLeft="10dp"
        android:textColor="#3399FF"
        android:textSize="14dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/button2"
        android:text="Add" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/desc"
        android:layout_alignParentRight="true"
        android:text="Sub" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

首先,如果要更改数量值,则应该有一个表示可以更改的数量的变量。您已在说明文字中包含数量值,这是糟糕的设计。您应该做的是在int类中有一个数量变量RowItem,如图所示。您desc应该只从数量值返回一个字符串,如下所示:

public class RowItem {
    private int imageId;
    private String title;
    private int quantity = 0;

    public RowItem(int imageId, String title, int quantity) {
        this.imageId = imageId;
        this.title = title;
        this.quantity = quantity;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getDesc() {
        return "Select " + quantity + " Item";
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return title + "\n" + quantity;
    }
}

在您的活动中,不是存储说明,而是存储数量:

public static final int[] quantities = new int[]{ 0 };

onCreate初始化rowItems如下

rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
    RowItem item = new RowItem(images[i], titles[i], quantities[0]);
    rowItems.add(item);
}

最后,在OnClickListener中,获取数量值,更新数量并设置新描述。

final ViewHolder viewHolderFinal = holder;
final RowItem finalRowItem = rowItem;
holder.add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int quantity = finalRowItem.getQuantity(); // get the quantity for this row item
        finalRowItem.setQuantity(quantity + 1); // update it by adding 1
        viewHolderFinal.txtDesc.setText(finalRowItem.getDesc()); // set the new description (that uses the updated qunatity)
    }
});

holder.sub.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int quantity = finalRowItem.getQuantity(); // get the quantity for this row item
        finalRowItem.setQuantity(quantity - 1);  // update it by subtracting 1
        viewHolderFinal.txtDesc.setText(finalRowItem.getDesc()); // set the new description (that uses the updated qunatity)
    }
});

* 注意:* 我编译并运行此代码。所以,如果你像我提到的那样做了一切,那就可以了。