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中,并且可以通过适配器的方法获取到这些值。

相关内容

热门资讯

透视科技!红龙poker辅助工... 透视科技!红龙poker辅助工具(透视)竟然真的有挂(详细辅助可靠教程)1、红龙poker辅助透视辅...
透视ai代打!aapoker辅... 透视ai代打!aapoker辅助插件工具(透视)原来真的是有挂(详细辅助必备教程)1)aapoker...
透视模拟器!wepoker有人... 透视模拟器!wepoker有人用过吗(透视)真是有挂(详细辅助2025新版教程)1、每一步都需要思考...
透视计算!pokermaste... 透视计算!pokermaster破解版(透视)一直是有挂(详细辅助必胜教程)1、玩家可以在软件透明挂...
透视教程!wepoker破解器... 透视教程!wepoker破解器激活码(透视)一直真的是有挂(详细辅助攻略教程)1)辅助挂:进一步探索...
透视ai代打!wepoker买... 透视ai代打!wepoker买钻石有用吗(透视)切实是有挂(详细辅助2025新版)1、起透看视 透明...
透视代打!wepoker透视脚... 透视代打!wepoker透视脚本免费下载(透视)果然是真的有挂(详细辅助必胜教程);1、wepoke...
透视攻略!hhpoker的辅助... 透视攻略!hhpoker的辅助是真的吗(透视)其实是有挂(详细辅助新版2025教程)1、完成透视辅助...
透视攻略!wepoker透视脚... 透视攻略!wepoker透视脚本免费下载pc(透视)竟然存在有挂(详细辅助教你攻略);1)wepok...
透视插件!聚星ai辅助工具下载... 透视插件!聚星ai辅助工具下载(透视)原来真的有挂(详细辅助揭秘教程);辅助器中分为三种模型:软件透...