Android Studio:如何使ListView中的EditText保留更改后的值?
创始人
2024-08-18 20:00:14
0

要在Android Studio中使ListView中的EditText保留更改后的值,可以通过以下步骤和代码示例来实现:

  1. 首先,在ListView的适配器中添加一个HashMap来存储每个EditText的值和位置。在适配器的构造函数中初始化该HashMap。
public class CustomAdapter extends BaseAdapter {
    private Context context;
    private ArrayList dataList;
    private HashMap editTextValues;

    public CustomAdapter(Context context, ArrayList dataList) {
        this.context = context;
        this.dataList = dataList;
        this.editTextValues = new HashMap<>();
    }
    // ...
}
  1. 在getView()方法中为每个EditText设置TextWatcher,并将其值保存到HashMap中。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // ...
    final EditText editText = convertView.findViewById(R.id.editText);
    editText.setText(dataList.get(position));

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            editTextValues.put(position, charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    // ...
}
  1. 在适配器中添加一个方法来获取HashMap中指定位置的EditText的值。
public String getEditTextValue(int position) {
    return editTextValues.get(position);
}
  1. 在Activity或Fragment中,创建适配器实例并设置给ListView。
CustomAdapter adapter = new CustomAdapter(this, dataList);
listView.setAdapter(adapter);
  1. 当需要获取ListView中EditText的值时,使用适配器的getEditTextValue()方法。
String editTextValue = adapter.getEditTextValue(position);

这样,每当用户更改EditText的值时,都会将其保存在HashMap中,并且可以通过适配器的方法获取到这些值。

相关内容

热门资讯

了解透视!wepokerplu... 了解透视!wepokerplus万能挂,微乐自建房辅助工具2025在哪,积累教程(存在有挂)-哔哩哔...
分享透视!拱趴大菠萝机器人,微... 分享透视!拱趴大菠萝机器人,微乐家乡自建房辅助app,练习教程(有挂总结)-哔哩哔哩1、任何拱趴大菠...
有挂透视!大菠萝789辅助器下... 有挂透视!大菠萝789辅助器下载,微乐小程序插件免费,讲义教程(有挂透明挂)-哔哩哔哩1、许多玩家不...
总结透视!werplan辅助软... 总结透视!werplan辅助软件,微乐小程序有脚本吗,机巧教程(有挂总结)-哔哩哔哩1、上手简单,内...
开挂透视!wpk刷入池率脚本,... 开挂透视!wpk刷入池率脚本,微信小程序微乐辅助器免费版v2.0,模板教程(有挂透明挂)-哔哩哔哩1...
有挂透视!aapoker公共底... 有挂透视!aapoker公共底牌,微乐智能辅助软件是真的吗,策略教程(有挂分析)-哔哩哔哩所有人都在...
有挂透视!hhpoker软件可... 有挂透视!hhpoker软件可以玩吗,微信小程序微乐为什么老输,手筋教程(真的有挂)-哔哩哔哩1、每...
科普透视!wepoker有没有... 科普透视!wepoker有没有辅助,微乐自建房辅助免费入口,模块教程(有挂解密)-哔哩哔哩1、每一步...
分享透视!大菠萝手游辅助,收i... 分享透视!大菠萝手游辅助,收i就微乐小程序游戏破解器,手段教程(有人有挂)-哔哩哔哩1、大菠萝手游辅...
推荐透视!hh poker辅助... 推荐透视!hh poker辅助有用吗,微信小程序怎么开挂,妙招教程(有挂攻略)-哔哩哔哩1、任何hh...