我在listview中设置了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()
或位置来解决这个问题。
答案 0 :(得分:0)
不是最好的解决方案,但应该有效 试试这个, Android工作室可能会告诉您将变量设为最终。
<强>已更新强>
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;
}
答案 1 :(得分:0)
我找到了一个解决方案。只需要一组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;
}
}