因此,我在CommentsAdapter
類中使用了此getView
方法,它基本上允許用戶在顯示upvote計數的同時在評論部分中投票或下投注釋。我可以通過相應的用戶界面更改(藍色按鈕用於未投票和橙色用於投票)來上調(並取消投票)。如何在Android中刷新後保持UI更改
但是,一旦我刷新了屏幕,無論它是否被upvoted,該按鈕將總是恢復爲藍色,並且投票計數返回原始計數。我沒有檢查數據庫,記錄了正確的upvote計數和操作。有小費嗎?
public class CommentsAdapter extends ArrayAdapter<Post> {
private int layout;
private Context context;
private ArrayList<Post> postList = new ArrayList<>();
public CommentAdapter(@NonNull Context cont, @LayoutRes int textViewResourceId, @NonNull ArrayList<Post> objects) {
super(cont, textViewResourceId, objects);
layout = textViewResourceId;
context = cont;
postList = objects;
}
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder v;
final Post comment = postList.get(position);
final String mimeType = "text/html";
final String encoding = "UTF-8";
final SharedPreferences prefs = context.getSharedPreferences("user_session", MODE_PRIVATE);
final String sessionKey = prefs.getString("session_key", "");
String htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"comments.css\" />" + comment.content;
if (convertView == null) {
v = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layout, parent, false);
v.upvotedIcon = convertView.findViewById(R.id.navbar_upvoted_icon);
v.upvoteIcon = convertView.findViewById(R.id.navbar_upvote_icon);
v.upvotedText = convertView.findViewById(R.id.navbar_upvoted_text);
v.upvoteText = convertView.findViewById(R.id.navbar_upvote_text);
v.upvoteButton = convertView.findViewById(R.id.navbar_upvote_button);
v.upvotedButton = convertView.findViewById(R.id.navbar_upvoted_button);
v.upvotedIcon.setTypeface(fontAwesome);
v.upvoteIcon.setTypeface(fontAwesome);
v.upvotedText.setTypeface(opensans);
v.upvoteText.setTypeface(opensans);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
v.upvoteText.setText(String.format(Locale.ENGLISH, "%d", comment.stats.upvotes));
v.upvotedText.setText(String.format(Locale.ENGLISH, "%d", comment.stats.upvotes + 1));
if (comment.hasReacted) {
v.upvoteButton.setVisibility(View.GONE);
v.upvotedButton.setVisibility(View.VISIBLE);
} else {
v.upvotedButton.setVisibility(View.GONE);
v.upvoteButton.setVisibility(View.VISIBLE);
}
v.upvoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setButtonState(true, comment, v);
Call<JsonObject> call = MyApi.endpoint().upVotePost(sessionKey, comment.id);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if(response.code() != 200) {
// show upvote icon
setButtonState(false, comment, v);
Toast.makeText(context, "Cannot upvote for the moment, try again later.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
// show upvote icon
setButtonState(false, comment, v);
Toast.makeText(context, "Cannot connect to the server, try again later.", Toast.LENGTH_SHORT).show();
}
});
}
});
v.upvotedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// show upvote icon
setButtonState(false, comment, v);
Call<JsonObject> call = MyApi.endpoint().downVotePost(sessionKey, comment.id);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if(response.code() != 200) {
// show upvoted icon
setButtonState(true, comment, v);
Toast.makeText(context, "Cannot undo your upvote for the moment, try again later.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
// show upvoted icon
setButtonState(true, comment, v);
Toast.makeText(context, "Cannot connect to the server, try again later.", Toast.LENGTH_SHORT).show();
}
});
}
});
return convertView;
}
}
你可以在你的代碼中使用'notifyDataSetChanged();'嗎? – KeLiuyue
@KeLiuyue我該怎麼做這個課程? 'CommentsAdapter'擴展'ArrayAdapter'順便說一句。 – Bargain23
您可以添加代碼嗎,您如何使用'postList'初始化適配器? –