0

EDIT: I think the post is becoming into a mess with all these edits. I' going to try to explain the problem from the beginning.

I have an activity with a ListView and a button to add new items. Each item in the listview is compesed by an EditText and two buttons. The problem is that if I add a new item the texts winthin the EditTexts are restored to their initial values...

I'm adding the new items with:

adapter.add(new Contacto(""));
adapter.notifyDataSetChanged();

"Contacto" is the name of the class I'm using in my arrayList.

@Iftikar Urrhman Khan suggested to me to use the addTextChangedListener but I don't know how.

This is the code of my customadapter:

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;
    }
}
8
  • 1
    you have to save your textviews values,because notifydatasetchange call get view again for every visible row May 22, 2013 at 15:49
  • what do you mean with save your textviews? Do I need to update my arrayList with the texts before to do the adapter.notifyDataSetChanged() and restore the texts after that ?
    – Richal
    May 22, 2013 at 15:59
  • I've debugged the code and I've seen the content of my arrayList and is not updated with the values of the screen. How can I get the reference to the textviews of each item from my activity to obtain their texts and update the array?
    – Richal
    May 22, 2013 at 16:08
  • you have to save your updated values May 22, 2013 at 16:19
  • can you post your code that will help May 22, 2013 at 16:19

1 Answer 1

1

The problem is in your code is when editText values changed its not updated in arraylist contactos.

when you add one more item to your array list it has the same older value in it.

you can add addTextChangedListener to your edit text

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) {

}});
4
  • Ok, I'v created the listener (i've changed tha name of the last method by onTextChanged). I've put a breakpoint in method afterTextChanged and is triggered a lot of times. The problem is that I don't know how to update the arrayList without knowning the index of the item I need to modify. Do you know how to obtain this index from this method? Thanks.
    – Richal
    May 23, 2013 at 8:51
  • @Richal you will write this in your adapter and in your adpater you have access to your position May 23, 2013 at 8:53
  • I've tried with this code: contactos.set(posi, new Contacto(arg0.toString())); _____ posi is an attribute I'm updating at the beginning of he method getView with the parameter position. ______ But doesn't work fine.
    – Richal
    May 23, 2013 at 10:32
  • @Richal can you post your contactos class as you have to update ,but in code you are setting new contacto May 23, 2013 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.