检索数据时,CursorAdapter以静默方式失败

时间:2014-06-04 22:49:21

标签: java android sqlite android-cursoradapter

我试图获取使用自定义CursorAdapter存储的数据,但到目前为止,它仍然无声地失败。它只是加载一个空白视图,并且不打印任何内容。

以下是主片段的onCreateView:

@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();
    }
}

1 个答案:

答案 0 :(得分:1)

在构建null实例时传递Cursor TweetAdapter,因此适配器开始时没有数据。然后updateList()将适配器的null Cursor替换为mCursor中的任何内容(您未在自己提供的代码中显示)。

如果mCursor也是null,那么您的代码最终会显示没有内容(您正在看到的内容)或抛出未捕获的异常。

因此,请确保在调用mCursor之前确实执行了填充updateList()变量的查询。