1

我正在做一个项目,我想在 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;

}

}

此类的代码取自一个示例,其中我使用了一个自定义列表适配器,该适配器从硬编码数组中获取数据,因此在从数据库中获取数据时我可能遗漏了一些东西。

非常感谢任何建议。非常感谢。

4

1 回答 1

6

参考这个例子,

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

如果您仍然对如何将值传递给您的自定义 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;
    }

新的 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;
}
于 2013-01-14T21:53:10.243 回答