简体   繁体   中英

Viewholder How to correctly update views

I'm trying to update listview with dialog box. But when I'm input the quantity in dialog box then it will show to listview but when I scroll the listview other row will update also. I just want to update the row which I clicked.

Below is my adapter code. Someone please help me. Thanks so much.

 public class MyAppAdapter extends BaseAdapter {

    public class ViewHolder {
        TextView productid, productname, pcs, cs;
        ImageView dist;
    }

    public List<Product_List> parkingList;

    public Context context;
    ArrayList<Product_List> arrayList;

    private MyAppAdapter(List<Product_List> apps, Context context) {
        this.parkingList = apps;
        this.context = context;
        arrayList = new ArrayList<Product_List>();
        arrayList.addAll(parkingList);
    }

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

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

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

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

        View rowView = convertView;
        final MyAppAdapter.ViewHolder viewHolder;
        if (rowView == null) {
            LayoutInflater inflater = getLayoutInflater();
            rowView = inflater.inflate(R.layout.productlist, parent, false);
            viewHolder = new MyAppAdapter.ViewHolder();
            viewHolder.productid = (TextView) rowView.findViewById(R.id.productid);
            viewHolder.productname = (TextView) rowView.findViewById(R.id.productname);
            viewHolder.pcs = (TextView) rowView.findViewById(R.id.pcs);
            viewHolder.cs = (TextView) rowView.findViewById(R.id.cs);
            viewHolder.dist = (ImageView) rowView.findViewById(R.id.dist);
            rowView.setTag(viewHolder);
        } else {
            viewHolder = (MyAppAdapter.ViewHolder) convertView.getTag();
        }

        viewHolder.productid.setText(parkingList.get(position).getInvtID() + "");
        viewHolder.productname.setText(parkingList.get(position).getDescr() + "");

        final String a = viewHolder.productid.getText().toString();
        final String b = viewHolder.productname.getText().toString();

        if ( position % 2 == 0) {
            rowView.setBackgroundColor(Color.parseColor("#FFFFFF"));
        } else {
            rowView.setBackgroundColor(Color.parseColor("#ECEAEA"));
        }

        rowView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                final Dialog dialog = new Dialog(InventoryChecking1.this);
                dialog.setTitle("Inventory Checking");
                dialog.setContentView(R.layout.inventory);
                final TextView productids = (TextView) dialog.findViewById(R.id.productids);
                productids.setText(a);
                final TextView productdesc = (TextView) dialog.findViewById(R.id.productdesc);
                productdesc.setText(b);
                final EditText pcss = (EditText) dialog.findViewById(R.id.pcss);
                final EditText css = (EditText) dialog.findViewById(R.id.css);
                final RadioButton btnyes = (RadioButton) dialog.findViewById(R.id.btnyes);
                final RadioButton btnno = (RadioButton) dialog.findViewById(R.id.btnno);
                btnno.setChecked(true);
                Button btnsave = (Button) dialog.findViewById(R.id.btnsave);
                btnsave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        viewHolder.pcs.setText(pcss.getText().toString());

                        viewHolder.cs.setText(css.getText().toString());

                        if (btnyes.isChecked()) {
                            viewHolder.dist.setImageResource(R.drawable.done);
                        } else {
                            viewHolder.dist.setImageResource(R.drawable.no);
                        }

                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });

        return rowView;
    }
}

in your class where you call the adapter , set it as follow.

myAppAdapter = new MyAppAdapter();

imagegrid.setAdapter(myAppAdapter);

//imagegrid is a gridview where i need to set the adapter

Hope it might help.

You should call notifyDataSetChanged ()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

DEMO

tnsave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                       // Your work        
                       notifyDataSetChanged() 
                    }
                });

FYI

It will be a good approach if you use RecyclerView .

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