You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android动态添加列表项时输入数据丢失问题求助

Fix: Skill Inputs Getting Cleared When Adding New Items

Hey there! Let's tackle that frustrating issue where your existing skill entries get wiped every time you tap the "add skill" button. This is a common pitfall with ListView and dynamic input fields—here's exactly how to fix it:

Why This Happens

The root cause is that you're not persisting the existing input data before adding a new item. ListView reuses views, and without a dedicated data source to store all entered skills, old inputs get lost when the list refreshes.

Step-by-Step Solution

1. Maintain a Global Data Source

First, create an ArrayList to store every skill entry. This will be the single source of truth for all user inputs:

// Define this at the activity/fragment level
private ArrayList<String> skillEntries = new ArrayList<>();
private SkillCustomAdapter skillAdapter;

2. Initialize with a Blank Entry

When setting up your ListView, start with one blank entry to let the user input their first skill:

// In onCreate() or onViewCreated()
skillEntries.add(""); // Initial blank input
skillAdapter = new SkillCustomAdapter(this, skillEntries);
yourListView.setAdapter(skillAdapter);

3. Update the Add Button Logic

Modify your button's click listener to first save all existing inputs, then add a new blank entry:

addSkillButton.setOnClickListener(v -> {
    // 1. Save all current inputs to the data source
    for (int i = 0; i < yourListView.getChildCount(); i++) {
        View listItem = yourListView.getChildAt(i);
        EditText skillInput = listItem.findViewById(R.id.your_skill_edittext_id);
        if (i < skillEntries.size()) {
            skillEntries.set(i, skillInput.getText().toString().trim());
        }
    }

    // 2. Add a new blank entry for the next skill
    skillEntries.add("");

    // 3. Refresh the ListView without losing data
    skillAdapter.notifyDataSetChanged();
});

4. Optimize Your Custom Adapter

Update your adapter's getView() method to bind data from the source and sync input changes in real-time:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.your_skill_item_xml, parent, false);
        holder = new ViewHolder();
        holder.skillEditText = convertView.findViewById(R.id.your_skill_edittext_id);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Set the saved skill text to the input field
    holder.skillEditText.setText(skillEntries.get(position));

    // Sync input changes back to the data source immediately
    holder.skillEditText.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) {
            skillEntries.set(position, s.toString().trim());
        }

        @Override
        public void afterTextChanged(Editable s) {}
    });

    return convertView;
}

// ViewHolder to optimize view reuse
static class ViewHolder {
    EditText skillEditText;
}

How This Works

  • The skillEntries list keeps track of every input, so nothing gets lost when the ListView refreshes.
  • We save existing inputs before adding a new entry, ensuring old data is preserved.
  • The TextWatcher updates the data source as the user types, so even edits to existing entries stay saved.

内容的提问来源于stack exchange,提问作者Christian Wolf Alves Hess

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:17:23