如何在android中滚动后修复listview中项目的位置

时间:2015-04-15 06:03:54

标签: android listview

我在android app中创建了一个动态列表视图。

listview的每个项目都有不同的值。 我已经使用动态edittexts产品。 但是当我向上或向下滚动时,值正在改变它们的位置。

我的代码为,

public View getView(int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.addeditem, null);

                holder.qty = (EditText) convertView.findViewById(R.id.editTextqty);
                holder.ProductName = (TextView) convertView.findViewById(R.id.prodname);

                convertView.setTag(holder);
            } 
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.qty.setId(position);
            try
            {
                tempQty = MyArrayList.get(position).get("qty");

                Log.i("Test", position+" tempQty= "+tempQty);

                holder.qty.setText(tempQty);
            } 
            catch (Exception e)
            {
                holder.qty.setText("");
                e.printStackTrace();
            }

            Log.i("Test","["+ position+"] Qty = " +holder.qty.getText().toString());

            holder.ProductName.setText(mproductName.get(position).get("MainProduct").toString());

            holder.qty.setOnFocusChangeListener(new OnFocusChangeListener()
            {
                public void onFocusChange(View v, boolean hasFocus)
                {
                    if (!hasFocus)
                    {
                        final int position = v.getId();
                        final EditText Caption = (EditText) v;

                        String qty = Caption.getText().toString();
                        String name = mproductName.get(position).get("MainProduct").toString();
                        String itemcode = mproductName.get(position).get("ItemCode").toString();

                        try 
                        {
                            if(!qty.equalsIgnoreCase("")||!qty.equalsIgnoreCase(null))
                            {
                                HashMap<String, String> hashMap=new HashMap<String, String>();
                                hashMap.put("name",name);
                                hashMap.put("itemcode",itemcode);
                                hashMap.put("qty",qty);
                                MyArrayList.add(position,hashMap);

                            }
                        } 
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }

                    }
                }
            });

            return convertView;
        }

屏幕截图..

There are dynamic qty edittexts

请帮帮我。

3 个答案:

答案 0 :(得分:1)

当适配器中的数据集发生变化时,您需要调用notifyDataSetChanged();。在你的情况下,你需要在这里打电话

    try {
        if (!qty.equalsIgnoreCase("") || !qty.equalsIgnoreCase(null)) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("name", name);
            hashMap.put("itemcode", itemcode);
            hashMap.put("qty", qty);
            MyArrayList.add(position, hashMap);

            notifyDataSetChanged(); //add this line change
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

IMPT :仅当对EditText的关注发生更改时才会调用setOnFocusChangeListener。我想说的是当你向下/向上滚动编辑一个EditText时(视图不可见),你的数据集不会更新,当你向下/向上滚动时,你将获得EditText中的旧数据。

答案 1 :(得分:0)

问题在于,您没有为每一行使用不同的视图持有者,并且在回收列表项目视图时,您的持有者会被洗牌。

您可以看到这只是向持有者添加 final int position 字段,存储getView位置参数的位置;调试,你会看到会发生什么。

因此,您无法使用持有人存储数量值

实施例: -

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

final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();

if (convertView == null) {

    convertView = inflater.inflate(R.layout.addeditem, null);

    holder = new ViewHolder();

答案 2 :(得分:-1)

摆脱持有人。它只是通过长列表和复杂的项目布局来提高性能。在引入任何优化之前,尝试使其在没有持有者的情况下工作。如果你取下持有人,你的问题可能就会消失。