【问题标题】:Set multiple Button in Android Listview with one listener?使用一个侦听器在 Android Listview 中设置多个按钮?
【发布时间】:2018-04-09 19:47:31
【问题描述】:

我在列表视图中设置了 3 个 ToggleButton。当一个按钮被选中时,另外两个将被取消选中。但它不工作。当我检查一个按钮时,其他两个按钮未从其他行中选中。但我想取消选中该行中的其他两个按钮。

public class MyListAdapter extends ArrayAdapter<Salat>{

    private Activity context;
    private Salat[] salat;

    TextView salatName;
    ToggleButton prayedButton;
    ToggleButton prayedLateButton;
    ToggleButton missedButton;
    final DB db = new DB(context);



    public MyListAdapter(@NonNull Context context, Salat[] salat) {
        super(context, R.layout.salat_listview, salat);
        this.context = (Activity) context;
        this.salat = salat;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listview = inflater.inflate(R.layout.salat_listview, null, true);

        salatName = (TextView) listview.findViewById(R.id.salat_name);
        prayedButton = (ToggleButton) listview.findViewById(R.id.prayed_button);
        prayedLateButton = (ToggleButton) listview.findViewById(R.id.prayed_late_button);
        missedButton = (ToggleButton) listview.findViewById(R.id.missed_button);

        salatName.setText(getItem(position).getName());


        prayedButton.setOnCheckedChangeListener(buttonListener);
        prayedLateButton.setOnCheckedChangeListener(buttonListener);
        missedButton.setOnCheckedChangeListener(buttonListener);



        prayedButton.setTag(position);
        prayedLateButton.setTag(position);
        missedButton.setTag(position);


        return listview;
    }

    CompoundButton.OnCheckedChangeListener buttonListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(compoundButton.getId() == prayedButton.getId()){
                compoundButton.setChecked(b);
                prayedLateButton.setChecked(!b);
                missedButton.setChecked(!b);
            }
            else if(compoundButton.getId() == prayedLateButton.getId()){
                compoundButton.setChecked(b);
                prayedButton.setChecked(!b);
                missedButton.setChecked(!b);
            }
            else if(compoundButton.getId() == missedButton.getId()){
                compoundButton.setChecked(b);
                prayedButton.setChecked(!b);
                prayedLateButton.setChecked(!b);
            }
        }
    };
}

我知道这是错误的方法。但是我怎样才能在 listView 中做到这一点。也许没有办法用getTag() 或位置来解决这个问题。

【问题讨论】:

  • 您的问题不清楚。您要取消选中列表视图中的所有项目还是仅取消选中该特定项目?
  • 没有。当从一行中选中一个按钮时,其他两个按钮将从该行中取消选中。请看截图。
  • 那么你的方法是正确的。 getView() 方法一次只能处理 1 个项目
  • 你能给我一个解决方案或其他方法吗?
  • 试试我给出的答案

标签: android listview android-studio listener


【解决方案1】:

不是最好的解决方案,但应该可以 尝试这个, Android Studio 可能会告诉您将变量设为最终变量。

更新

 public class MyListAdapter extends BaseAdapter{

        private Activity context;
        private Salat[] salat;

        TextView salatName;
        ToggleButton prayedButton;
        ToggleButton prayedLateButton;
        ToggleButton missedButton;
        final DB db = new DB(context);



        public MyListAdapter(@NonNull Context context, Salat[] salat) {
            super(context, R.layout.salat_listview, salat);
            this.context = (Activity) context;
            this.salat = salat;
        }


        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View listview = inflater.inflate(R.layout.salat_listview, null, true);

            salatName = (TextView) listview.findViewById(R.id.salat_name);
            prayedButton = (ToggleButton) listview.findViewById(R.id.prayed_button);
            prayedLateButton = (ToggleButton) listview.findViewById(R.id.prayed_late_button);
            missedButton = (ToggleButton) listview.findViewById(R.id.missed_button);

            salatName.setText(getItem(position).getName());




           prayedButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    prayedButton.setChecked(true);
                    prayedLateButton.setChecked(false);
                    missedButton.setChecked(false);
                }
            });

           prayedLateButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    prayedButton.setChecked(false);
                    prayedLateButton.setChecked(true);
                    missedButton.setChecked(false);
                }
            });

           missedButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    prayedButton.setChecked(false);
                    prayedLateButton.setChecked(false);
                    missedButton.setChecked(true);
                }
            });



            return listview;
        }

    @Override
    public Salat getItem(int position){
       return salat[position];
    }

    @Override
    public int getCount(){
       return salat.length;
    }

【讨论】:

  • 我之前检查过这个。但同样的问题。不适用于同一行。谢谢你的回答。
  • 究竟是什么问题?如果点击了prayButton会发生什么?
  • 当我点击prayButton 时,其他两个按钮未选中,但来自其他行(我的意思是来自其他列表项)。检查屏幕截图
  • 我认为您的问题出在适配器类中。您是否在问题中粘贴了完整的适配器?
  • 它将是 salart.length 而不是 sala.length();但仍然没有解决我的问题。
【解决方案2】:

我找到了一个解决方案。只需选择一组 3 个按钮。

package com.ahmed.aziz.salattracker;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.Date;

public class MyListAdapter extends ArrayAdapter<Salat>{

    private Activity context;
    private Salat[] salat;

    TextView salatName;
    ToggleButton[] prayedButton = new ToggleButton[5];
    ToggleButton[] prayedLateButton = new ToggleButton[5];
    ToggleButton[] missedButton = new ToggleButton[5];
    final DB db = new DB(context);



    public MyListAdapter(@NonNull Context context, Salat[] salat) {
        super(context, R.layout.salat_listview, salat);
        this.context = (Activity) context;
        this.salat = salat;
    }


    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listview = inflater.inflate(R.layout.salat_listview, null, true);

        salatName = (TextView) listview.findViewById(R.id.salat_name);
        prayedButton[position] = (ToggleButton) listview.findViewById(R.id.prayed_button);
        prayedLateButton[position] = (ToggleButton) listview.findViewById(R.id.prayed_late_button);
        missedButton[position] = (ToggleButton) listview.findViewById(R.id.missed_button);

        salatName.setText(getItem(position).getName());


        prayedButton[position].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    prayedLateButton[position].setChecked(!b);
                    missedButton[position].setChecked(!b);
                }

            }
        });
        prayedLateButton[position].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    prayedButton[position].setChecked(!b);
                    missedButton[position].setChecked(!b);
                }
            }
        });
        missedButton[position].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    prayedButton[position].setChecked(!b);
                    prayedLateButton[position].setChecked(!b);
                }

            }
        });


        return listview;
    }

}

【讨论】:

  • 好,你找到了解决办法!
猜你喜欢
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2011-08-24
  • 1970-01-01
  • 2020-08-02
  • 2011-08-21
  • 2011-12-02
相关资源
最近更新 更多