0

追加ボタンがクリックされたときに数量を追加し、ここでサブボタンがクリックされたときに数量を減算したい セッター/ゲッタークラス(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