Android:使用sqlite中的数据填充列表视图

时间:2015-12-23 20:28:57

标签: android sqlite

概述:我现在尝试制作的程序包含以下步骤。

  1. 从用户处获取输入数据并将其存储在SQLite数据库中。
  2. 使用从数据库中检索的数据填写列表视图。
  3. 为了实现1,我首先创建了addNewItem()方法,如下所示。

    public void addNewPost(Post post) {
        SQLiteDatabase db = this.getWritableDatabase();
    
        ContentValues values = new ContentValues();
    
        // get the variables which are to be put into the database row.
        String uploader = post.getUploader();
        String content = post.getContent();
        String privacy_level = post.getPrivacyLevel();
        String uploaded_at = post.getUploadedAt();
        String edited_at = post.getEditedAt();
    
        values.put(KEY_UPLOADER, uploader);
        values.put(KEY_CONTENT, content);
        values.put(KEY_PRIVACY_LEVEL, privacy_level);
        values.put(KEY_UPLOADED_AT, uploaded_at);
        values.put(KEY_EDITED_AT, edited_at);
    
        db.insert(TABLE_POST, null, values);
        db.close();
    }
    

    对于2,我创建了另一个名为initListView()的方法。

    private void initListView() {
        SQLiteDatabase db = sqliteHandler.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + SQLiteHandler.TABLE_POST, null);
        username.clear();
        uploaded_at.clear();
        content.clear();
    
        if(cursor.moveToFirst()) {
            do {
                username.add(cursor.getString(cursor.getColumnIndex(DataKeyLists.KEY_UPLOADER)));
                uploaded_at.add(cursor.getString(cursor.getColumnIndex(DataKeyLists.KEY_UPLOADED_AT)));
                content.add(cursor.getString(cursor.getColumnIndex(DataKeyLists.KEY_CONTENT)));
            } while(cursor.moveToNext());
        }
    
        FeedAdapter adapter = new FeedAdapter(this, username, uploaded_at, content);
        listView.setAdapter(adapter);
        cursor.close();
    }
    

    最后这是FeedAdapter类。

    public class FeedAdapter extends BaseAdapter {
    
        private Context context;
        private ArrayList<String> username;
        private ArrayList<String> uploaded_at;
        private ArrayList<String> content;
    
        public FeedAdapter(Context context, ArrayList<String> username, ArrayList<String> uploaded_at, ArrayList<String> content) {
            this.context = context;
            this.username = username;
            this.uploaded_at = uploaded_at;
            this.content = content;
        }
    
        public int getCount() {
            return username.size();
        }
    
        public Object getItem(int position) {
            return null;
        }
    
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater;
    
            if(convertView == null) {
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.feed_item, null);
    
    
                ImageView ivFeedThumbnail = (ImageView) convertView.findViewById(R.id.ivFeedThumbnail);
                TextView tvFeedUsername = (TextView) convertView.findViewById(R.id.tvFeedUsername);
                TextView tvFeedUploaded = (TextView) convertView.findViewById(R.id.tvFeedCreated);
                TextView tvFeedText = (TextView) convertView.findViewById(R.id.tvFeedText);
                TextView tvFeedLikes = (TextView) convertView.findViewById(R.id.tvFeedLikes);
                TextView tvFeedComments = (TextView) convertView.findViewById(R.id.tvFeedComments);
                Button btnLike = (Button) convertView.findViewById(R.id.btnLike);
                Button btnComment = (Button) convertView.findViewById(R.id.btnComment);
    
                tvFeedUsername.setText(username.get(position));
                tvFeedUploaded.setText(uploaded_at.get(position));
                tvFeedText.setText(content.get(position));
    
            }
    
            return convertView;
        }
    }
    

    代码似乎没有问题,但是当我运行应用程序时,列表仍然是空的。

    非常感谢解决方案的任何提示。

0 个答案:

没有答案