简体   繁体   中英

How can I extract data from my database with a Cursor for use in a ListView, but not have to display that data within a column in the ListView?

I am populating a ListView with a cursor adapter just like as described HERE :

DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
Cursor cursor = databaseHelper.getPeople();
String[] fromColumns = {"_id","firstname","lastname","email"};
int[] toViews = {R.id.count, R.id.firstname, R.id.lastname, R.id.email};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.person_name_and_email, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);

I also have another column in my database table called "status" which is whether the person is "friend", "family", "business associate", etc.

I am going to use the content of this column to determine the color of the person row; if the person is a friend, the row will be green, if the person is family, the row will be blue, etc.

So, I do not want the "status" data to show up in the ListView as a column, but I need it for each row I iterate through.

Is there a way to do this?

Thanks!

Try Overriding the getView() method

SimpleCursorAdapter notes = new SimpleCursorAdapter(this, layout,     cursor, fromColumns, toViews, 0);
{
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        cursor.moveToPosition(position);
        String s = cursor.getString(cursor.getColumnIndex(“status"));
        final View row = super.getView(position, convertView, parent);

        if (s.equalsIgnoreCase(“friend"))
            row.setBackgroundColor();

        return row;
    }
};       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM