【问题标题】:Unable change button text in onClickListener无法更改 onClickListener 中的按钮文本
【发布时间】: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


【解决方案1】:

如下更新您的适配器,此代码将在单击按钮时更新textcount

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 并从adapter class 中删除private fields

【讨论】:

    【解决方案2】:

    试试这个,

    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");    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多