Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:28:07+00:00 2026-06-16T01:28:07+00:00

I am getting the last element copied in the whole array list. Here MyContact

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:28:09+00:00Added an answer on June 16, 2026 at 1:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this error while trying to erase the last element of a list. I
I wanted to check my last element's array lenght is 6 or not.In here
My last question was about getting the string representation of an object serialized to
[EDIT] original title of this question was Getting the last element of a Postgres
I am trying to detect whether this is the last element of a list.
I am crashing at runtime when i pop last element from NSMuttable array.I am
I am getting the last element from a form which has a value like
I have seen this question that talks about getting the last part of a
I am trying to replicate a forum function by getting the last reply of
Was getting deprecation warning message in the last version. Now I can't run the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.