如何使用光标适配器刷新适配器中的评级

时间:2016-06-29 06:47:18

标签: android mysql android-cursoradapter

它没有重复。我看到所有答案都是Stack。 我的问题:我有CommentActivity,我得到了Cursor。在CommentCursorAdapter中,我从Database获取值。 在适配器中我有两个图像:喜欢和不喜欢。当我点击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();
        }
    });
}
}

2 个答案:

答案 0 :(得分:0)

您可以选择以下选项:

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

答案 1 :(得分:0)

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

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

希望它会对你有所帮助。