Maintaining a ListView in which each list item is related from one to the other

As my application is set up, I have a ListView supported by CursorLoader, messages made by users. Each post has user comments that are associated with it. Each listitem has its own text view at the bottom, where comments are scrolled (with right or left swipe). Inside each of the custom text views, there is a list of comments related to its specific post. Its kind of comments about the Google+ app, with the exception of the elements, scroll. Comments are stored in a separate database table than messages.

The problem I am facing is that every time BindView is called, I query the database and retrieve the cursor with the corresponding comments and add it to the user text view list. It seems really inefficient to query the comment database table for each item. So I'm wondering if there would be an ideal way to handle this. My code looks something like this:

public void bindView(View view, Context context, Cursor cursor) final String rowid = cursor.getString(cursor.getColumnIndexOrThrow(Database.ROWID)); Cursor commentcursor = c.getContentResolver().query(DatabaseProvider.CONTENT_URI_COMMENTTABLE, freeWriteCommentColumns, Database.PARENTPOSTROWID + " =?", new String[]{rowid}, null); commentcursor.moveToFirst(); while (commentcursor.isAfterLast() == false){ //get comment //add to comment text view list } 

I looked at CursorJoiners, but that doesn't seem to be useful. I played with JOINS, but this makes the number of lines much larger than necessary. I am now playing with the holder for the comment table cursor, which is created when the adapter is created and set as a global variable. It seems like a decent prospect because I don’t need to demand every time. I'm just not sure how to deal with this situation.

+7
android sqlite android-listview android-cursoradapter android-cursorloader
source share
1 answer

I know that I have tried this, but this is the way to go. I had almost the same problem, even bigger, because mine was with JOIN and then MergeCursor. But let's get back to your problem: bindView () is called in the user interface, and you make DB calls in the user interface thread, this is bad practice and, in addition, you use the entire cursor. I would join a query for a message and concatenate comments at the SQL load level, and then I would just use one cursor for messages and comments, and everything would be much better.

How to reconcile string at SQL level? all comments together? Well, I'll take a look and edit my answer.

+1
source share

All Articles