我在整个数组列表中复制了最后一个元素。 这里MyContact包含一个ID(int),name(String),Number(String,primary key)。
例如。我添加了name = John,number = 99999并提交。 和name = Jonny,number = 99998并提交。
在DB中我可以正确地看到它们(条目是正确的)
现在我希望他们在listview中看到像
-------------
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;
}
答案 0 :(得分:1)
您的MyContact
课程看起来是一个单身人士(例如,getInstance()
方法):
MyContact contact = MyContact.getInstance();
...
contactList.add(contact); // Always the same instance--sad panda.
列表中的每个引用都引用同一个实例,其中包含数据库中最后一项的值。您需要为每个列表项创建一个新实例。