0

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

这是我的适配器类

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;
            }
        }

这是我的活动课

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>
4

1 回答 1

0

首先,如果你想改变数量值,你应该有一个代表数量的变量,你可以改变它。您在描述文本中包含了数量值,这是糟糕的设计。您应该做的是intRowItem类中有一个数量变量,如图所示。您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)
    }
});

*注意:*我已经编译并运行了这段代码。因此,如果您按照我提到的那样做所有事情,它就会起作用。

于 2013-10-29T06:54:14.800 回答