【问题标题】:Custom ArrayAdapter with data from SQLite local db使用来自 SQLite 本地数据库的数据的自定义 ArrayAdapter
【发布时间】:2013-01-14 21:30:44
【问题描述】:

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

}

}

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

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

【问题讨论】:

  • 您可以为您的类创建自己的构造函数,并将其作为参数。更好的是,你为什么不把它作为你的 Contact 类的一个属性呢?
  • 所以在 Contact 类中是这样的:private ContactsListAdapter adapter = new ContactsListAdapter()?

标签: android sqlite android-arrayadapter custom-adapter


【解决方案1】:

参考这个例子,

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

如果您仍然对如何将值传递给自定义 ArrayListAdapter 感到困惑,请告诉我...

请按如下方式从您的表中检索所有记录并将其添加到数组列表中

contactList.add(contact);

sn-p 代码,

  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;
}

【讨论】:

  • 我真的看过这个教程。我了解示例中发生的情况。但是我对 sqli 感到困惑,因为我不知道如何将适配器链接到数据。另外我似乎没有一个列表来存储我的数据,这让我更加困惑!
  • 感谢您的帮助。我现在意识到自己的错误了。
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
相关资源
最近更新 更多