我有一个按钮,每次按下时都会向ListView插入新行。
我的ListView是 editText的列表,当我将内容设置为添加的editText时,然后尝试添加新行时,以前的editText会丢失插入的数据。
这是我的EditText布局:
<EditText
android:id="@+id/myEditText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawablePadding="8dp"
android:background="@android:color/transparent"
android:layout_gravity="center_vertical"
android:hint="@string/ingresa_codigo_participacion"
android:textColorHint="@color/white"
android:paddingLeft="11dp"
android:paddingRight="11dp"
android:singleLine="true"
android:maxLines="1"
android:lines="1"
android:textCursorDrawable="@drawable/white_cursor_edittext"
/>
这是我的ListView适配器:
public class AddNewCodesAJEAdapter extends BaseAdapter {
private Context context;
private List<EditText> editTexts;
public AddNewCodesAJEAdapter(Context context){
this.context = context;
this.editTexts = new ArrayList<>();
}
public void refreshList(List<EditText> ets){
editTexts = ets;
notifyDataSetChanged();
}
@Override
public int getCount() {
return editTexts.size();
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return editTexts.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_edittext_aje, parent, false);
final EditText myEditText = convertView.findViewById(R.id.myEditText);
final ImageView imgClearEdt = convertView.findViewById(R.id.imgClearEditText);
imgClearEdt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(imgClearEdt.getTag().equals("0")){
myEditText.setText("");
myEditText.setEnabled(true);
}
}
});
myEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
myEditText.setTextColor(ContextCompat.getColor(context,R.color.white));
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>0){
imgClearEdt.setVisibility(View.VISIBLE);
}else{
imgClearEdt.setVisibility(View.INVISIBLE);
}
}
@Override
public void afterTextChanged(Editable et) {
if(s.length() == 7){
Toast.makeText(context, "calling web service", Toast.LENGTH_SHORT).show();
myEditText.setEnabled(false);
}
}
});
return convertView;
}
}
这是我的按钮,它将新的editText行添加到ListView:
@Override
public void onClick(View v) {
Toast.makeText(this, "Adding", Toast.LENGTH_SHORT).show();
editTexts.add(new EditText(EnterExperienceCodeActivity.this));
addNewCodesAJEAdapter.refreshList(EnterExperienceCodeActivity.this, editTexts);
}
我想我有这个问题,因为我正在调用 notifyDataSetChanged ,但我想保留插入到每个editText中的数据。