Question

Here is my sample code

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

}

and xml as well

    <?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>

For a start, it displays 100 and productName as a hardcode. When i click productItem i couldnt change productCount and productName, but each productItems count works apparently.What`s more, i made this structure by xml and adapter. I mean all productCounts button have the same id. Please where is my exception or give me a suggest.

enter image description here

Was it helpful?

Solution

Update your adapter as follows, this code will update the text and count when button is clicked:

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

Please go through the above code and notice how I have used the PlaceItemHelper helper and removed private fields from the adapter class.

OTHER TIPS

Try this,

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");    
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top