gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:50:36 26 4
gpt4 key购买 nike

它不重复。我看到所有答案都是堆栈。我的问题:我有 CommentActivity,我在其中获取 Cursor。在 CommentCursorAdapter 中,我从数据库获取值。在适配器中,我有两个图像:喜欢和不喜欢。当我单击“赞”时,数据库中的评级会增加。TextView 评级应在按下后显示新的评级。如何正确地做到这一点?

评论 Activity

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

最佳答案

您可以选择一些选项:

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

关于android - 如何使用光标适配器刷新适配器中的评级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38092228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com