views:

84

answers:

2

I have a custom CursorAdapter that is taking items from a database and displaying them in a listview. If possible, I would like to display only certain elements based on some boolean value within a database element. Here is something similar to what I would like to do:

package itp.uts.program;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
//Adapter tests if an element is read or unread, and bolds the items that are unread
public class BoldAdapter extends CursorAdapter{

private final LayoutInflater mInflater;
public BoldAdapter(Context context, Cursor c, boolean autoRequery) {
    super(context, c, autoRequery);
    mInflater = LayoutInflater.from(context);
}
public BoldAdapter(Context context, Cursor c) {
    super(context, c);
    mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {

}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    //Creates a view to display the items in
    final View view = mInflater.inflate(R.layout.notes_row, parent, false);
    TextView textRequestNo = (TextView) view.findViewById(R.id.text1);
    TextView textMessage = (TextView) view.findViewById(R.id.text2);
    StringBuilder requestNo = new StringBuilder(cursor.getString(cursor
            .getColumnIndex("requestNo")));
    StringBuilder message = new StringBuilder(cursor.getString(cursor
            .getColumnIndex("Message")));

    //Sets the text fields as elements from the database
    textRequestNo.setText(requestNo);
    textMessage.setText(message);

    if (cursor.getString(cursor.getColumnIndex("Read")).equals("true"))
    {//Tests if the item is unread
        **//DO NOT SHOW THE ELEMENT, HOW DO I DO THIS?**
    }

    return view;
}

}

+1  A: 

just return a View and set it's visibility property to VIEW.GONE

Pentium10
This makes the actual element disappear, but leaves an empty space where the item would be in the list.
mbauer14
+1  A: 

Ideally, you just filter out the rows you don't want in your database query.

If, for whatever reason, that is not possible, create a wrapping adapter that filters out the rows you do not want. Here is an AdapterWrapper base class you can use to help.

CommonsWare
Didn't even think of this, thanks. What exactly would I query for though. I have never really dealt with database querying before, and only know that I would change some parameter in this call: return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_BODY1, KEY_BODY2, KEY_BODY3, KEY_BODY4, KEY_BODY5}, null, null, null, null, null);
mbauer14
@mbauer14: That's really impossible to answer, since I did not create your database schema. You really need to spend some time learning SQL before creating Android apps that use SQLite. Sorry!
CommonsWare
@Commonsware: Yeah I definitely do, I have just been trying to learn as I go, but with databases, it seems like this just doesn't work. Say I want to return as a cursor all elements where KEY_BODY3 is false, is this a simple operation or do I just need to read up more? Thanks
mbauer14
@mbauer14: There is no `false` in SQLite, since there is no boolean datatype. Try making your third parameter to `query()` to be `KEY_BODY3+"=0"`, if that column is an integer and `0` represents false. But I really really recommend that you spend some time learning SQL before creating Android apps that use SQLite. Sorry!
CommonsWare
Nevermind, just needed to use the WHERE call for selection. I have been using Strings for the true and false fields, I should have mentioned that, but I got what I needed using : return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_BODY1, KEY_BODY2, KEY_BODY3, KEY_BODY4, KEY_BODY5}, "Read= 'false' ", null, null, null, null);
mbauer14