gpt4 book ai didi

java - Android 中滑动刷新后如何保留 UI 更改

转载 作者:行者123 更新时间:2023-12-02 02:37:37 24 4
gpt4 key购买 nike

所以,我的 CommentsAdapter 类中有这个 getView 方法,它基本上允许用户在显示赞成票计数的同时对评论部分中的评论进行投票赞成或反对。我可以通过相应的 UI 更改来投票(并撤消投票)(蓝色按钮表示未投票,橙色按钮表示投票)。

但是,一旦我刷新屏幕,无论是否投票,该按钮将始终恢复为蓝色,并且投票数将恢复到原始计数。我确实检查了数据库并记录了正确的点赞数和操作。有什么建议吗?

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

最佳答案

您应该向模型添加一个状态属性,其值为“upvoted”、“downvoted”等,或{空字符串}。然后,当您的数据被执行时,相应地设置该状态。然后,当数据重新加载时,检查模型的状态并使用每个状态的正确图标进行显示。 (我只是使用字符串作为状态值,但你可以做一些更优雅的事情)。

编辑:

正如一些用户所指出的,请确保在修改数据库值后调用 notifyDataSetChanged。此外,如果您从远程服务器提取数据,请确保在重新加载之前已更新赞成票/反对票值(或者至少正确地将远程数据与本地数据合并)。

关于java - Android 中滑动刷新后如何保留 UI 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46048043/

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