【发布时间】: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();
}
});
}
}
【问题讨论】:
-
您需要创建一个接口并在活动中调用它。查看此链接以供参考stackoverflow.com/a/35170004/5746722
标签: android mysql android-cursoradapter