Viewholder如何正确更新视图

时间:2018-02-08 06:27:00

标签: java android android-adapter

我尝试使用对话框更新listview。但是,当我在对话框中输入数量时,它将显示到列表视图,但是当我滚动列表视图时,其他行也将更新。我只想更新我点击的行。

以下是我的适配器代码。 有人请帮帮我。非常感谢。

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

2 个答案:

答案 0 :(得分:1)

在您调用适配器的类中, 设置如下。

myAppAdapter = new MyAppAdapter();

imagegrid.setAdapter(myAppAdapter);

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

希望它可能会有所帮助。

答案 1 :(得分:0)

您应该致电notifyDataSetChanged ()

  

通知附加的观察者基础数据已经存在   已更改,任何反映数据集的视图都应自行刷新。

<强>样本

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

                       // Your work        
                       notifyDataSetChanged() 
                    }
                });

<强> FYI

如果您使用 RecyclerView ,这将是一个很好的方法。