Android 自定义 ListView CursorAdapter 更新最后一项
Android Custom ListView CursorAdapter updating the last item
我的自定义列表视图 (CursorAdapter) 有问题
当我在第二个项目上单击 btAdd 时,它正在更新最后一个列表项目。而且,当我单击一个项目时,它不会显示
Log.e("Selected", "" + position);
我想做的是更新第二个项目的编辑文本或任何被选中的项目。
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>
有什么想法吗?我很乐意提供任何帮助。谢谢
ibAdd
单击按钮总是更新最后一行 etQuantity
TextView,因为 etQuantity
是对 TextView 的引用,后者是 return 通过 getView
方法在上次调用。
要更新单击的行 TextView 值,请使用 setTag/getTag
方法在 ibAdd
和 ibSubtract
按钮的 onClick
中获取单击的行。喜欢:
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));
}
});
在 ibSubtract
上执行相同操作 单击按钮以显示减去的值
这是对 ρяσѕρєя K 的回答(对我有用)的改进。这是使用两个标签的更好方法:
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));
}
});
注意:ids R.id.TAG_ID_1 和 R.id.TAG_ID_2需要在res/values文件夹中定义为tags.xml。内容是这样的:
<resources>
<item name="TAG_ID_1" type="id"/>
<item name="TAG_ID_2" type="id"/>
</resources>
我的自定义列表视图 (CursorAdapter) 有问题
当我在第二个项目上单击 btAdd 时,它正在更新最后一个列表项目。而且,当我单击一个项目时,它不会显示
Log.e("Selected", "" + position);
我想做的是更新第二个项目的编辑文本或任何被选中的项目。
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>
有什么想法吗?我很乐意提供任何帮助。谢谢
ibAdd
单击按钮总是更新最后一行 etQuantity
TextView,因为 etQuantity
是对 TextView 的引用,后者是 return 通过 getView
方法在上次调用。
要更新单击的行 TextView 值,请使用 setTag/getTag
方法在 ibAdd
和 ibSubtract
按钮的 onClick
中获取单击的行。喜欢:
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));
}
});
在 ibSubtract
上执行相同操作 单击按钮以显示减去的值
这是对 ρяσѕρєя K 的回答(对我有用)的改进。这是使用两个标签的更好方法:
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));
}
});
注意:ids R.id.TAG_ID_1 和 R.id.TAG_ID_2需要在res/values文件夹中定义为tags.xml。内容是这样的:
<resources>
<item name="TAG_ID_1" type="id"/>
<item name="TAG_ID_2" type="id"/>
</resources>