user1707035
user1707035

Reputation:

last element is over-written in whole arraylist?

I am getting the last element copied in the whole array list. Here MyContact contains an ID(int), name(String), Number(String, primary key).

eg. i added name = John, number = 99999 and submit it. and name = Jonny, number = 99998 and submit it.

in DB i can see them properly(entries are correct)

now i want them to see in listview like

-------------
  John 
  99999
-------------
  Jonny
  99998
-------------

but actual result is

-------------
  Jonny
  99998
-------------
  Jonny
  99998
-------------

code from DataBaseHelper.java

// Getting All Contacts

public List<MyContact> getAllContacts() {
        Log.d(ContactManagerUtil.TAG_ENTER, ContactManagerUtil.getInstance()
                .getMehtodName());
    List<MyContact> contactList = new ArrayList<MyContact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        MyContact contact = MyContact.getInstance();
        if (cursor.getString(0) != null)
            contact.setId(Integer.parseInt(cursor.getString(0)));
        contact.setName(cursor.getString(1));
        contact.setNumber(cursor.getString(2));
        // Adding contact to list
        contactList.add(contact);
        contact = null;
        cursor.moveToNext();
    }

    Log.d(ContactManagerUtil.TAG_EXIT, contactList.toString());
    Log.d(ContactManagerUtil.TAG_EXIT, ContactManagerUtil.getInstance()
            .getMehtodName());
    cursor.close();
    db.close();

    // return contact list
    return contactList;
}

from below i am calling the getAllContactMethod. public class ContactView extends ListActivity {

DataBaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("@gaurav","IN ContactView.onCreate");
    db = new DataBaseHelper(getApplicationContext());
    ArrayAdapter<MyContact> adapter = new MyContactAdapter(this,
            db.getAllContacts());
    setListAdapter(adapter);
    Log.d("@gaurav","OUT ContactView.onCreate");
}
}

Adapter is as follows

public class MyContactAdapter extends ArrayAdapter<MyContact> {

    public MyContactAdapter(Activity context, List<MyContact> obj) {
        super(context, R.layout.layout_contact_view, obj);
        contactlist = obj;
        this.context = context;
    }

    private List<MyContact> contactlist;
    private Activity context;

    static class ViewHolder {
        protected QuickContactBadge contactbadge;
        protected TextView contactname;
        protected TextView contactnumber;
    }

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

        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.layout_contact_view, null);
            viewHolder = new ViewHolder();
            viewHolder.contactname = (TextView) convertView
                    .findViewById(R.id.contactname);
            viewHolder.contactnumber = (TextView) convertView
                    .findViewById(R.id.contactnumber);
            viewHolder.contactbadge = (QuickContactBadge) convertView.findViewById(R.id.contactBadge);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(contactlist.get(position) == null){
            Log.d(ContactManagerUtil.TAG_ENTER, "In adapter getView, found: contact null");
            return null;
        }
        viewHolder.contactname.setText(contactlist.get(position).getName());
        viewHolder.contactnumber.setText(contactlist.get(position).getNumber());
        viewHolder.contactbadge.setFocusable(true);
        return convertView;
    }
}

MyContact.java

public static MyContact getInstance() {
        if (contact == null) {
            contact = new MyContact();
        }
        return contact;
    }

Upvotes: 0

Views: 144

Answers (1)

Dave Newton
Dave Newton

Reputation: 160291

Your MyContact class looks to be a singleton (e.g., the getInstance() method):

MyContact contact = MyContact.getInstance();
...
contactList.add(contact); // Always the same instance--sad panda.

Every reference in the list refers to the same instance, which contains the values of the last item from the DB. You need to create a new instance for each list item.

Upvotes: 1

Related Questions