简体   繁体   中英

ListView Items(TextView) is reloading everytime when Scrolling

i created an application in which i want to perform a task like to add the value and decrease the value by clicking button.and it is working fine but the problem i'm facing is that when i'm scrolling the listview then the value is getting zero 0.

but when i click on increase button or decrease button then it is giving me the true value.

ie : when i add 2 qty. by clicking.

and when i scrolling the page then it will become 0 and when i click on addButton it will gives me 3.

or when i scrolling the page then it will become 0 and when i click on minus Button it will gives me 1

when i scroll away from it the TextView displayInteger is discarded and forgets its value.

Here the complete code :-

listViewadapter.java

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

    public View getView(final int position, View convertView, ViewGroup parent) {

        TextView mname , pmethod2, pamount3, premark4;
        final ImageView increase,decrease;
        final Button add;
        inflater = (LayoutInflater) cntx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LayoutInflater inflater = LayoutInflater.from(cntx);

        convertView = inflater.inflate(R.layout.raw_order, parent,
                false);
        decrease = (ImageView) convertView.findViewById(R.id.decrease);
        increase= (ImageView) convertView.findViewById(R.id.increase);
        add= (Button) convertView.findViewById(R.id.add);

        mname = (TextView) convertView.findViewById(R.id.mname);
        mname.setText("  " + o_aproduct.get(position));


        final TextView displayInteger = (TextView) convertView.findViewById(R.id.integer_number);
        //add.setText("  " +count);

        increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int count = sia.get(position);
                count= count + 1;
                sia.put(position, count);
                displayInteger.setText("" + count);
                add.setText("  " +count);

                if (count > 0 ) { decrease.setEnabled(true); }
            }
        });
        decrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int count = sia.get(position);
                count= count- 1;
                sia.put(position, count);
                displayInteger.setText("" + count);
                add.setText("  " +count);
                if (count > 0 )
                {decrease.setEnabled(true);}
                else if (count == 0 ){
                    decrease.setEnabled(false);
                }
                else { decrease.setEnabled(false);}
            }
        });




        return convertView;
    }

With ListView (or RecyclerView ), your event listener should never change the View.

Event listeners should update the Model then call notifyDataSetChanged() . notifyDataSetChanged() tells the ListView to refresh from your updated Model.

Only make changes to the View from within getView() directly.

    final TextView displayInteger = (TextView) convertView.findViewById(R.id.integer_number);
    int count = sia.get(position);
    displayInteger.setText("" + count);
    decrease.setEnabled(count > 0);

    increase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int count = sia.get(position);
            count++;
            sia.put(position, count);
            notifyDataSetChanged();
        }
    });
    decrease.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int count = sia.get(position);
            count--;
            sia.put(position, count);
            notifyDataSetChanged();
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM