我有一个自定义列表视图,其中包含单选按钮和文本视图。当我选择1个单选按钮时,有多个单选按钮被选中。接下来的4个索引单选按钮也都被选择了。我想一次只选择一个单选按钮。
这是适配器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Definition.Dto;
using static Android.Widget.CompoundButton;
namespace SGDDPortal.Android.Model
{
public class ViewHolder : Java.Lang.Object
{
public TextView LabelText { get; set; }
public RadioButton ListRadioButton { get; set; }
}
public class DepartmentListAdapter : BaseAdapter<DepartmentDto>, IOnCheckedChangeListener
{
private Activity activity;
List<DepartmentDto> Departments;
int selectedIndex = -1;
public DepartmentListAdapter(Activity activity, List<DepartmentDto> Departments)
{
this.activity = activity;
this.Departments = Departments;
}
public override DepartmentDto this[int position] => Departments[position];
public override int Count => Departments.Count;
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.DepartmentPopUpListViewRow, parent, false);
var btnRadio = view.FindViewById<RadioButton>(Resource.Id.SelectedDepartment);
btnRadio.SetOnCheckedChangeListener(null);
btnRadio.Tag = position;
btnRadio.Checked = Departments[position].Checked;
btnRadio.Text = Departments[position].Afdeling_Txt;
btnRadio.SetOnCheckedChangeListener(this);
return view;
}
private void BtnRadio_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
throw new NotImplementedException();
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
int position = (int)buttonView.Tag;
if (isChecked)
{
foreach (DepartmentDto model in Departments)
{
if (model != Departments[position])
{
model.Checked = false;
}
else
{
model.Checked = true;
}
}
NotifyDataSetChanged();
}
}
}
}
这是Windowpopup代码
private void DepartmentPicker_Click(object sender, EventArgs e)
{
//ViewModelInstances.DepartmentVieModel.PopUpCommand.CanExecute(this);
ButtonNext.Visibility = ViewStates.Invisible;
GetListView.ChoiceMode=ListView.ChoiceModeSingle;
GetListView.Adapter = new DepartmentListAdapter(this, Departments);
bool focusable = true;
int width = 350;//LinearLayout.LayoutParams.WrapContent;
int height = 450;//LinearLayout.LayoutParams.WrapContent;
// listView = _PopUpView.FindViewById<ListView>(Resource.Id.Departmentlistview);
PopupWindow popupWindow = new PopupWindow(_PopUpView, width, height, focusable);
popupWindow.ContentView = _PopUpView;
popupWindow.ShowAtLocation(_PopUpView, GravityFlags.CenterVertical, 0, 0);
popupWindow.Focusable = false;
popupWindow.Touchable = true;
}
这是Xamal for WindowPopUp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:weightSum="100">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_weight="10"
android:layout_height="40dp">
<TextView
android:text="Vælg din afdeling"
android:textSize="20sp"
android:textColor="#FF222222"
android:paddingLeft="30dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/textView1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="50">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:choiceMode="multipleChoice"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Departmentlistview" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/btnAddExpense"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:textColor="#61222222"
android:background="@null"
android:text="Annuller"
android:layout_marginLeft="20dp"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
<Button
android:id="@+id/btnok"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:textColor="#FFF62F5E"
android:text="Gem"
android:background="@null"
android:layout_marginLeft="1dp"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
</LinearLayout>
</LinearLayout>
包含单选按钮和文本的布局xamal,在Api调用时返回文本并与此编辑文本绑定
<?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:layout_height="match_parent"
android:background="@android:color/background_light"
android:weightSum="100">
<RadioGroup
android:id="@+id/radioGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="60dp"
android:checked="false"
android:id="@+id/SelectedDepartment" />
</RadioGroup>
<TextView
android:text="303 - Lorem ipsum"
android:layout_weight="50"
android:layout_marginTop="20dp"
android:textColor="#FF222222"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="@+id/SelectDepartmentName" />
</LinearLayout>
此布局包含显示数据的ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:descendantFocusability="blocksDescendants"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:weightSum="100">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_weight="10"
android:layout_height="40dp">
<TextView
android:text="Vælg din afdeling"
android:textSize="20sp"
android:textColor="#FF222222"
android:paddingLeft="30dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/textView1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="50">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:choiceMode="singleChoice"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Departmentlistview" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/btnAddExpense"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:textColor="#61222222"
android:background="@null"
android:text="Annuller"
android:layout_marginLeft="20dp"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
<Button
android:id="@+id/btnok"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:textColor="#FFF62F5E"
android:text="Gem"
android:background="@null"
android:layout_marginLeft="1dp"
android:layout_gravity="right"
android:layout_marginRight="15dp" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
您已将单选按钮放在每个列表视图项目上,它们彼此之间没有关联,因为它们不在同一RadioGroup
中。您必须在模型中定义一个属性,以指出应检查的无线电。
我模拟您的模型,例如:
public class DepartmentDto
{
public string Afdeling_Txt { set; get; }
public bool Checked { set; get; }
}
并且应该调整您的GetView事件:
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.DepartmentPopUpListViewRow, parent, false);
// var DepartmentpopUp = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.DepartmentPopUpListViewRow, parent, false);
var btnRadio = view.FindViewById<RadioButton>(Resource.Id.SelectedDepartment);
btnRadio.SetOnCheckedChangeListener(null);
btnRadio.Tag = position;
btnRadio.Checked = Departments[position].Checked;
btnRadio.SetOnCheckedChangeListener(this);
view.FindViewById<TextView>(Resource.Id.SelectDepartmentName).Text = Departments[position].Afdeling_Txt;
return view;
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
int position = (int)buttonView.Tag;
if (isChecked)
{
foreach (DepartmentDto model in Departments)
{
if (model != Departments[position])
{
model.Checked = false;
}
else
{
model.Checked = true;
}
}
NotifyDataSetChanged();
}
}
不要忘记在您的适配器IOnCheckedChangeListener
中实现public class DepartmentListAdapter : BaseAdapter<DepartmentDto>, IOnCheckedChangeListener
接口。
最后,适配器的构造函数可能是这样的:
List<DepartmentDto> list = new List<DepartmentDto>();
for (int i = 0; i<10; i++)
{
list.Add(new DepartmentDto { Checked = false, Afdeling_Txt = "item" + i });
}
DepartmentListAdapter customAdapter = new DepartmentListAdapter(this, list);
Departmentlistview.Adapter = customAdapter;
答案 1 :(得分:0)
现在,您的单选按钮是不同布局的一部分。由于您未使用Xamarin Forms,因此Android建议使用的方法是将所有单选按钮放在一个单选组中,如下所示。
var employeeReleasedAllocatedBonus =
(from br in _context.BonusReleases
join emp in _context.Employees
on new
{
br.EmployeeID,
empID = br.EmployeeID
} equals new
{
emp.EmployeeID,
empID = eid
}
join job in _context.Jobs on br.JobID equals job.JobID
join bonus in _context.Bonus
on new
{
br.JobID,
empID = br.EmployeeID
}
equals new
{
bonus.JobID,
empID = bonus.EmployeeID
}
select new
{
AllocatedToEmployee = br.Amount
,AllocatedPercentage = bonus.Amount * 100
,JobNumber = job.JobNumber
,JobDescription = job.JobDescription
})
.GroupBy(g => new {g.JobNumber, g.JobDescription, g.AllocatedPercentage})
.Select(t => new EmployeeAllocatedReleasedBonusViewModel
{
JobNumber = t.Key.JobNumber,
JobDescription = t.Key.JobDescription,
AllocatedPercentage = t.Key.AllocatedPercentage,
AllocatedToEmployee = t.Sum(ae => ae.AllocatedToEmployee)
});