我的自定义列表视图(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>
有什么想法吗?我很乐意感谢任何帮助。感谢。
答案 0 :(得分:1)
ibAdd
按钮点击始终更新最后一行etQuantity
TextView,因为etQuantity
是对TextView的引用,后者在上次调用时通过getView
方法返回。
要更新点击的行TextView值,请使用setTag/getTag
方法获取onClick
ibAdd
和ibSubtract
按钮中的点击行。像:
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
按钮上执行相同操作单击以显示减去的值
答案 1 :(得分:0)
这改善了ρяσѕρєяK的答案(这对我有用)。这是使用两个标签的更好方法:
CREATE TABLE company_details
(
company_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
company_name VARCHAR(200) NOT NULL,
company_address VARCHAR(200),
company_contactperson VARCHAR(200),
company_contactno VARCHAR(200),
company_email VARCHAR(200)
);
注意:需要在 res / values 文件夹中定义ids R.id.TAG_ID_1 和 R.id.TAG_ID_2 as tags.xml 。内容如下:
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));
}
});