gpt4 book ai didi

android - ListView 中的 EditText 在 ListView 滚动时丢失内容

转载 作者:行者123 更新时间:2023-11-29 00:15:44 24 4
gpt4 key购买 nike

我正在开发一款用户可以填写报告和投票的应用程序。在一个 Activity 中,我有一个 ListView,其中子项包含 EditText、Checkbox 和 TextView。

EditTexts 和 Checkbox 的问题在于滚动时,它们所拥有的内容会丢失。我能够通过将检查状态保存在数组中来解决复选框问题,但我无法解决 EditTexts 的问题。我知道有一些关于这个特定问题的问题,但他们提供的解决方案似乎不起作用。所以这是我的适配器:

public class DiveProfileAdapter extends ArrayAdapter<DiveProfile> {

private Context mContext;
private List<DiveProfile> mTasks;
ViewHolder holder;

private ArrayList<Boolean> itemChecked = new ArrayList<>(); //for saving checkbox values

public DiveProfileAdapter(Context context, List<DiveProfile> objects) {
super(context, R.layout.row_dive_profile_divers_list_item, objects);
this.mContext = context;
this.mTasks = objects;

for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
}

public View getView(final int position, View convertView, ViewGroup parent) {

if (convertView == null) {
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.row_dive_profile_divers_list_item, null);

holder = new ViewHolder();
holder.diverAttended = (CheckBox) convertView.findViewById(R.id.diver_attended_checkbox);
holder.diveTime = (EditText) convertView.findViewById(R.id.dive_time_edit_text);
holder.depth = (EditText) convertView.findViewById(R.id.depth_edit_text);

convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}

final DiveProfile diverObjectTask = mTasks.get(position);

holder.diverAttended.setChecked(diverObjectTask.isDiverAttended());
holder.diveTime.setText(diverObjectTask.getDiveTime());

holder.diverAttended.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

CheckBox cb = (CheckBox) v.findViewById(R.id.diver_attended_checkbox);

if (cb.isChecked()) {
itemChecked.set(position, true);
// do some operations here
} else if (!cb.isChecked()) {
itemChecked.set(position, false);
// do some operations here
}
}
});
holder.diverAttended.setChecked(itemChecked.get(position)); // this will Check or Uncheck the

holder.diveTime.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String txt = setDiveTimeDialog();
holder.diveTime.setText(txt);
mTasks.get(position).setCurrentText(txt); //add the setter to the current item's position
}
});
holder.diveTime.setText(diverObjectTask.getCurrentText());

return convertView;
}

static class ViewHolder {
CheckBox diverAttended;
EditText diveTime;
EditText depth;
}

private String setDiveTimeDialog() {

//final String[] items = finItems;
final String[] items = getContext().getResources().getStringArray(R.array.certification_level_items);

// Create list dialog with required parameters - context, title, and our array of items to fill the list.
CustomListDialog.Builder builder = new CustomListDialog.Builder(getContext(), "Dive Time", items);

// Now again we can use some extra methods on the builder to customise it more.
builder.titleAlignment(BaseDialog.Alignment.LEFT);
builder.itemAlignment(BaseDialog.Alignment.LEFT);
builder.titleColor(getContext().getResources().getColor(R.color.primary_color));


// Now we can build our dialog.
CustomListDialog customListDialog = builder.build();

// Finally we can show it.
customListDialog.show();

customListDialog.setListClickListener(new CustomListDialog.ListClickListener() {
@Override
public void onListItemSelected(int i, String[] strings, String s) {

result = s;

}
});

return result;
}
}

最佳答案

你应该这样做:

final DiveProfile diverObjectTask = mTasks.get(position);
holder.diveTime.setText(diverObjectTask.getDiveTie());
//also set your CheckBox value

这些值变为空,因为您没有在 getView 中设置它(getView 列表滚动时自动调用)

更新

看看你的问题,我认为最简单的方法是在 DiveProfile 中创建新变量类,例如 boolean isSelectedString currentText .

在那之后,就不需要使用像itemChecked这样的单独列表了。 .你可以getset当前项目的值。

使用 getter 的示例:

holder.diverAttended.setChecked(mTasks.get(position).getSelected()); // getSelected is isSelected getter method

然后调用setSelected ( setterisSelected )在你的 holder.diverAttendedonClick

您的 EditText 示例setter :

    holder.diveTime.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

String txt = setDiveTimeDialog();
holder.diveTime.setText(txt);
mTasks.get(position).setcurrentText(txt); //add the setter to the current item's position
}
});

然后调用getCurrentText()getView

关于android - ListView 中的 EditText 在 ListView 滚动时丢失内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27017196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com