简体   繁体   English

Android Listview-单击后更改项目按钮文本

[英]Android Listview - Change Item Button Text After Clicking

I am trying to convert my app to android version, and I am a bit new in android. 我正在尝试将我的应用程序转换为android版本,而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    
                }
            });

you need to change the value in myItemList so that next time when view load, the value from list set as text on profile button comes which ix followed ie you need to update the list on click of follow button and notify the ListView . 您需要更改myItemList的值,以便下次加载视图时,配置文件按钮上设置为文本的列表中的值出现在ix之后,即您需要在单击跟随按钮时更新列表并通知ListView

followBtn.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        System.out.println(myItemList.get(position));
        myItemList.get(position).set("followed");
        profileBtn.setText("followed");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM