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

相关内容

热门资讯

玩家必备攻略!wepoker有... 玩家必备攻略!wepoker有辅助插件吗,xpoker怎么作弊,透明教程(有挂攻略)是由北京得wep...
揭秘关于!wpk插件,we p... 揭秘关于!wpk插件,we poker插件,扑克教程(有挂透明)是一款可以让一直输的玩家,快速成为一...
必看攻略!大菠萝免费辅助器,a... 必看攻略!大菠萝免费辅助器,aapoker透视脚本下载,2025新版技巧(有挂软件);玩家必备必赢加...
科技揭秘!wepoker透视破... 1、科技揭秘!wepoker透视破解版,wepoker有没有挂,普及教程(有挂软件)。2、wepok...
实测必看!hhpoker底牌透... 实测必看!hhpoker底牌透视脚本,wepoker怎么开辅助,wpk教程(有挂透明);1.hhpo...
科普常识!wejoker辅助器... 科普常识!wejoker辅助器要钱玩吗,红龙poker辅助平台,可靠教程(有挂技巧)准备好在wejo...
技术分享!约局吧德州真的有透视... 1、技术分享!约局吧德州真的有透视挂吗,aapoker怎么设置抽水,必赢方法(有挂辅助);详细教程。...
实操分享!aa poker辅助... 此外,数据分析德州(来玩app破解)辅助神器app还具备辅助透视行为开挂功能,通过对客户来玩app破...
一分钟秒懂!wepoker俱乐... 一分钟秒懂!wepoker俱乐部辅助器,德扑圈有透视吗,我来教教你(有挂软件)1、点击下载安装,微扑...
科普常识!hhpoker可以开... 科普常识!hhpoker可以开挂吗,wepoker透视苹果系统,专业教程(有挂透视);玩家必备必赢加...