编辑:我认为该帖子正在变得混乱所有这些编辑。我将尝试从一开始就解释这个问题。
我有一个带有ListView的活动和一个添加新项目的按钮。 列表视图中的每个项目都由EditText和两个按钮进行竞争。 问题是如果我添加一个新项目,那么EditTexts中的文本将恢复到它们的初始值......
我正在添加新项目:
adapter.add(new Contacto(""));
adapter.notifyDataSetChanged();
“Contacto”是我在arrayList中使用的类的名称。
@Iftikar Urrhman Khan建议我使用addTextChangedListener,但我不知道如何。
这是我的惯例的代码:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class ContactsArrayAdapter extends ArrayAdapter<Contacto>{
private ArrayList<Contacto> contactos;
private Activity activity;
public ContactsArrayAdapter(Activity a, int textViewResourceId, ArrayList<Contacto> contactos) {
super(a, textViewResourceId, contactos);
this.contactos = contactos;
this.activity = a;
}
public static class ViewHolder{
public EditText mail;
public Button btn_contactos;
public Button btn_delete;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
final Contacto item = contactos.get(position);
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contacto, null);
holder = new ViewHolder();
holder.mail = (EditText) v.findViewById(R.id.txt_to);
holder.btn_contactos = (Button) v.findViewById(R.id.btn_contacts);
holder.btn_delete = (Button) v.findViewById(R.id.btn_delete);
v.setTag(holder);
//Delete;
holder.btn_delete.setTag(position);
holder.btn_delete.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
int posicion = (Integer)v.getTag();
contactos.remove(posicion);
notifyDataSetChanged();
}
});
//Add contacts
holder.btn_contactos.setTag(position);
holder.btn_contactos.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//Log.d("TAG", "Botón contactos pulsado, pos: "+Integer.toString(pos));
((Contactos)getContext()).setPosicion((Integer)v.getTag());
Intent intent =new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Email.CONTENT_URI);
((Activity) v.getContext()).startActivityForResult(intent, MainActivity.act_pick_contact_mail);
}
});
//TextEdit Listener
holder.mail.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
//holder.mail.setText(item.mail);
//item.mail=arg0.toString();
//int posi = (Integer)v.getTag();
//contactos.set(posi, new Contacto(arg0.toString()));
//contactos.get(pos).setMail(arg0.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
});
}
else holder=(ViewHolder)v.getTag();
holder.mail.setTag(position);
if (item != null) {
holder.mail.setText(item.getMail());
}
return v;
}
}
答案 0 :(得分:1)
问题出现在您的代码中,当editText值更改为未在arraylist contactos
中更新时。
当您向数组列表中再添加一个项目时,它具有相同的旧值。
您可以将addTextChangedListener
添加到您的编辑文字
holder.mail.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}});