在我的GetView()
我的BaseAdapter
類的方法...我有一個視圖持有者。當我點擊holder.feedUpVotebutton
feed.numOFLikes
Textview會增加。問題是,當我向下滾動並向上滾動時,該值將恢復爲其原始值。我拒絕撥打notifydatasetChanged();
,因爲我沒有添加任何東西或從列表中刪除任何東西。如何在BaseAdapter中設置Viewholder文本?
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
likes = new int[GlobalFeedTab.arrayFeedList.size()];
ViewHolder holder;
if (view == null) {
position = i;
view = inflater.inflate(R.layout.feed_list_row, viewGroup, false);
holder = new ViewHolder();
holder.feedNumOfLikes = (TextView) view.findViewById(R.id.feedNumofLikes);
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
view.setTag(holder);
}
else if(view != null){
position = i;
holder = (ViewHolder) view.getTag();
}
HashMap<String, String> mFeed = new HashMap<>(); //Hashmap of data that I get from the MainActivity
mFeed = GlobalFeedTab.arrayFeedList.get(position);
holder.feedNumOfLikes.setText(mFeed.get("likes"));
holder.feedUpVoteButton.setTag(position);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
ParseObject parseObFeed = objects.get(pos);
likes[pos] += 1;
parseObFeed.put("likes", likes[pos]);
holder.feedNumOfLikes.setText(String.valueOf(parseObFeed.getInt("likes")
)); //This is where I set the Text
parseObFeed.saveInBackground();
}
});
}
class ViewHolder {
TextView feedNumOfLikes;
Button feedUpVoteButton;
}
爲什麼不使用ArrayAdapter或RecyclerView.Adapter? –
'GlobalFeedTab.arrayFeedList'是我的ArrayAdapter。 @ cricket_007 – grant
我不這麼認爲。這是'ArrayList' –