Reputation: 5361
I have a custom listview having the following layout:
I have defined an onClick method for 'qadd' button and 'qsub' button in xml using android:onClick
property.
In my Activity class how do I select the 'quantity' textView for current position?
I tried this but it only updates 'quantity' textView at first position.
public void addClicked(View v) {
final int position = list.getPositionForView((View) v.getParent().getParent());
long rec_id = adapter.getItemId(position);
JSONObject o = (JSONObject) fullList.get(rec_id + "");
TextView q = (TextView)findViewById(R.id.quantity);
int quantity = Integer.parseInt((String) q.getText());
quantity++;
q.setText(quantity + "");
try {
String price = o.getString("price");
subtotal = subtotal + (Integer.parseInt(price) * quantity);
total_text = (TextView) findViewById(R.id.subtotal);
total_text.setText(subtotal + "");
// Log.d("position", price);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above code goes wrong at this line:
TextView q = (TextView)findViewById(R.id.quantity);
It always selects the textView at first position. Please ask if any other piece of code is needed.
Entire custom_list_layout.xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/checkBox1" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="@drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="@+id/item_img"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:orientation="vertical" >
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item name"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/item_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item description"
android:textColor="#343434"
android:textSize="10dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/rupee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Rs. "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
<TextView
android:id="@+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="0 "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom|right" >
<Button
android:id="@+id/qadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="addClicked"
android:text="+" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/qsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="subClicked"
android:text="-" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Upvotes: 0
Views: 1058
Reputation: 4816
Your textview to address should be the one within the list item row you specify in XML for the list adapter.
If you're using a custom adapter, within getView you should wire up the textview as follows:
// create a new TextView for each item referenced by the Adapter
public View getView(final int position, View convertView, final ViewGroup parent) {
View listView = convertView;
if (listView == null) { // if it's not recycled, initialize some attributes
// get layout from xml
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.browse_product_item, null);
}
final ListProductHolder holder;
if (listView.getTag() != null) {
holder = (listProductHolder) listView.getTag();
} else {
holder = new listProductHolder();
listView.setTag(holder);
holder.text= (TextView) listView.findViewById(R.id.quantity);
One you have the proper TextView from within each item in the listView, then it's a simple process to wire up an OnClickListener for that view and respond to those click events.
Upvotes: 1