我正在做一个项目,我想在ListView中显示联系人名称列表。从本地sqli db中检索名称。到目前为止,我已设法检索名称并使用标准ArrayAdapter类显示它们。
但是为了更多的控制,我正在尝试创建自己的适配器,以便我也可以在每一行上显示控制按钮。我对这段代码感到困惑:
private void fillData() {
Cursor mContactsCursor = mDbAdapter.getAllContacts();
startManagingCursor(mContactsCursor);
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
//What variables should constructor take?
adapter = new ContactsListAdapter(this, from, to);
data.setAdapter(adapter);
}
基本上我不知道如何将这些值传递给构造函数,或者我是否应该这样做?
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
这是我的ContactsListAdapter类:
public class ContactsListAdapter extends ArrayAdapter<Contact> {
private List<Contact> contacts;
private Button deleteBtn;
private Button editBtn;
private TextView name;
public ContactsListAdapter(Context context,List<Contact> contacts) {
super(context, R.layout.contact_row, contacts);
this.contacts = contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contact_row, null);
}
//assign values to the view
final Contact c = this.contacts.get(position);
//add listeners to buttons
deleteBtn = (Button)v.findViewById(R.id.deleteBtn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
});
editBtn = (Button)v.findViewById(R.id.editBtn);
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Edit", Toast.LENGTH_SHORT).show();
}
});
//insert name into the text view
name = (TextView)v.findViewById(R.id.name);
name.setText(c.getName());
return v;
}
}
这个类的代码取自我使用自定义列表适配器的示例,该适配器从硬编码数组中获取数据,因此在从数据库获取数据时我可能遗漏了一些东西。
非常感谢任何建议。非常感谢。
答案 0 :(得分:7)
参考这个例子,
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
请告诉我,如果您仍然对如何将值传递给Custom ArrayListAdapter ...
感到困惑请从表中检索所有记录,如下所示,并将其添加到arrayList
contactList.add(contact);
代码片段
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
new CustomerAdapter(this,resid,contactList); //这就是您需要自定义适配器的方式 在您的自定义适配器
中公共类CustomAdapter扩展了ArrayAdapter {
Context context;
ArrayList<Tip> objects;
public CustomAdapter(Context applicationContext, int dovizLayout, ArrayList<Contact> ts) {
super(applicationContext, dovizLayout);
this.context = applicationContext;
this.objects = (ArrayList) ts;
}