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

相关内容

热门资讯

wepoke辅助机器人!wop... wepoke辅助机器人!wopoker辅助(Wepoke规律)本来真的是有挂(详细黑科技教程);1分...
wepok软件透明挂!wepo... wepok软件透明挂!wepoke脚本(德州版Wepoke)确实真的是有挂(详细计算辅助教程)1、让...
aapoker猫腻!aapok... aapoker猫腻!aapoker安卓怎么下载,(aapoker辅助)果然是有挂(详细透视辅助教程)...
wepoke计算辅助!wepo... wepoke计算辅助!wepower有辅助器吗(weopke真的有挂)果然是真的有挂(详细辅助德之星...
德扑之星猫腻!德扑软件决策,德... 德扑之星猫腻!德扑软件决策,德扑之星决策总是真的有挂(详细辅助教程);德扑之星猫腻黑科技是一款具有i...
微扑克ai机器人!德州微扑克辅... 微扑克ai机器人!德州微扑克辅助透视,(微扑克开发)真是真的有挂(详细ai辅助工具教程);亲真的是有...
微扑克游戏辅助器!微扑克真的有... 1、微扑克游戏辅助器!微扑克真的有挂吗,(微扑克微乐)一直是真的有挂(详细全自动机器人教程)。2、微...
aapoker透明挂!aapo... aapoker透明挂!aapoker辅助软件,(aapoker俱乐部后台)一贯有挂(详细透视辅助教程...
德扑ai助手!德扑之星电脑版,... 《德扑ai助手软件透明挂》是一款多人竞技的德扑ai助手辅助透视游戏,你将微扑克对手来到同一个战场,为...
微扑克全自动机器人!微扑克系统... 微扑克全自动机器人!微扑克系统是不是有问题,(微扑克代打)一直真的是有挂(详细wpk透视辅助教程);...