在每一行中,我都有一个按钮和一个textView。单击Button时,它成功将textView设置为该行中的新值。但是,当我在ParseQuery()
中调用GetView();
并尝试执行object.put("objectName", num)
时,填充到我的Parse-Server的唯一值是最后一行。 我的SetTag();
和GetTag();
以及ViewHolder class
是正确的当我点击按钮时,我相信android工作室不确定要填充哪一行的TextView它只是自动填充最后一行的TextView。
自定义ListView适配器类
public class CustomFeedListViewAdapter extends BaseAdapter {
String likesString;
int position;
private Context mContext;
private ArrayList<HashMap<String, String>> feed;
private static LayoutInflater inflater = null;
ParseObject parseObFeed;
public CustomFeedListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {
super();
this.mContext = context;
this.feed = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return feed.size();
}
@Override
public Object getItem(int i) {
return feed.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
position = i;
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.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
view.setTag(holder);
HashMap<String, String> mFeed = new HashMap<>();
mFeed = feed.get(i);
holder.feedNumOfLikes.setText(mFeed.get("likes"));
likesString = mFeed.get("likes");
holder.mLikes = Integer.valueOf(likesString);
position = i;
}
else{
position = i;
holder = (ViewHolder) view.getTag();
}
holder.feedUpVoteButton.setTag(position);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("FeedItem");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(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) {
ParseObject[] mParseObject = new ParseObject[feed.size()];
int pos = (Integer) v.getTag();
mParseObject[pos] = object;
holder.likes[pos] = holder.mLikes;
mParseObject[pos].put("likes", holder.likes[pos]);
mParseObject[pos].saveInBackground();
holder.feedNumOfLikes.setText(String.valueOf(holder.likes[pos]
));
}
});
}
}
}
});
return view;
}
class ViewHolder {
ImageView feedProfilePic;
TextView feedUsername;
TextView feedNumOfLikes;
TextView feedFeedItem;
TextView feedDate;
TextView feedNumofReplies;
Button feedUpVoteButton;
Button feedDownVoteButton;
Button feedCommentButton;
ListView feedListView;
int likes[] = new int[feed.size()];
int mLikes;
}
}