Я создал, и приложение с представлением поиска поставляется с RecyclerView с флажком. Когда я установил флажок, поиск флажок становится снят. Как я могу решить проблему? Состояние флажка при поиске состояния флажок все еще отмечен. Спасибо коллегам-программистам
Снимок экрана 1: Вид приложения
При проверке элемента в представлении переработчика: Флажок выбора элемента
Когда япри поиске предмета состояние флажка меняется, он становится непроверенным: Поиск предмета становится непроверенным
public class SelectBranch : AppCompatActivity
{
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
List<Info_LoadBranch> loadBranch = new List<Info_LoadBranch>();
BranchAdapter holder;
Android.Widget.SearchView searchViewBranch;
List<string> SelectedBranchCode = new List<string>();
Button BtnProceed;
string dbPath;
SQLiteConnection db;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_select);
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView1);
mLayoutManager = new LinearLayoutManager(this);
BtnProceed = FindViewById<Button>(Resource.Id.btnProceed);
BtnProceed.Click += BtnProceed_Click;
LoadData();
searchViewBranch = FindViewById<Android.Widget.SearchView>(Resource.Id.searchView1);
searchViewBranch.QueryTextChange += SearchViewBranch_QueryTextChange;
GlobalVariable.SelectedBranchCode = new List<string>();
// Create your application here
}
private void SearchViewBranch_QueryTextChange(object sender, Android.Widget.SearchView.QueryTextChangeEventArgs e)
{
var adapter = new BranchAdapter(this, FindInsideList(e.NewText));
holder.NotifyDataSetChanged();
mRecyclerView.SetAdapter(adapter);
}
private List<Info_LoadBranch> FindInsideList(string text)
{
if (text != null && text.Length > 0)
{
List<Info_LoadBranch> filteredList = new List<Info_LoadBranch>();
foreach (var itm in loadBranch)
{
if (itm != null)
{
text = text.ToLower();
if (itm.BranchName != null ? itm.BranchName.ToLower().Contains(text) : false)
{
filteredList.Add(itm);
}
}
}
return filteredList;
}
return loadBranch;
}
private void BtnProceed_Click(object sender, EventArgs e)
{
if (GlobalVariable.SelectedBranchCode.Count > 0)
{
var intent = new Intent(this, typeof(Inquiry));
StartActivity(intent);
this.Finish();
}
else
{
DialogHelper.ShowAlertMessage(this, "Exception", "Please select Branch");
return;
}
}
private void LoadData()
{
dbPath = Path.Combine(BaseVariable.DEFAULT_APP_SETTINGS_DB_PATH, BaseVariable.DEFAULT_SQLITE_DB_COMPANY);
db = new SQLiteConnection(dbPath);
loadBranch = db.Query<Info_LoadBranch>("Select BranchCode, BranchName from BranchDB");
holder = new BranchAdapter(this, loadBranch);
//ListViewSelectedBranch.Adapter = output;
mRecyclerView.SetLayoutManager(mLayoutManager);
holder.NotifyDataSetChanged();
mRecyclerView.SetAdapter(holder);
}
public class BranchAdapter : RecyclerView.Adapter
{
public event EventHandler<int> ItemClick;
AppCompatActivity activity;
private List<Info_LoadBranch> branchesList;
public BranchAdapter(AppCompatActivity activity, List<Info_LoadBranch> branchesList)
{
//this.items = items;
this.activity = activity;
this.branchesList = branchesList;
//this.branchesList = branchesList.OrderBy(s => s.BranchName).ToList();
}
public override int ItemCount
{
get
{
return branchesList.Count;
}
}
public Filter Filter { get; private set; }
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.adapter_selectbranch, parent, false);
//itemView.SetBackgroundResource(Resource.Drawable.listItem1);
PickingViewHolder vh = new PickingViewHolder(itemView);
return vh;
}
private Dictionary<int, bool> map = new Dictionary<int, bool>();
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var item = branchesList[position];
PickingViewHolder vh = holder as PickingViewHolder;
vh.BranchCodeCheckBox.SetOnCheckedChangeListener(new MyListener(map, position));
vh.LblBranch.Text = item.BranchName;
vh.BranchCodeCheckBox.Tag = item.BranchCode;
if (map != null && map.ContainsKey(position))
{
vh.BranchCodeCheckBox.Checked = true;
// vh.BranchCodeCheckBox.Checked = branchesList[position].IsChecked;
}
else
{
vh.BranchCodeCheckBox.Checked = false;
}
//vh.BranchCodeCheckBox.Checked = branchesList[position].IsChecked;
//vh.BranchCodeCheckBox.SetOnCheckedChangeListener(new CheckedChangeListener(this.activity));
//if (map != null && map.ContainsKey(position))
//{
// vh.BranchCodeCheckBox.Checked = true;
//}
//else
//{
// vh.BranchCodeCheckBox.Checked = false;
//}
//if (position != 0)
//{
// var linearLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FillParent,
// ViewGroup.LayoutParams.WrapContent);
// linearLayoutParams.SetMargins(0, 10, 0, 0);
// vh.MainLinearLayout.LayoutParameters = linearLayoutParams;
//}
//vh.Image.SetImageResource (mPhotoAlbum[position].PhotoID);
}
class MyListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
{
public Dictionary<int, bool> map;
public int mPosotion;
string name;
public MyListener(Dictionary<int, bool> map, int position)
{
this.map = map;
mPosotion = position;
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
if (isChecked == true)
{
if (!map.ContainsKey(mPosotion))
{
map.Add(mPosotion, true);
name = (string)buttonView.Tag;
GlobalVariable.SelectedBranchCode.Add(name);
}
}
else
{
map.Remove(mPosotion);
foreach (var a in GlobalVariable.SelectedBranchCode)
{
GlobalVariable.SelectedBranchCode.Remove(name);
break;
}
}
}
}
public class PickingViewHolder : RecyclerView.ViewHolder
{
public LinearLayout MainLinearLayout { get; set; }
public TextView LblBranch { get; set; }
public CheckBox BranchCodeCheckBox { get; set; }
public bool IsChecked { get; set; }
public PickingViewHolder(View itemView):base(itemView)
{
//Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
MainLinearLayout = itemView.FindViewById<LinearLayout>(Resource.Id.linearLayout1);
LblBranch = itemView.FindViewById<TextView>(Resource.Id.textViewBranch);
BranchCodeCheckBox = itemView.FindViewById<CheckBox>(Resource.Id.checkBoxSelect);
// Detect user clicks on the item view and report which item
// was clicked (by layout position) to the listener:
// itemView.Click += (sender, e) => listener(base.LayoutPosition);
BranchCodeCheckBox.Click += delegate
{
if (BranchCodeCheckBox.Checked)
{
IsChecked = true;
}
else
{
IsChecked = false;
}
};
}
}
void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
}
public class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
{
public Activity activity;
string name;
public CheckedChangeListener(Activity activity)
{
this.activity = activity;
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
if (isChecked)
{
name = (string)buttonView.Tag;
GlobalVariable.SelectedBranchCode.Add(name);
}
else if (!isChecked)
{
foreach (var a in GlobalVariable.SelectedBranchCode)
{
GlobalVariable.SelectedBranchCode.Remove(name);
break;
}
}
}
}
}