Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add/remove quantity in a list view
    primarykey
    data
    text
    <p>I want to add quantity when the add button is clicked and subtract quantity when sub button is clicked here i am not showing my setter/getter class(RowItem) and my xml files (main.xml,item_details_view) </p> <p>This is my Adapter class</p> <pre><code>public class ItemListBaseAdapter extends ArrayAdapter&lt;RowItem&gt; { Context context; public ItemListBaseAdapter(Context context, int resourceId, List&lt;RowItem&gt; 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; } } </code></pre> <p>Thisi is my Activity class</p> <pre><code>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&lt;RowItem&gt; 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&lt;RowItem&gt;(); for (int i = 0; i &lt; 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&lt;?&gt; 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(); } } </code></pre> <p>this is my RowItem class</p> <pre><code>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; } } </code></pre> <p>this is my main.xml </p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>this is my item_details_view</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;ImageView android:id="@+id/icon" android:layout_width="80dp" android:layout_height="80dp" android:paddingLeft="10dp" android:paddingRight="10dp" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload