我的主要活动有一个哈希映射,它从parse-server获取数据以传递到自定义列表视图适配器类。
当我单击列表视图行中的按钮时,我希望每次单击时都会更改文本。但是,仅在单击按钮一次时,行中的文本才会更改。如果我再次点击它,没有任何反应。我假设这与我有关,我的notifyDataSetChanged()
被错误地调用了。
按钮和textview都在自定义列表视图适配器类中设置。我可以在主活动或自定义列表视图适配器类中调用notifyDataSetChanged()
吗?
我的MainActivity中的customFeedListViewAdapter.notifyDataSetChanged();
允许我在单击按钮一次时成功更改文本。但是,当我再次单击该按钮时,没有任何反应。
主要活动:
customFeedListViewAdapter = new CustomFeedListViewAdapter(this, arrayFeedList);
feedList = (ListView) findViewById(R.id.feedListView);
feedList.setAdapter(customFeedListViewAdapter);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("FeedItem");
ParseUser.getCurrentUser().getUsername());
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){
for(ParseObject object: objects){
HashMap<String, String> data = new HashMap<String, String>();
data.put("user", (object.getString("username")));
data.put("feed", (object.getString("textItem")));
data.put("likes", String.valueOf(object.getInt("likes")));
data.put("replies",String.valueOf(object.getInt("comments")));
object.saveInBackground();
arrayFeedList.add(data);
customFeedListViewAdapter.notifyDataSetChanged();
}
customFeedListViewAdapter = new CustomFeedListViewAdapter(getApplicationContext(), arrayFeedList);
feedList.setAdapter(customFeedListViewAdapter);
}
}
});
自定义ListView适配器:
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
position = i;
final int likes[] = new int[feed.size()];
final ViewHolder holder;
if (view == null) {
view = inflater.inflate(R.layout.feed_list_row, viewGroup, false);
holder = new ViewHolder();
holder.feedProfilePic = (ImageView) view.findViewById(R.id.feedProfilePic);
holder.feedUsername = (TextView) view.findViewById(R.id.feedUsernameId);
holder.feedNumOfLikes = (TextView) view.findViewById(R.id.feedNumofLikes);
holder.feedFeedItem = (TextView) view.findViewById(R.id.feedTextId);
holder.feedDate = (TextView) view.findViewById(R.id.feedDate);
holder.feedNumofReplies = (TextView) view.findViewById(R.id.feedRepliesNumId);
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedDownVoteButton = (Button) view.findViewById(R.id.feedDownVoteButton);
holder.feedCommentButton = (Button) view.findViewById(R.id.feedCommentButton);
holder.feedListView = (ListView) view.findViewById(R.id.feedListView);
view.setTag(holder);
HashMap<String, String> mFeed = new HashMap<>();
mFeed = feed.get(i);
holder.feedUsername.setText(mFeed.get("user"));
holder.feedFeedItem.setText(mFeed.get("feed"));
holder.feedDate.setText(mFeed.get("date"));
holder.feedNumOfLikes.setText(mFeed.get("likes"));
likesString = mFeed.get("likes");
mLikes = Integer.valueOf(likesString);
holder.feedNumofReplies.setText(mFeed.get("replies"));
position = i;
}
else{
position = i;
holder = (ViewHolder) view.getTag();
}
holder.feedUpVoteButton.setTag(position);
holder.feedDownVoteButton.setTag(position);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("FeedItem");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> objects, ParseException e) {
if(e == null){
for (final ParseObject object : objects) {
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
parseObFeed = objects.get(pos);
likes[pos] = mLikes;
Log.i("yo","Check1: Get Likes from Parse: " + String.valueOf(likes[pos]));
likes[pos] += 1;
Log.i("yo","Check2: add 1: " + String.valueOf(likes[pos]));
parseObFeed.put("likes", likes[pos]);
Log.i("yo","Check3: Put Likes in Parse: " + String.valueOf(likes[pos]));
parseObFeed.put("upClicked", true);
parseObFeed.saveInBackground();
holder.feedNumOfLikes.setText(String.valueOf(parseObFeed.getInt("likes")
));
Log.i("yo","Check4: Get Likes in Set Text: " + String.valueOf(likes[pos]));
}
}
}
});
return view;
}
private class ViewHolder {
ImageView feedProfilePic;
TextView feedUsername;
TextView feedNumOfLikes;
TextView feedFeedItem;
TextView feedDate;
TextView feedNumofReplies;
Button feedUpVoteButton;
Button feedDownVoteButton;
Button feedCommentButton;
ListView feedListView;
}