我正在整个数组列表中复制最后一个元素。这里 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;
}