【发布时间】:2013-12-18 06:04:34
【问题描述】:
这是我的示例代码
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 productItems 计数显然有效。更重要的是,我通过 xml 和适配器制作了这个结构。我的意思是所有 productCounts 按钮都具有相同的 ID。请问我的例外在哪里或给我一个建议。
【问题讨论】:
-
你能比这更清楚一点吗?
-
点击productItem时需要更改productCount。
-
您实际得到的响应是什么?
-
你想改变按钮文字,那么你应该做
productItem.setText()。您现在没有更改按钮文本。productName是文本视图而不是按钮 -
我也测试过productItem。无论如何它不起作用
标签: android button widget android-arrayadapter onitemclicklistener