我正在创建一个显示帖子的应用程序。这些帖子有评论, 当用户登录到我正在查询数据库的应用程序时,我正在显示注释。但是当用户输入新评论时,我已经动态地将其显示给用户,然后当用户再次登录时,我将查询来自数据库的评论。当用户点击提交按钮时,如何动态显示卡片视图?欢迎提出所有建议。
我的适配器类:
public class ToadlineCommentAdapter extends RecyclerView.Adapter<ToadlineCommentAdapter.MyViewHolder> {
private ClickListener clickListener;
private SwipeRefreshLayout.OnRefreshListener clickListener1;
private LayoutInflater inflater;
Context mContext;
List<CommentDataStore> data = Collections.EMPTY_LIST;
public ToadlineCommentAdapter(Context context, List<CommentDataStore> data) {
inflater = LayoutInflater.from(context);
this.data = data;
mContext = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_comment_layout, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return data.size();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public void setClickListener1(SwipeRefreshLayout.OnRefreshListener clickListener1) {
this.clickListener1 = clickListener1;
}
// View Holder object for Recycler View
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
TextView commentUserName, commentDays, commentDescription, commentAgreeCount, commentDisAgreeCount;
ImageView commentImage;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener((View.OnClickListener) this);
commentImage = (ImageView) itemView.findViewById(R.id.textViewNoOfDays);
commentDescription = (TextView) itemView.findViewById(R.id.textViewCommentDescription);
commentAgreeCount = (TextView) itemView.findViewById(R.id.textViewAgreeCount);
commentDisAgreeCount = (TextView) itemView.findViewById(R.id.textViewDisAgreeCount);
}
@Override
public void onClick(View view) {
if (clickListener != null) {
clickListener.itemClicked(view, getPosition());
}
}
@Override
public void onRefresh() {
onClick(itemView);
}
}
public interface ClickListener {
public void itemClicked(View view, int position);
}
}
如果您需要更多信息,请与我们联系。
我的活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewTimeline);
mComments = (EditText) findViewById(R.id.textViewNewCommentDescription);
mProfileImage = (ImageView) findViewById(R.id.imageViewUserNewComment);
mSubmit = (Button) findViewById(R.id.buttonCommentSubmit);
commentProfileImageHashMap = (HashMap<String, ArrayList<String>>) getIntent().getExtras().getSerializable("CommentHashMapProfileImage");
commentDaysHashMap = (HashMap<String, ArrayList<String>>) getIntent().getExtras().getSerializable("CommentHashMapDays");
commentDescriptionHashMap = (HashMap<String, ArrayList<String>>) getIntent().getExtras().getSerializable("CommentHashMapDescription");
commentUserNameHashMap = (HashMap<String, ArrayList<String>>) getIntent().getExtras().getSerializable("CommentHashMapUserName");
commentAgreeCountHashMap = (HashMap<String, ArrayList<String>>) getIntent().getExtras().getSerializable("CommentHasMapAgree");
commentDisAgreeCountHashMap = (HashMap<String, ArrayList<String>>) getIntent().getExtras().getSerializable("CommentHashMapDisAgree");
ToadlineCommentAdapter adapter1 = new ToadlineCommentAdapter(getApplicationContext(), getData2());
recyclerView.setAdapter(adapter1);
adapter1.setClickListener(this);
adapter1.notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredCommentText = mComments.getText().toString();
if (enteredCommentText != null) {
if (enteredCommentText.equals("") || enteredCommentText.startsWith(" ")) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.empty_comment_text), Toast.LENGTH_SHORT).show();
return;
} else {
new RegisterCheck().execute();
}
}
}
});
}
public static List<CommentDataStore> getData2() {
List<CommentDataStore> data = new ArrayList<>();
Bitmap[] commentProfileImage = commentProfileImageDownload;
ArrayList<String> commentUserName1 = newCommentUserName;
ArrayList<String> commentDays1 = newCommentDays;
ArrayList<String> commentDescription1 = newCommentDescription;
ArrayList<String> commentAgreeCount1 = newCommentAgreeCount;
ArrayList<String> commentDisAgreeCount1 = newCommentDisAgreeCount;
for (int i = 0; i < commentProfileImage.length && i < commentUserName1.size() && i < commentDays1.size() && i < commentDescription1.size(); i++) {
CommentDataStore current = new CommentDataStore();
current.commentProfileImageLink = commentProfileImage[i];
current.commentUserName = commentUserName1.get(i);
current.commentDays = commentDays1.get(i);
current.commentDescription = commentDescription1.get(i);
current.commentAgreeCount = commentAgreeCount1.get(i);
current.commentDisAgreeCount = commentDisAgreeCount1.get(i);
data.add(current);
}
return data;
}
public class RegisterCheck extends AsyncTask<String, String, String> implements ToadlineCommentAdapter.ClickListener {
private HttpURLConnection conn;
private String
text;
@Override
protected String doInBackground(String... strings) {
if (response != null) {
JSONObject jsonObject = new JSONObject(text);
commentContent = jsonObject.getString("content");
commenterId = jsonObject.getString("commenterId");
commenterName = jsonObject.getString("commenterName");
commenterPhotos = jsonObject.getString("commenterPhotos");
commentCreatedDate = jsonObject.getString("dateCreated");
newCommentDescription.add(commentContent);
newCommentUserName.add(commenterName);
ToadlineCommentAdapter adapter1 = new ToadlineCommentAdapter(getApplicationContext(), getData2());
recyclerView.setAdapter(adapter1);
adapter1.setClickListener(CommentActivity.this);
adapter1.notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
}
}
}
答案 0 :(得分:0)
您正在使用List,因此您可以在提交按钮
上将新数据添加到List 0索引中data.add(0, yourObject);
然后致电
adapter.notifyDataSetChanged();
通知适配器数据已更改
这样你的新对象将被添加到循环视图的第一个位置
答案 1 :(得分:0)
如果我正确理解您的问题,您希望在列表与稍后数据库中的注释同步之前向适配器添加新注释。
只需使用
toadlineCommentAdapter.add(new CommentDataStore("New comment"), 0);
toadlineCommentAdapter.notifyDataSetChanged();
将新对象添加为实例化引用的适配器的第一个元素。
例如,您可以在活动中执行以下操作:
ToadlineCommentAdapter toadlineCommentAdapter = new ToadlineCommentAdapter(mContext, commentsList);
newCommentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toadlineCommentAdapter.add(new CommentDataStore("New comment"), 0);
toadlineCommentAdapter.notifyDataSetChanged();
}
});
稍后,当您将评论与数据库同步时,您可以执行以下操作:
toadlineCommentAdapter.clear();
toadlineCommentAdapter.addAll(newlyFetchedCommentsList);
toadlineCommentAdapter.notifyDataSetChanged();
确切的实现取决于您如何实例化新的CommentDataStore对象以及如何从数据库中获取数据。