Reputation:
I'm having issue with my custom listview (CursorAdapter)
When I click btAdd on 2nd item, it's updating the last list item. And also, when I click an item, it doesn't display this
Log.e("Selected", "" + position);
What I wanna do is to update the edittext of the 2nd item or whatever is the selected item.
MainActivity.class
private void initControls(View rootView) {
lv = (ListView) rootView.findViewById(R.id.listView1);
MyTextView tvHeader = (MyTextView) rootView.findViewById(R.id.tvHeader);
DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
DBConnector.dbConnect(dbHelper);
cursor = dbHelper.getAllItems(user);
adapterProd = new CustomAdapter(getActivity(), cursor);
lv.setAdapter(adapterProd);
adapterProd.notifyDataSetChanged();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.e("Selected", "" + position);
}
});
if(adapterProd.getCount() < 1) {
tvHeader.setVisibility(View.VISIBLE);
tvHeader.setText(getResources().getString(R.string.no_saved_items));
} else {
tvHeader.setVisibility(View.GONE);
}
}
class CustomAdapter extends CursorAdapter
{
TextView name, price, description;
EditText etQuantity;
ImageButton ibAdd, ibSubtract;
ImageView img;
LayoutInflater inflater;
@SuppressWarnings("deprecation")
public CustomAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(final View itemView, Context context, Cursor cursor) {
name = (TextView) itemView.findViewById (R.id.tvName);
price = (TextView) itemView.findViewById(R.id.tvPrice);
description = (TextView) itemView.findViewById(R.id.tvDescription);
etQuantity = (EditText) itemView.findViewById(R.id.etQuantity);
etQuantity.append(String.valueOf(quantity));
etQuantity.setSelection(etQuantity.getText().length());
ibAdd = (ImageButton) itemView.findViewById(R.id.ibAdd);
ibSubtract = (ImageButton) itemView.findViewById(R.id.ibSubtract);
img = (ImageView) itemView.findViewById(R.id.imageView);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
final String selected =
cursor.getString(
cursor.getColumnIndex(Constants.KEY_ID));
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//Case 1
Log.e("Selected", "" + selected);
itemView.setPressed(true);
}
else {
//case 2
Log.e("Deselected", "" + selected);
itemView.setPressed(false);
}
}
});
ibAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quantity++;
etQuantity.setText(String.valueOf(quantity));
}
});
ibSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
quantity--;
if(quantity < 1) {
quantity = 1;
}
etQuantity.setText(String.valueOf(quantity));
}
});
if(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_NAME)) != null) {
name.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_NAME)));
price.setText(Formatter.formatWithPesoSign(cursor.getString(
cursor.getColumnIndex(
Constants.KEY_PRODUCT_PRICE))));
description.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_NAME)));
} else {
price.setVisibility(View.GONE);
name.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_PARTITION_KEY)));
description.setText(cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_ROW_KEY)));
if(ConnectionDetector.hasNetworkConnection(getActivity())) {
GetSavedItemAsyncTask task = new GetSavedItemAsyncTask(
getActivity(), accessToken, sessionKey,
cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_PARTITION_KEY)),
cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_ROW_KEY)),
cursor.getString(
cursor.getColumnIndex(Constants.KEY_ID)),
user);
task.execute();
}
}
Picasso.with(getActivity())
.load(Constants.displayProductThumbnail(Constants.MERCHANT,
cursor.getString(
cursor.getColumnIndex(Constants.KEY_PRODUCT_ROW_KEY)), "200"))
.placeholder(R.drawable.placeholder)
.into(img);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.list_item, parent, false);
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:background="@drawable/card_background_selector"
android:descendantFocusability="afterDescendants">
<ImageView
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView"
android:layout_toLeftOf="@+id/checkBox"/>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:id="@+id/tvPrice"
android:textColor="@color/cadmium_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/price"
android:layout_below="@+id/tvDescription"
android:layout_toRightOf="@+id/imageView" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/description"
android:layout_below="@+id/tvName"
android:layout_toRightOf="@+id/imageView" />
<LinearLayout
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/tvPrice"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView" >
<ImageButton
style="@style/My.Button"
android:id="@+id/ibSubtract"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_remove_white_36dp"
android:padding="5dp" />
<EditText
android:background="#fff"
android:id="@+id/etQuantity"
android:layout_width="30dp"
android:layout_height="30dp"
android:focusable="false"
android:gravity="center"
android:inputType="number" />
<ImageButton
style="@style/My.Button"
android:id="@+id/ibAdd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_add_white_36dp"
android:padding="5dp" />
</LinearLayout>
<CheckBox
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:checked="false" />
</RelativeLayout>
Any ideas? I would gladly appreciate any help. Thanks.
Upvotes: 0
Views: 1049
Reputation: 14287
This is improving on ρяσѕρєя K's answer (which had worked for me). This is a better way of using two tags:
ibAdd.setTag(R.id.TAG_ID_1, etQuantity);
ibAdd.setTag(R.id.TAG_ID_2, new Integer(quantity));
ibAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView etQuantityView = (TextView) v.getTag(R.id.TAG_ID_1);
int prevQuantity = (Integer) v.getTag(R.id.TAG_ID_2);
prevQuantity++;
etQuantityView.setText(String.valueOf(prevQuantity));
}
});
NOTE: The ids R.id.TAG_ID_1 and R.id.TAG_ID_2 are required to be defined in the res/values folder as tags.xml. The contents are like this:
<resources>
<item name="TAG_ID_1" type="id"/>
<item name="TAG_ID_2" type="id"/>
</resources>
Upvotes: 0
Reputation: 132982
ibAdd
Button click always update last row etQuantity
TextView because etQuantity
is reference to TextView which is return by getView
method on last call.
To update clicked row TextView value use setTag/getTag
method to get clicked row in onClick
of ibAdd
and ibSubtract
Buttons. like:
ibAdd.setTag(etQuantity);
ibAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView etQuantityView=(TextView)v.getTag();
quantity++;
etQuantityView.setText(String.valueOf(quantity));
}
});
Do same on ibSubtract
Button click to show subtracted value
Upvotes: 1