Android Listview-单击后更改项目按钮文本 [英] Android Listview - Change Item Button Text After Clicking
问题描述
我正在尝试将我的应用程序转换为android版本,而android有点新了.我有一个列表视图,其中的项目包括按钮.这是一个用户列表,您可以跟随每个用户,当单击按钮时,仅在包括按钮的项目中,按钮文本应变成跟随".检索列表工作正常,但是使用下面的代码,按钮文本没有更改.我怎样才能做到这一点?非常感谢.
I am trying to convert my app to android version, and I am a bit new in android. I have a list view in which the items include buttons. It is a user list and you can follow each user, when the button is clicked, only in the item including the button, the button text should turn in to "followed". Retrieving the list works fine, but with my below code the button text is not changing. How can I make this happen? Thank you very much.
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter() {
super(getActivity().getApplicationContext(), R.layout.fragment_users_cell, myItemList);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View cellView = convertView;
if (cellView == null){
cellView = getActivity().getLayoutInflater().inflate(R.layout.fragment_users_cell, parent, false);
}
profileBtn = (Button) cellView.findViewById(R.id.fragment_users_cell_profileBtn);
followBtn = (Button) cellView.findViewById(R.id.fragment_users_cell_followBtn);
profileBtn.setText(myItemList.get(position));
profileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
System.out.println(position);
}
});
followBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
profileBtn.setText("followed");
}
});
return cellView;
}
}
推荐答案
在进行更改后,您必须更新数据集并引用列表,以使其反映最新的更改.根据您的情况,文本会更改.
You have to update your dataset and refersh the list after you make changes so that it reflects the latest changes. In your case the text changes.
followBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
profileBtn.setText("followed");
myItemList.add(position, "followed");//Change your dataset
notifyDataSetChanged(); //And refresh the adapter
}
});
这篇关于Android Listview-单击后更改项目按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!