首页 > 解决方案 > 根据 UserType 从 ListView 中隐藏按钮

问题描述

我有一个登录页面,当用户登录时,它会将他重定向到包含图像、按钮和文本视图的 ListViewAct。

重定向后,我创建了一个 if 条件来检查登录的用户类型是管理员还是普通用户

我有三个按钮(添加、编辑、删除)我想对普通用户隐藏这些按钮。

我尝试将这些按钮的可见性设置为GONE,但结果在调试时没有任何反应,并且这些按钮仍然为普通用户显示。

MainAct代码:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button btn_add,btn_edit,btn_delete=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = getIntent();
        String username = intent.getStringExtra("Username");
        String password = intent.getStringExtra("Password");

        btn_add = findViewById(R.id.btn_add);
        btn_edit = findViewById(R.id.btn_edit);
        btn_delete = findViewById(R.id.btn_delete);


        if(username.equals("admin") && password.equals("admin")){

            Log.d(TAG, "onCreate: Started.");
            ListView listView = (ListView) findViewById(R.id.li_view);

            ArrayList<PersonInfo> students = new ArrayList<>();

            students.add(new PersonInfo(android.R.drawable.btn_star, "test", "03/27/1998"));

            StudentsListAdapter studentsListAdapter = new StudentsListAdapter(
                    this, R.layout.adapter_view_layout, students);
            listView.setAdapter(studentsListAdapter);
        }

            else if (username.equals("user") && password.equals("user"))
            {
                ListView listView = (ListView) findViewById(R.id.li_view);

                ArrayList<PersonInfo> students = new ArrayList<>();

                students.add(new PersonInfo(android.R.drawable.btn_star, "test", "03/27/1998"));

                StudentsListAdapter studentsListAdapter = new StudentsListAdapter(
                        this, R.layout.adapter_view_layout, students);
                listView.setAdapter(studentsListAdapter);

                if (btn_add != null && btn_edit !=null && btn_delete !=null){
                btn_add.setVisibility(View.GONE);
                btn_edit.setVisibility(View.GONE);
                btn_delete.setVisibility(View.GONE);
            }


        }
    }
}

列表适配器代码:

  public class StudentsListAdapter extends ArrayAdapter<PersonInfo> {
    private Context contxt;
    private int rsrc;
    private List<PersonInfo> persons;


    public StudentsListAdapter( Context context, int resource, List<PersonInfo> _persons) {
        super(context, resource, _persons);
        contxt = context;
        rsrc = resource;
        persons=_persons;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(contxt);
        View view = inflater.inflate(rsrc, null,false);

        ImageView imageView = view.findViewById(R.id.imgP);
        TextView pName = view.findViewById(R.id.txtView2);
        TextView pBirthday = view.findViewById(R.id.txtView3);

        PersonInfo p = persons.get(position);

        imageView.setImageDrawable(contxt.getResources().getDrawable(p.getImage()));
        pBirthday.setText(p.getBirthday());
        pName.setText(p.getName());

        return view;
    }
}

列表视图 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:padding="15dp"
    android:layout_height="match_parent"
    android:weightSum="100" >

    <ImageView
        android:id="@+id/imgP"
        android:layout_width="399dp"
        android:layout_height="150dp"
        android:layout_weight="66.6" />

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="33.3">

        <TextView
            android:gravity="center"
            android:text="TextView2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/txtView2"/>

        <TextView
            android:id="@+id/txtView3"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/txtView2"
            android:gravity="center"
            android:text="TextView3" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <Button
            android:id="@+id/btn_add"
            android:layout_width="69dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtView3"
           android:layout_marginRight="20dp"
            android:text="ADD" />

        <Button
            android:layout_width="69dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn_add"
            android:layout_below="@id/txtView3"
            android:id="@+id/btn_edit"
            android:text="edit"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn_edit"
            android:layout_below="@+id/txtView3"
            android:id="@+id/btn_delete"
            android:text="Delete"/>
    </RelativeLayout>

    </LinearLayout>

主要 XML:

        <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ListView
        android:id="@+id/li_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        tools:layout_algignParentStart="true" />
</androidx.constraintlayout.widget.ConstraintLayout>

个人信息类:

        public class PersonInfo {
    private int image;
    private String name;
    private String birthday;

    public PersonInfo(int image, String name, String birthday) {
        this.image = image;
        this.name = name;
        this.birthday = birthday;
    }

    public String getName(){
        return name;
    }
    public int getImage() {
        return image;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
}

我没有对 ListAdapter 上的按钮进行任何更改。

我做错了什么?任何帮助将不胜感激。

谢谢!

标签: androidlistviewbuttonhide

解决方案


根据你xmlbuttons在里面ListView的项目。所以,你必须在里面处理它adapter而不是在fragment. 检查以下:

public class StudentsListAdapter extends ArrayAdapter<PersonInfo> {
    private Context contxt;
    private int rsrc;
    private List<PersonInfo> persons;
    private boolean isAdmin;


    public StudentsListAdapter( Context context, int resource, List<PersonInfo> _persons, boolean _isAadmin) {
        super(context, resource, _persons);
        contxt = context;
        rsrc = resource;
        persons=_persons;
        isAdmin = _isAadmin;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(contxt);
        View view = inflater.inflate(rsrc, null,false);

        ImageView imageView = view.findViewById(R.id.imgP);
        TextView pName = view.findViewById(R.id.txtView2);
        TextView pBirthday = view.findViewById(R.id.txtView3);

        Button btn_add = view.findViewById(R.id.btn_add);
        Button btn_edit = view.findViewById(R.id.btn_edit);
        Button btn_delete = view.findViewById(R.id.btn_delete);

        if(isAdmin) {
            btn_add.setVisibility(View.VISIBLE);
            btn_edit.setVisibility(View.VISIBLE);
            btn_delete.setVisibility(View.VISIBLE);
        } else {
            btn_add.setVisibility(View.GONE);
            btn_edit.setVisibility(View.GONE);
            btn_delete.setVisibility(View.GONE);
        }

        PersonInfo p = persons.get(position);

        imageView.setImageDrawable(contxt.getResources().getDrawable(p.getImage()));
        pBirthday.setText(p.getBirthday());
        pName.setText(p.getName());

        return view;
    }
}

然后修改你fragment的调用adapter如下:

ListView listView = (ListView) findViewById(R.id.li_view);
ArrayList<PersonInfo> students = new ArrayList<>();
students.add(new PersonInfo(android.R.drawable.btn_star, "test", "03/27/1998"));

if(username.equals("admin") && password.equals("admin")){
    StudentsListAdapter studentsListAdapter = new StudentsListAdapter(
            this, R.layout.adapter_view_layout, students, true); // true -> admin user
} else if (username.equals("user") && password.equals("user")) {
    StudentsListAdapter studentsListAdapter = new StudentsListAdapter(
            this, R.layout.adapter_view_layout, students, false); // false -> normal user
}

listView.setAdapter(studentsListAdapter);

推荐阅读