ListView中的EditText在滚动时丢失内容

时间:2016-04-22 12:18:18

标签: android listview android-edittext

我一直在寻找解决方案来解决我的问题,但是没有一个对我有用。 :/ /我在每个列表项中都有一个带有edittext的列表视图,当向下滚动并再次向上移动时,已编辑的edittext的内容已丢失或其他edittext已占用其值。

请帮帮我一个

MainActivity: -

public class MainActivity extends AppCompatActivity {

    private ArrayList<Friend> friendArrayList;
    ListViewAdapter adapter;

    String[] countriessArray =  { "USA", "USA", "USA", "USA",
            "China", "Japan", "France", "USA","USA", "USA", "USA", "USA",
            "China", "Japan", "France", "USA"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        friendArrayList = new ArrayList<Friend>();
        for (int i = 0; i < countriessArray.length; i++) {

            Friend item = new Friend(countriessArray[i]);
            friendArrayList.add(item);
        }

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setItemsCanFocus(true);

        adapter = new ListViewAdapter(this, R.layout.item_listview, friendArrayList);

        listView.setAdapter(adapter);
    }

ListViewAdaper: -

public class ListViewAdapter extends ArrayAdapter<Friend> {

    ViewHolder holder;
    private List<Friend> myFriends;
    private Activity activity;

    public static HashMap<Integer,String> myList=new HashMap<Integer,String>();

    public ListViewAdapter(Activity context, int resource, List<Friend> objects) {

        super(context, resource, objects);
        this.activity = context;
        this.myFriends = objects;
    }

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

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        final int viewpos = position;

        // If holder not exist then locate all view from UI file.
        if (convertView == null) {
            // inflate UI from XML file
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            // get all UI view
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            // if holder created, get tag from view
            holder = (ViewHolder) convertView.getTag();
        }

        holder.friendName.setText(getItem(position).getName());

        //Edit Text Listeners:-

        holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                myList.put(position,s.toString().trim());
            }
        });

        holder.editText.setText(myList.get(position));

        return convertView;
    }

    private static class ViewHolder {

        private TextView friendName;
        private EditText editText;

        public ViewHolder(View v) {
            friendName = (TextView) v.findViewById(R.id.name);
            editText = (EditText) v.findViewById(R.id.edit);
        }
    }
}

自定义对象: -

public class Friend {

    private String name,editText;
    private int selection = 0;

    public Friend(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setSelection(int selection) {
        this.selection = selection;
    }

    public int getSelection() {
        return selection;
    }

    public void setEditText(String editText) {
        this.editText = editText;
    }

    public String getEditText() {
        return editText;
    }
}

3 个答案:

答案 0 :(得分:2)

您需要将afterTextChanged中的数据存储为与在文本

中设置的对象相同的对象
   holder.friendName.setText(myFriends.get(position).getName());

        //Edit Text Listeners:-

        holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                myFriends.get(position).setName(s.toString);
            }
        });

答案 1 :(得分:0)

好吧,首先,所有ListView都使用循环视图。这意味着当您向上和向下滚动时,它们将再次重新充气,这就是文本消失的原因。我建议不使用ListView,而只是使用LinearLayout,然后以编程方式添加EditText视图。现在,如果你固执并且不想改变你当前的解决方案。当用户在字段中键入时,你可以做的基本上存储所有String值,然后在适配器中你可以设置为类型值。

例如......

String valueForEditTextOne = "";

//in the get view adapter you have to set a tag for each edittext to be able to identify them. 

String tagName = editTextOne.getTag();

   if (tagName == null) {
   //You need to set the tag name (use the passed position constructor)
   editTextOne.setTag(String.valueOf(position));
   } else {
     if(tagName.equals("1")) {
      textViewOne.setText(valueForEditTextOne);
     } else if (tagName.equals("2") {
      //etc...
     }
   } 

}

我不知道你有多少TextView,但这肯定有用。您还必须添加文本更改侦听器以更新该文本视图的每个手动字符串。

答案 2 :(得分:0)

已解决:

滚动列表视图时,所有 editText 项都设置为相同的值,如果处理程序代码像这样,则失去其实际值,

 txtQuantityVal.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            items.get(position).setStock(Double.parseDouble(s.toString()));
        }

因为,所有元素的 textChangeEventListener 都会发生变化。

避免这种情况的方法,将数据项对象添加到editText的Tag属性中,

  txtQuantityVal.setTag(item);
  txtQuantityVal.setText(String.valueOf(item.getStock()));

然后,我们在 TextChange 事件中保持控制, 这里我的数据的唯一键是 item.productId , 解决方案的逻辑是, 通过检查两个值,我们应该检查正确的元素是否正在改变。

 txtQuantityVal.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                    BarcodeItem bitem=items.get(position);
                    BarcodeItem xitem=((BarcodeItem)txtQuantityVal.getTag());
                    if(bitem.getProductId() == xitem.getProductId() ) { // match id, item change ?
                        items.get(position).setStock(Double.parseDouble(s.toString()));
                    }
                    else{
                        // data item ids different, item no changes
                        
                    }
            }