mercredi 23 décembre 2015

Android: fill list view with data from sqlite

Overview: The programme I'm now trying to make has the following steps.

  1. get the input data from users and store them in the SQLite database.
  2. Fill the list view with the data retrieved from the database.

In order to implement 1, I first created the addNewItem() method as follows.

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

And for 2, I created another method called 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();
}

And finally this is the FeedAdapter class.

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;
    }
}

The code seems to have no problem, but when I run the application, the list is still empty.

Any tips for the solution will be very much appreciated.

Aucun commentaire:

Enregistrer un commentaire