这是我的示例代码
public class ProductAdapter extends ArrayAdapter<Product> {
LayoutInflater inflater;
static int resId;
private TextView productName;
private Button productItem;
private Button productCount;
PlaceItemHelper helper = null;
public ProductAdapter(Context context, int resource, int textViewResourceId,List<Product>products) {
super(context, resource, textViewResourceId, products);
this.inflater = LayoutInflater.from(context);
setResId(resource);
}
public View getView(int position, View convertView, ViewGroup parent) {
Product product = getItem(position);
if (convertView == null) { // if it's not recycled, initialize some attributes
convertView = inflater.inflate(resId, parent, false);
productName = (TextView) convertView.findViewById(R.id.productNameTextView);
productItem = (Button) convertView.findViewById(R.id.product_button);
productCount = (Button) convertView.findViewById(R.id.product_count);
helper = new PlaceItemHelper(productName, productItem, productCount);
convertView.setTag(helper);
Log.i("info ", " true");
} else {
helper = (PlaceItemHelper) convertView.getTag();
productName = helper.getTextView();
Log.i("info ", " false ");
}
productName.setText(product.getName());
Log.i("ssssssssss", productCount + "");
productCount.setText("100");
productItem.setOnClickListener(new OnClickListener() {
int count = 0;
@Override
public void onClick(View v) {
count++;
Log.i("ddddddddddddddd", count + "");
Log.i("ddddddddddddddd", productCount + "");
productItem.setText(count+"");
helper. getProductCount().setText("200");
productName.setText("Name");
}
});
return convertView;
}
public static int getResId() {
return resId;
}
public static void setResId(int resId) {
ProductAdapter.resId = resId;
}
}
和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:layout_gravity="center" >
<Button
android:id="@+id/product_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:padding="10dp"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="@+id/productNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="BB"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/product_count"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
android:background="@drawable/round_button"
android:text="0"
/>
</FrameLayout>
首先,它显示100和productName作为硬编码。当我点击productItem时,我的t change productCount and productName, but each productItem
计数显然无法实现。更重要的是,我用xml和适配器制作了这个结构。我的意思是所有productCounts按钮都具有相同的ID。请问我的例外情况或给我一个建议。
答案 0 :(得分:2)
按如下方式更新您的适配器,此代码会在点击按钮时更新text
和count
:
public class ProductAdapter extends ArrayAdapter<Product> {
LayoutInflater inflater;
static int resId;
public ProductAdapter(Context context, int resource, int textViewResourceId, List<Product> products) {
super(context, resource, textViewResourceId, products);
this.inflater = LayoutInflater.from(context);
setResId(resource);
}
public View getView(int position, View convertView, ViewGroup parent) {
Product product = getItem(position);
final PlaceItemHelper helper;
if (convertView == null) { // if it's not recycled, initialize some attributes
convertView = inflater.inflate(resid, parent, false);
TextView productName = (TextView) convertView.findViewById(R.id.productNameTextView);
TextView productItem = (Button) convertView.findViewById(R.id.product_button);
TextView productCount = (Button) convertView.findViewById(R.id.product_count);
helper = new PlaceItemHelper(productName, productItem, productCount);
convertView.setTag(helper);
} else {
helper = (PlaceItemHelper) convertView.getTag();
}
helper.getProductName().setText(product.getName());
helper.getProductCount().setText("100");
helper.getProductItem().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helper.getProductCount().setText("200");
helper.getProductName().setText("Name");
}
});
return convertView;
}
public static int getResId() {
return resId;
}
public static void setResId(int resId) {
ProductAdapter.resId = resId;
}
}
请仔细阅读上述代码并注意我如何使用PlaceItemHelper helper
并从private fields
中删除了adapter class
。
答案 1 :(得分:-1)
试试这个,
productItem.setOnClickListener(new OnClickListener() {
int count = 0;
@Override
public void onClick(View v) {
count++;
Log.i("ddddddddddddddd", count + "");
Log.i("ddddddddddddddd", productCount + "");
helper.productCount.setText("200");
helper.productName.setText("Name");
}
});