如何使用listview行中的按钮onclicklistener更改textview文本

时间:2016-03-06 09:12:16

标签: android listview

我有以下AdapterClass:

public class ItemListAdapter extends BaseAdapter {

//My private ViewHolder class
private static class ViewHolder
{
    ImageButton plusButton;
    ImageButton minusButton;
    TextView item_name, item_price, item_quantity, item_count;
    NetworkImageView networkImageView;
}
private static final String TAG = KindHeart.class.getSimpleName();
private Activity activity;
private LayoutInflater inflater;
private ArrayList<Items> itemsArrayList;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
int count = 0;

public ItemListAdapter(Activity activity, ArrayList<Items> itemsArrayList) {
    this.activity = activity;
    this.itemsArrayList = itemsArrayList;
}

@Override
public int getCount() {return itemsArrayList.size();}

@Override
public Object getItem(int position) {
    return itemsArrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final  ViewHolder viewHolder;
    if (inflater == null)
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();


    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.items_row, null);
        viewHolder = new ViewHolder();
        viewHolder.networkImageView = (NetworkImageView)convertView.findViewById(R.id.networkImageView);
        viewHolder.item_name = (TextView)convertView.findViewById(R.id.item_name);
        viewHolder.item_price = (TextView)convertView.findViewById(R.id.item_price);
        viewHolder.item_quantity = (TextView)convertView.findViewById(R.id.item_quantity);
        viewHolder.item_count = (TextView)convertView.findViewById(R.id.item_count);
        viewHolder.item_count.setTag(position);
        viewHolder.plusButton = (ImageButton)convertView.findViewById(R.id.plusButton);
        viewHolder.plusButton.setTag(position);
        viewHolder.minusButton = (ImageButton)convertView.findViewById(R.id.minusButton);
        viewHolder.minusButton.setTag(position);
        convertView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder)convertView.getTag();
    }
    //Setting item_data in item_row
    //networkImageView.setImageUrl(item.getImage(),imageLoader);
    Items item = itemsArrayList.get(position);
    viewHolder.networkImageView.setImageUrl("", imageLoader);
    viewHolder.item_name.setText(item.getName());
    viewHolder.item_price.setText("₹" + item.getPrice());
    viewHolder.item_quantity.setText(item.getQuantity()+"gm");
    viewHolder.plusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            count++;
            viewHolder.item_count.setText(count + "");
            notifyDataSetChanged();
        }
    });
    /*
    viewHolder.minusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (a[position] > 0)
                a[position]--;
            //viewHolder.item_count.setText(a[position]);
            //notifyDataSetChanged();
        }
    });*/


    return convertView;
}

}

我想要的是当我点击加号按钮时,我的相应文本视图'item_count'会更新。一切都很顺利,但当我点击加号按钮时,列表中的多个文本视图会更新。我正在分享截图:

1st half of list

2nd half of list

R.id.items_row:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout         xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp"
>

<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/networkImageView"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp" />

<!-- Item name -->
<TextView
    android:id="@+id/item_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/networkImageView"
    android:layout_toRightOf="@+id/networkImageView"
    android:textSize="@dimen/title"
    android:textStyle="bold"
    />

<!-- Item price -->
<TextView
    android:id="@+id/item_price"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@+id/networkImageView"
    android:layout_below="@+id/item_name"
    android:textColor="@color/genre"
    android:textSize="@dimen/genre"
    />

<!-- Item quantity -->
<TextView
    android:id="@+id/item_quantity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/networkImageView"
    android:layout_below="@+id/item_price"
    android:layout_marginTop="1dip"
    android:textSize="@dimen/rating"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="right"
    android:layout_toRightOf="@id/item_quantity"
    android:layout_marginTop="10dp"
    >

<ImageButton
    android:layout_margin="5dp"
    android:id="@+id/plusButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/plus"
    android:focusable="false"
    android:background="@android:color/transparent"
    android:focusableInTouchMode="false"
    />

<TextView
    android:layout_margin="1dp"
    android:id="@+id/item_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/year"
    android:text="0"
    android:layout_gravity="center_vertical"
    android:textSize="@dimen/year" />

<ImageButton
    android:layout_margin="5dp"
    android:id="@+id/minusButton"
    android:background="@android:color/transparent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:src="@drawable/minus"
    android:focusableInTouchMode="false"
    />
</LinearLayout>

</RelativeLayout>

我正在努力制作像PepperTap应用程序一样的列表。请帮帮我。您可以参考PepperTap应用程序来查看。 谢谢和问候。

1 个答案:

答案 0 :(得分:0)

只需在OnBindViewHolder的onClick上添加setText即可 希望它有所帮助:)

public void onBindViewHolder(DisplayTrainsAdapter.ViewHolder viewHolder,final int position){

viewHolder.tvSrc.setText(mDataset.get(position).strSrc);
viewHolder.tvSrc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       //Main Trick 
       viewHolder.tvSrc.setText("Your text");
    }
});
}