Search code examples
javaandroidsqliteandroid-cursoradapter

CursorAdapter fails silently when retrieving data


I'm trying to fetch the data stored using a custom CursorAdapter, but so far it is failing silently. It just loads a blank view and doesn't print anything.

Here is the onCreateView for the main fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View view = inflater.inflate(R.layout.network_listview, container, false);
    mListView = (ListView)view.findViewById(R.id.network_listview);     

    aToken = getSherlockActivity().getIntent().getStringExtra("token");
    aTokenSecret = getSherlockActivity().getIntent().getStringExtra("token_secret");

    context = getSherlockActivity().getBaseContext();

    ConfigurationBuilder builder = new ConfigurationBuilder(); 
    builder.setOAuthConsumerKey(Const.CONSUMER_KEY);
    builder.setOAuthConsumerSecret(Const.CONSUMER_SECRET);
    builder.setOAuthAccessToken(aToken);
    builder.setOAuthAccessTokenSecret((aTokenSecret));
    Configuration configuration = builder.build();
    mTwitter = new TwitterFactory(configuration).getInstance();

    mListAdapter =  getListAdapter(); 
    mListView.setAdapter(mListAdapter); 

    updateList();

    return view;
}

getListAdapter():

CursorAdapter getListAdapter() { 
    CursorAdapter ad = new TweetAdapter(getSherlockActivity(), null);
    return ad; 
}

TweetAdapter:

public class TweetAdapter extends CursorAdapter 
{
    private ImageLoader imageLoader; 

    private static class ViewHolder
    {
        private ImageView profileView;
        private TextView updated; 
        private ImageView favoriteIcon; 
        private TextView name; 
        private TextView message; 
        private TextView retweeted_by;

        private ViewHolder(View row)
        {
            profileView = (ImageView)row.findViewById(R.id.preview); 
            updated = (TextView)row.findViewById(R.id.updated);
            favoriteIcon = (ImageView)row.findViewById(R.id.favorite_icon);
            name = (TextView)row.findViewById(R.id.name);
            message = (TextView)row.findViewById(R.id.message);
            retweeted_by = (TextView)row.findViewById(R.id.retweeted_by);
        }
    }

    public TweetAdapter(Context context, Cursor c){
        super(context, c, true);        
    }

    @Override
    public void bindView(View row, Context context, Cursor cursor) 
    {
     // this doesnt print out anything, even though there is data in the database
        String tweetText = cursor.getString(cursor.getColumnIndex(Tweets.COL_TEXT_PLAIN));
        System.out.println("Tweet Text: " + tweetText);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.item_tweet, parent, false); 
        ViewHolder viewHolder = new ViewHolder(row);
        row.setTag(viewHolder);

        return row; 
    }
}

updateList():

void updateList() 
{
    mCursor = getCursor();
    Cursor oldCursor = mListAdapter.swapCursor(mCursor);
    mListAdapter.notifyDataSetChanged();

    if (oldCursor != null) {
            oldCursor.close();
    }
}

Solution

  • You pass a null Cursor when constructing the TweetAdapter instance, so your adapter starts out with no data. Then updateList() replaces the adapter's null Cursor with whatever is in mCursor (which you do not show in the code you provided).

    If mCursor is also null, then your code will end up either displaying no content (which is what you are seeing) or throwing an uncaught exception.

    So, make sure you actually do a query that fills in the mCursor variable before calling updateList() .