gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:55:10 24 4
gpt4 key购买 nike

我一直在努力寻找解决我的问题的方法,但没有一个对我有用。 :/我在每个列表项中都有一个带有编辑文本的 ListView ,当向下滚动并再次向上滚动时,已编辑编辑文本的内容已丢失或其他编辑文本已采用他的值。

请帮我一些

主要 Activity :-

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

最佳答案

您需要将数据存储在 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);
}
});

关于android - ListView 中的 EditText 在滚动时丢失内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com