Jav*_*bra 1 sqlite android android-arrayadapter custom-adapter
我正在做一个项目,我想在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);
}
Run Code Online (Sandbox Code Playgroud)
基本上我不知道如何将这些值传递给构造函数或者我是否应该这样做?
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
Run Code Online (Sandbox Code Playgroud)
这是我的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;
}
Run Code Online (Sandbox Code Playgroud)
}
这个类的代码取自我使用自定义列表适配器的示例,该适配器从硬编码数组中获取数据,因此在从数据库获取数据时我可能遗漏了一些东西.
任何建议都非常感谢.非常感谢.
参考这个例子,
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
让我知道,如果你仍然困惑如何将值传递给Custom ArrayListAdapter ...
请从表中检索所有记录,如下所示,并将其添加到arrayList
contactList.add(contact);
Run Code Online (Sandbox Code Playgroud)
代码片段,
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;
}
Run Code Online (Sandbox Code Playgroud)
new CustomerAdapter(this,resid,contactList); //这是您在自定义适配器中需要自定义适配器的方式
public class CustomAdapter extends 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12160 次 |
| 最近记录: |