我有一个自定义列表视图适配器。当我单击一行中的按钮时,我想要更改同一行中的文本。但是,我收到了一个错误。当我单击第0行中的按钮时,它会改变第1行或第2行中的文本(随机)。另外,我是否需要使用setTag();
和getTag();
如何在第0行中获取按钮,以便在单击时更改第0行中的文本。
自定义列表行代码
public CustomFeedListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {
mContext = context;
feed = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
position = i;
View mView = view;
if (view == null) {
mView = inflater.inflate(R.layout.feed_list_row, viewGroup, false);
feedNumOfLikes = (TextView) mView.findViewById(R.id.feedNumofLikes);
feedUpVoteButton = (Button) mView.findViewById(R.id.feedUpVoteButton);
feedDownVoteButton = (Button) mView.findViewById(R.id.feedDownVoteButton);
HashMap<String, String> mFeed = new HashMap<>();
mFeed = feed.get(i);
// feedUpVoteButton.setTag(i);
// feedNumOfLikes.setTag(i);
feedUpVoteButton.setOnClickListener(this);
}
return mView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.feedUpVoteButton:
//v.getTag();
likes +=1;
Log.i("yo", String.valueOf(position));
feedNumOfLikes.setText(String.valueOf(likes));
break;
case R.id.feedDownVoteButton:
// Do stuff when imageView1 is clicked...
default:
break;
}
}
}