简体   繁体   English

在ListView中更改单个项目的TextView

[英]Changing TextView of individual items in a ListView

I am in the making of a shopping list using a ListView with custom items with the posibility of increasing the amount of that item and also decreasing the amount. 我正在使用带有自定义项目的ListView制作购物清单,并可能增加该项目的数量并减少其数量。

At the moment, I get the right amount of items when I load them in to the shopping list, as can seen in the image below: 现在,当我将它们加载到购物清单中时,我得到的物品数量是正确的,如下图所示:

the image of the ListView and problem ListView的图像和问题

But the problem occurs when I am trying to increase/decrese the amount of each item by clicking the buttons, only the last item in the ListView gets updated by the clicks. 但是,当我尝试通过单击按钮来增加/减少每个项目的数量时,就会出现问题,单击只会更新ListView中的最后一个项目。

The reason for this is because i am re-declaring the TextView for each item, and the others (probably?) doesn't get stored anywhere. 这样做的原因是因为我为每个项目重新声明了TextView,而其他项目(可能是?)没有存储在任何地方。 So i read about "setTag()" and "getTag()" and tried to implement but this didn't work either (maybe beacuse i did it wrong). 因此,我阅读了有关“ setTag()”和“ getTag()”的内容,并尝试实现,但这都不起作用(也许是因为我做错了)。

I am using a custom class called "Item" which stores each item and the amount of that item. 我正在使用一个名为“ Item”的自定义类,该类存储每个项目和该项目的数量。 Otherwise, this is the custom adapter and the .xml 否则,这是自定义适配器和.xml

public class ShoppingListAdapter extends ArrayAdapter<Item> {
int amount;
TextView amountText;

private Context mContext;
int mResource;
public ShoppingListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Item> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;
}


@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    //get the item information
    String name = getItem(position).getName();
    String brand = getItem(position).getBrand();
    int id = getItem(position).getId();
    String img = getItem(position).getImg();
    amount = getItem(position).getAmount();

    //all info
    Item item = new Item(name,id,brand,img);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource,parent,false);

    TextView tVName = (TextView) convertView.findViewById(R.id.textView2);
    ImageView imgV = (ImageView) convertView.findViewById(R.id.imageView);
    TextView tVId = (TextView) convertView.findViewById(R.id.textView3);

    amountText = (TextView) convertView.findViewById( R.id.amountItem );

    tVName.setText(name);

    amountText.setText( String.valueOf( amount ) );
    amountText.setTag(position);

    Picasso.with(mContext).load(img).into(imgV);       // tVBrand.setText(brand);
    //tVId.setText(id);


    //Buttons
   Button addButton =(Button) convertView.findViewById(R.id.addValue);
   Button subButton =(Button) convertView.findViewById(R.id.subValue);

   addButton.setOnClickListener( new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           getItem(position).incrementAmount();

           //This gives the right position and the amount for all items
           Log.d("ifIncreased", " " + getItem(position).getAmount());
           Log.d("ifIncreased", position + "" );


           TextView newText = (TextView) amountText.getTag(position);

           newText.setText( String.valueOf( getItem(position).getAmount()));


       }
   } );

    subButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(getItem(position).getAmount() == 1)
            {
                //TODO remove from list; 
            }
            else{
                getItem(position).decrementAmount();
                Log.d("ifIncreased", position + "" );
                amountText.setText( String.valueOf( getItem(position).getAmount()) );
            }
        }
    } );


    return convertView;

}

and the XML: 和XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_weight="33.3"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="212dp"
        android:layout_height="60dp"
        android:layout_weight="33.3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/textView2"
            android:gravity=""
            android:paddingLeft="10dp"
            android:text="TextView3" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity=""
            android:paddingLeft="10dp"
            android:text="TextView2" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="112dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/amountItem"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="1"
            android:textStyle="bold" />

        <Button
            android:id="@+id/addValue"
            android:layout_width="40dp"
            android:layout_height="33dp"
            android:layout_gravity="center"
            android:text="+"
            android:textAlignment="center"
            android:textSize="12sp" />

        <Button
            android:id="@+id/subValue"
            android:layout_width="40dp"
            android:layout_height="33dp"
            android:layout_gravity="center"

            android:text="–"

            android:textAlignment="center"
            android:textSize="12sp" />

    </LinearLayout>

</LinearLayout>

So- since I am having the right values in the onClick functions, my question is, how can I change each TextView (the amount of that item) from the items in the full list? 因此-因为我在onClick函数中具有正确的值,所以我的问题是,如何从完整列表中的项目中更改每个TextView(该项目的数量)?

Try this to update single value into adapter.. make public method and that method called into fragment or activity when you updated any value and refresh adapter. 尝试此操作以将单个值更新为适配器。.使公共方法生效,并在更新任何值并刷新适配器时将该方法调用为片段或活动。

/**
 * this method used to update list item when broad cast received.
 * @param percentage
 * @param position
 */
public void updateProgress(double percentage, final int position) {
    feedBackVoList.get(position).setPercentage(percentage);
    notifyItemChanged(position);
}

after that updated value set into textview when you called notifyItemChanged(position) reset again that row or refresh that row. 在您调用notifyItemChanged(position)时将更新后的值设置为textview之后,再次重置该行或刷新该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android中设置单个listView项目的样式? - How to style individual listView items in android? 将listview项中的随机数据添加到Textview中 - Add random data from listview items into Textview 每次滚动时都会重新加载ListView Items(TextView) - ListView Items(TextView) is reloading everytime when Scrolling listView (Android) 中项目的语言不变 - Language not changing for items in listView (Android) ListView项目不会更改文本颜色 - ListView items not changing text colour ListView可以承载TextView项,这些项也显示在单独的TextView中 - Can ListView host TextView items that are also displayed in a separate TextView 在 Android 中,给定带有 TextView 和 RadioGroup 的项目的 ListView,如何动态添加项目? - In Android, given a ListView of items with TextView and RadioGroup, how to add items dynamically? 如何在所有选中的项目中获取作为 ListView 行一部分的 TextView 的值 - How to get the value of a TextView that is part of a ListView row in all checked items 如何将数据从textview传递到listview项目选择 - how to pass data from textview to listview items selection 如何通过单击每个单独的项目,使从Firebase的listview中检索的项目显示在文本框中? - How to make the items which are retrieved from firebase on a listview to be shown on text boxes by clicking each individual items?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM