0

I've got a ListView that displays some results from the DB through a CursorAdapter...Title, text and a flag to indicate if the user has already viewed that item (by taking them to another screen)... Basically if the DB "visited" value is 0 it needs to display a small "new" icon next to the item...If not it shouldn't show any icon...

The problem is that when it loads the list it assigns the flag seemingly at random...The visited ones don't have the flag, but some unvisited ones miss the flag too...

I've tried moving the "visited" check between BindView and NewView but I get the same results...

My adapter:

public class newsAdapter extends CursorAdapter{

public newsAdapter(Context context, Cursor cursor){

    super(context, cursor);

}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView title = (TextView) view.findViewById(R.id.newsItemTitle);
    TextView text = (TextView) view.findViewById(R.id.newsItemText);

    ImageView newflag = (ImageView) view.findViewById(R.id.newsNewFlag);
    if(cursor.getInt(cursor.getColumnIndex("visited")) == 0){
        Log.i("extra","Not visited yet");
        newflag.setImageResource(R.drawable.icon_new);
    }

    title.setText(cursor.getString(cursor.getColumnIndex("title")));
    text.setText(cursor.getString(cursor.getColumnIndex("text"))); iv);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.news_item, parent, false);

    return view;
}


}

I'm sure the data is correct in the DB... Thanks in advance!

user1504495
  • 666
  • 8
  • 17

1 Answers1

0

Try this,

add an else block to your following code and in it either set the imageview to invisible/gone. You could also set the imageresource to null but why bother?

if(cursor.getInt(cursor.getColumnIndex("visited")) == 0){
        Log.i("extra","Not visited yet");
        newflag.setImageResource(R.drawable.icon_new);
    }

However, setting it to gone might effect your layout. So handle that if u plan to use GONE.

The other thing you can try is to implement getView() instead of newView() and implement the ViewHolder pattern. It makes it more efficient and to my knowledge less error prone.

Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • have u overriden the getView() ? – Shubhayu Jul 17 '12 at 09:32
  • No I have not, what should I override it with? – user1504495 Jul 17 '12 at 09:37
  • you could check this post http://stackoverflow.com/questions/5183813/android-issue-with-newview-and-bindview-in-custom-simplecursoradapter might be of help. also for this LayoutInflater inflater = LayoutInflater.from(parent.getContext()); use context instead of parent.getContext() – Shubhayu Jul 17 '12 at 10:02
  • Pasting the code into GetView seems to have done the trick! No idea why I'd ever use new/bindview then...Thanks a bunch! I'm not sure about the rules of StackOverflow but I'll accept your answer as it's in the comments... – user1504495 Jul 17 '12 at 10:18
  • i'll add it to the answer so that anyone else referring to it gets it :) – Shubhayu Jul 17 '12 at 10:27