因此,我在我的getView类中有此CommentsAdapter方法,它基本上允许用户在显示upvote计数的同时在评论部分中对一个评论进行upvote或downvote。我可以使用相应的UI更改进行投票(并撤消更改)(蓝色按钮代表未投票,橙色按钮代表已投票)。

但是,一旦刷新屏幕,无论是否进行了投票,该按钮将始终恢复为蓝色,并且投票数恢复为原始计数。我确实检查了数据库,并记录了正确的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;
  }
}

最佳答案

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

编辑:

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

10-06 16:09