0

我正在整个数组列表中复制最后一个元素。这里 MyContact 包含一个 ID(int)、name(String)、Number(String, primary key)。

例如。我添加了 name = John,number = 99999 并提交。name = Jonny, number = 99998 并提交。

在数据库中我可以正确地看到它们(条目是正确的)

现在我希望他们在列表视图中看到

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

但实际结果是

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

来自 DataBaseHelper.java 的代码

// 获取所有联系人

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;
}

从下面我调用 getAllContactMethod。公共类 ContactView 扩展 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");
}
}

适配器如下

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;
    }
4

1 回答 1

1

你的MyContact类看起来是一个单例(例如,getInstance()方法):

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

列表中的每个引用都指向同一个实例,其中包含数据库中最后一项的值。您需要为每个列表项创建一个新实例。

于 2012-12-15T16:17:40.240 回答