编辑文本在滚动时丢失列表视图中的内容

时间:2015-10-04 06:46:58

标签: android listview android-edittext

我在列表视图中有多个编辑文本。编辑文本的数量不固定。滚动列表视图时,编辑文本会丢失其内容,或者其他一些焦点上的编辑文本显示某些其他编辑文本的值。我已经尝试过在Stack Overflow上提供的解决方案,但没有任何帮助。

我已经声明了一个哈希映射,我在其中存储位置和值,然后在哈希映射中设置编辑文本中的文本,但某处哈希映射会覆盖先前存储位置中的值。

如果需要代码,请告诉我。我会用我的代码更新我的问题。

使用我尝试的代码更新我的问题 -

public class JunkTabAdapter extends ArrayAdapter<JunkTabBean> {

    private Activity mContext;
    /* android.graphics.Typeface face; */
    private int listItemForFile;
    private static ArrayList<JunkTabBean> itemList = new ArrayList<JunkTabBean>();

    private ListView download_row;
    ImageLoader imageloader;
    long fileLength;
    String getImage, saveinternalImagePath;
    Bitmap getBitmapImage;
    HomeLoader hl;
    NewImageLoader img;
    String fromWhere;

    String value="";
    String qty="";
    List<Integer> totAmtList = new ArrayList<Integer>();
    JunkTab junkTab;
    public int totAmt=0;

    ViewHolder holder;

    Map<Integer,String>  map = new HashMap<Integer,String>();
    int count=0;
    public JunkTabAdapter(Activity context, int listItemForFile,
            ArrayList<JunkTabBean> itemList,String fromWhere,JunkTab junkTab) {
        super(context, listItemForFile, itemList);
        this.mContext = context;
        this.listItemForFile = listItemForFile;
        this.itemList = itemList;
        this.fromWhere=fromWhere;
        this.junkTab=junkTab;

        img=new NewImageLoader(context);
    }

    public int getCount() {
        return itemList.size();
    }

    public JunkTabBean getItem(JunkTabBean position) {
        return position;
    }

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

    public class ViewHolder {

        public ImageView imgItem;
        public ImageView imgInfo;
        public ImageView imgDel;
        public EditText edQty;
        public TextView tvAmt;
        public TextView tvTotAmt;
        public View viewDivider;

        int reference;

    }
    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) {



          View row = convertView; 

          if (row == null) {

                holder = new ViewHolder();

                LayoutInflater vi = (LayoutInflater)    
                        mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = vi.inflate(R.layout.junktabcomponent, null);

                holder = new ViewHolder();
                  holder.imgItem = (ImageView)row.findViewById(R.id.imgItem);
                  holder.imgDel = (ImageView)row.findViewById(R.id.imgDelete);
                  holder.imgInfo = (ImageView)row.findViewById(R.id.imgInfo);
                  holder. edQty = (EditText)row.findViewById(R.id.edQty); 
                  holder.tvAmt = (TextView)row.findViewById(R.id.tvAmt); 
                  holder.tvTotAmt = (TextView)row.findViewById(R.id.tvTotalAmt); 
                  holder.viewDivider = (View)row.findViewById(R.id.view1); 


                  row.setTag(holder);
                //  holder.edQty.setTag(itemList.get(position).getId());


            } else {

                holder = (ViewHolder) row.getTag();

            }


         /* LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
          row = inflater.inflate(R.layout.junktabcomponent,null, false);
          holder = new ViewHolder();
          holder.imgItem = (ImageView)row.findViewById(R.id.imgItem);
          holder.imgDel = (ImageView)row.findViewById(R.id.imgDelete);
          holder.imgInfo = (ImageView)row.findViewById(R.id.imgInfo);
          holder. edQty = (EditText)row.findViewById(R.id.edQty); 
          holder.tvAmt = (TextView)row.findViewById(R.id.tvAmt); 
          holder.tvTotAmt = (TextView)row.findViewById(R.id.tvTotalAmt); 
          holder.viewDivider = (View)row.findViewById(R.id.view1); */

         // holder.edQty.setText(qty);


          holder.edQty.setInputType(InputType.TYPE_CLASS_NUMBER);


              final JunkTabBean item = (JunkTabBean) itemList.get(position);


              img.DisplayImage(item.getImage(), holder.imgItem);
              holder.edQty.setHint(item.getUnits());

             // final int pos=(Integer) holder.edQty.getTag();

              if(map.containsKey(holder.reference))
              {
                  holder.edQty.setText(map.get(holder.reference).toString());
              }
              holder.edQty.addTextChangedListener(new TextWatcher()

              {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    /*value=value+s;
                    Log.e("value",value);*/


                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    //map.put(pos,s.toString());

                    map.put(holder.reference,s.toString());

                    Log.e("map",""+map);
                    //holder.tvAmt.setText(""+(Integer.parseInt(item.getPrize()) * Integer.parseInt(map.get(holder.reference))));
                    //holder.tvAmt.setText(""+(Integer.parseInt(item.getPrize()) * Integer.parseInt(s.toString())));
                    /*totAmt=totAmt+(Integer.parseInt(item.getPrize()) * Integer.parseInt(s.toString()));
                    junkTab.setTotAmt(""+totAmt);*/
                    //qty=s.toString();

                }
            });

              holder.reference=position;
             // holder.edQty.setText(map.get(position));

              //  if(!map.get(position).contains(""))

              /*  if(!holder.edQty.getText().toString().equalsIgnoreCase(""))
              holder.tvAmt.setText(""+(Integer.parseInt(item.getPrize())* Integer.parseInt(holder.edQty.getText().toString())));*/

          return row;

    }



 }

