Java 在安卓系统中,如何在刷卡刷新后保持UI更改

Java 在安卓系统中,如何在刷卡刷新后保持UI更改,java,android,Java,Android,因此,我在我的CommentsAdapter类中有一个getView方法,它基本上允许用户在显示向上投票计数的同时向上投票或向下投票评论部分中的评论。我可以通过相应的UI更改(蓝色按钮表示未投票,橙色按钮表示已投票)向上投票(并撤消向上投票) 但是,一旦我刷新屏幕,按钮将始终恢复为蓝色,无论它是否被升级,投票计数恢复到原始计数。我确实检查了数据库,并记录了正确的投票计数和操作。有什么建议吗 public class CommentsAdapter extends ArrayAdapter<

因此,我在我的
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;
  }
}
公共类CommentsAdapter扩展了ArrayAdapter{
专用int布局;
私人语境;
private ArrayList postList=new ArrayList();
公共CommentAdapter(@NonNull Context cont、@LayoutRes int textViewResourceId、@NonNull ArrayList objects){
超级(cont,textViewResourceId,objects);
布局=textViewResourceId;
上下文=续;
postList=对象;
}
公共视图getView(最终整数位置,@Nullable View convertView,@NonNull ViewGroup父级){
最终持票人v;
最终帖子注释=postList.get(position);
最终字符串mimeType=“text/html”;
最终字符串编码=“UTF-8”;
final SharedReferences prefs=context.getSharedReferences(“用户会话”,模式为私有);
最后一个字符串sessionKey=prefs.getString(“session_key”,”);
字符串htmlData=“”+comment.content;
if(convertView==null){
v=新的视图保持架();
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(布局,父级,假);
v、 upvotedIcon=convertView.findViewById(R.id.navbar\u upvoted\u图标);
v、 upvoteIcon=convertView.findViewById(R.id.navbar\u upvote\u图标);
v、 upvotedText=convertView.findViewById(R.id.navbar\u upvoted\u text);
v、 upvoteText=convertView.findViewById(R.id.navbar\u upvote\u text);
v、 upvoteButton=convertView.findViewById(R.id.navbar\u upvote\u按钮);
v、 upvotedButton=convertView.findViewById(R.id.navbar\u upvoted\u按钮);
v、 upvotedIcon.setTypeface(字体);
v、 upvoteIcon.setTypeface(字体);
v、 setTypeface(OpenSAN);
v、 upvoteText.setTypeface(OpenSAN);
convertView.setTag(v);
}否则{
v=(ViewHolder)convertView.getTag();
}
v、 setText(String.format(Locale.ENGLISH,“%d”,comment.stats.upvoces));
v、 setText(String.format(Locale.ENGLISH,“%d”,comment.stats.upvots+1));
如果(comment.hasReacted){
v、 设置可见性(View.GONE);
v、 upvotedButton.setVisibility(View.VISIBLE);
}否则{
v、 upvotedButton.setVisibility(View.GONE);
v、 设置可见性(View.VISIBLE);
}
v、 setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图){
setButtonState(true,comment,v);
Call Call=MyApi.endpoint().upVotePost(sessionKey,comment.id);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
如果(response.code()!=200){
//显示向上投票图标
setButtonState(假,注释,v);
Toast.makeText(上下文,“暂时无法向上投票,请稍后再试。”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
//显示向上投票图标
setButtonState(假,注释,v);
Toast.makeText(上下文,“无法连接到服务器,请稍后重试。”,Toast.LENGTH_SHORT.show();
}
});
}
});
v、 upvotedButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//显示向上投票图标
setButtonState(假,注释,v);
Call Call=MyApi.endpoint().downVotePost(sessionKey,comment.id);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
如果(response.code()!=200){
//显示向上投票的图标
setButtonState(true,comment,v);
Toast.makeText(上下文,“暂时无法撤消向上投票,请稍后再试。”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
//显示向上投票的图标
setButtonState(true,comment,v);
Toast.makeText(上下文,“无法连接到服务器,请稍后重试。”,Toast.LENGTH_SHORT.show();
}
});
}
});
返回视图;
}
}

您应该在模型中添加一个状态属性,其值为“向上投票”、“向下投票”等或{empty string}。然后,当对数据执行操作时,相应地设置该状态。然后,在重新加载数据时,检查您的模型的状态,并在每个状态下使用适当的图标显示。(我只是使用字符串作为状态值,但您可以做一些更优雅的事情)

编辑:

正如一些用户指出的那样,确保在修改数据库值后调用
notifyDataSetChanged
。此外,如果要从远程服务器提取数据,请确保在重新加载之前更新了upvote/downvote值(或至少正确更新)