【问题标题】:How to refresh rating in adapter using the Cursor Adapter如何使用光标适配器刷新适配器中的评级
【发布时间】:2016-06-29 06:47:18
【问题描述】:

它不是重复的。我看到所有的答案都是堆栈。 我的问题:我有 CommentActivity,在那里我得到了光标。在 CommentCursorAdapter 我从数据库中获取值。 在适配器中,我有两个图像:喜欢和不喜欢。当我单击 Like - 在数据库中评级增加。 TextView 评分应在按下后显示新评分。如何正确操作?

评论活动

public class CommentActivity extends AppCompatActivity {

private String idComment;
private ListView listComments;
private CommentCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comment);
    listComments = (ListView) findViewById(R.id.list_comments);

    idComment = getIntent().getStringExtra(MainActivity.ID_KEY);
    getCursorData();
}

private void getCursorData() {
    adapter = new CommentCursorAdapter(this, new CursorLoader(App.getInstance(), idComment).loadInBackground(), 0);
    listComments.setAdapter(adapter);

}

private static class CursorLoader extends SimpleCursorAdapter {
    private String idComment;

    public CursorLoader(Context context, String idComment) {
        super(context);
        this.idComment = idComment;
    }

    @Override
    public Cursor loadInBackground() {
        return App.getInstance().getDb().rawQuery(
                "SELECT comment._id AS _id, comment.text AS text, user.email AS email, comment.rate FROM comment JOIN user ON comment.userId = user._id WHERE comment.postId = ? ORDER BY _id ASC", new String[]{
                        idComment
                });
    }
}
}

CommentCursorAdapter

public class CommentCursorAdapter extends CursorAdapter {

private TextView commentEmail;
private TextView commentText;
private TextView ratingText;
private ImageView imageViewLike;
private ImageView imageViewDislike;

private static final String ID = "_id";
private static final String EMAIL = "email";
private static final String TEXT = "text";
private static final String RATE = "rate";

public CommentCursorAdapter(Context context, Cursor c, int flags) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    return LayoutInflater.from(context).inflate(R.layout.adapter_comment, viewGroup, false);
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {
    commentEmail = (TextView) view.findViewById(R.id.comment_email);
    commentText = (TextView) view.findViewById(R.id.comment_text);
    ratingText = (TextView) view.findViewById(R.id.rating_text);
    imageViewLike = (ImageView) view.findViewById(R.id.image_like);
    imageViewDislike = (ImageView) view.findViewById(R.id.image_dislike);

    imageViewLike.setTag(cursor.getString(cursor.getColumnIndexOrThrow(ID)));
    imageViewDislike.setTag(cursor.getString(cursor.getColumnIndexOrThrow(ID)));

    String email = cursor.getString(cursor.getColumnIndexOrThrow(EMAIL));
    String text = cursor.getString(cursor.getColumnIndexOrThrow(TEXT));
    String rating = cursor.getString(cursor.getColumnIndexOrThrow(RATE));

    commentEmail.setText(email);
    commentText.setText(text);
    ratingText.setText(rating);

    imageViewLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("like", "click");
            SQLiteStatement statement = App.getInstance().getDb().compileStatement(
                    "UPDATE comment SET rate = rate + 1 WHERE comment._id = ?"
            );
            statement.bindString(1, (String) view.getTag());

            try {
                statement.execute();
            } finally {
                statement.close();
            }

        }
    });

    imageViewDislike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("dislike", "click");
            SQLiteStatement statement = App.getInstance().getDb().compileStatement(
                    "UPDATE comment SET rate = rate - 1 WHERE comment._id = ?"
            );
            statement.bindString(1, (String) view.getTag());

            try {
                statement.execute();
            } finally {
                statement.close();
            }

            //swapCursor(cursor);
            //notifyDataSetChanged();
        }
    });
}
}

【问题讨论】:

标签: android mysql android-cursoradapter


【解决方案1】:

您可以选择一些选项:

  1. 在数据库中更新评分后,您可以使用加载器重新加载所有数据。
  2. 您可以将数据库逻辑迁移到使用ContentProvider。如果操作正确,提供程序可以通知所有加载程序它们的基础数据已更改,因此它们会自动重新加载。

【讨论】:

    【解决方案2】:

    在您的 CustomAdapter 中,您需要将 this.notifyDataSetChanged(); 放在您正在执行 LIKECOMMENT 操作的位置。

    很高兴您使用了 CursorAdapter - 侦听更新通知并自动重新加载内容。

    希望对你有所帮助。

    【讨论】:

    • notifyDataSetChanged() 在这种情况下不起作用)谢谢
    • @ЕгорУрбанович : 你能告诉我你从哪里调用 notifyDataSetChanged() 吗?
    • in imageViewDislike (and like) after block try-finally
    • @ЕгорУрбанович : 执行操作后单击按钮调用它
    • 请查看我的部分代码 imageViewDislike.seotOnClickListener... 我注释掉了我放置 notifyDataSetChanged(); 的地方
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多