自ArrayAdapter从本地的SQLite数据库数据数据库、数据、ArrayAdapter、SQLite

2023-09-06 11:16:13 作者:我早已无所谓

我做的一个项目,我想在一个ListView显示联系人的名单。名称是从本地SQLI数据库检索。到目前为止,我已成功地检索名称和使用标准ArrayAdapter类显示出来。

I am doing a project where I want to display a list of contacts names in a ListView. The names are retrieved from the local sqli db. So far I have managed to retrieve the names and display them using the standard ArrayAdapter class.

不过,对于更多的控制我试图创建自己的适配器,让我还对每行显示控制按钮。我感到困惑这块code的:

However for more control I am trying to create my own adapter to allow me to also display control buttons on each row. I am confused about this piece of code:

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

}

基本上我不知道如何把这些价值传递给构造函数,或者如果我甚至应该这样做?

Basically I don't know how to pass these values to the constructor or if I should even do that??

String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME,   ContactsDbAdapter.COLUMN_LNAME};
    int [] to = new int [] { R.id.fname, R.id.lname};

这是我的ContactsListAdapter类:

This is my ContactsListAdapter class:

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;

}

}

这个类的code的从我使用的是从硬codeD阵列获取数据,所以我可能我失去了一些东西,当它从数据库来获取数据的自定义列表适配器为例采取

The code for this class was taken from an example where I used a custom list adapter which was getting data from a hard coded array so I probably am missing something when it comes to getting data from the db.

任何意见是非常AP preciated。非常感谢。

Any advice is much appreciated. Many thanks.

推荐答案

请参考这个例子中,

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

让我知道,如果你还在困惑如何将值传递给您的自定义ArrayListAdapter ...

let me know, if you are still confused how to pass values to your Custom ArrayListAdapter...

请从你的表如下检索所有记录,并将其添加到一个ArrayList中

Please retrieve all the records from your table as below and add it to a arrayList

contactList.add(contact);

code的片断,

snippet of code,

  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(这一点,渣油,contactList); //你这是怎么需要自定义适配器在您的自定义适配器

new CustomerAdapter (this, resid, contactList); // this is how you need to the Custom Adapter In your custom Adapter

公共类CustomAdapter扩展ArrayAdapter {

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