2 个答案:

答案 0 :(得分:2)

这种private class MyViewHolder { public EditText myEditText; public ViewGroup myListItemLayout; } 问题的通用解决方案是View Holder模式。它对绩效也有很小的积极影响。

基本上,我们的想法是创建一个表示一个Listview项的UI的类。它存储View实例(EditTexts和whatnot)及其内容完整。

getView()

首次在列表适配器public View getView(int position, View convertView, ViewGroup parent) { MyViewHolder holder = new ViewHolder(); // Get the data object for this row. MyDataItem item = items.get(position); // If we receive a null View we need to construct a one. if (convertView == null) { convertView = inflater.inflate(R.layout.my_list_item, parent, false); // Store the UI elements in the View Holder. holder.myEditText = (EditText)view.findViewById(R.id.editTextMyEditText); holder.myListItemLayout = (ViewGroup)view.findViewById(R.id.layoutMyListItemLayout); // Strore the View Holder as the View's tag. convertView.setTag(holder); } // Get the holder from the View's tag. holder = (ViewHolder) convertView.getTag(); ... } 中访问视图时会创建View Holder对象,然后将其存储为View对象&#39;标签

getView()

现在,稍后在convertView != nullfindViewById()中再次访问相同的视图时,我们将跳过所有holder = (ViewHolder) convertView.getTag(); 代码并直接进入:

convertView.myTextView.setText("Hello world!")

现在,视图具有正确的内容,而不是其他列表项的数据。

至于getView()代码的其余部分,您只需用对View Holder的引用替换对View convertView的所有引用,即代替holder.myTextView.setText("Hello world!"),您将使用tableView.reloadData()

一些参考文献:
https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
https://dzone.com/articles/optimizing-your-listview
http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder

答案 1 :(得分:0)

您的适配器必须为每个EditText保存文本:

  1. 创建地图以保存值
  2. 为每个EditText
  3. 定义并设置通用TextWatcher
  4. 在editText上设置标签(使用其位置或给出您想要的任何ID)您将使用此标记作为hashMap中的键
  5. 创建一个方法来检索给定EditText的值,并将HashMap值保存在cache或sharedPreferences中。
  6. 检查editText的位置,并通过从存储的cache / sharedPreferences中检索它来相应地填充值。
  7. 仅关注Get all EditText values提示。