gpt4 book ai didi

android - ListView 中的搜索过滤器未按预期工作

转载 作者:行者123 更新时间:2023-11-30 02:40:01 26 4
gpt4 key购买 nike

我们有一个 ListView fragment ,其中包含一个 ListView 、编辑文本(用于搜索)和复选框(全选)。如前所述,我们使用编辑文本从 ListView 中搜索值。

问题:

  1. 我们有显示 10 个项目的 ListView ( ListView 按字母顺序排序)。
  2. 当我们在编辑文本中键入“W”时,会显示两个值,即 WF 和 WH。
  3. 选择的是 WF(显示的第一项)。
  4. 清除编辑文本中的字符后,我们现在显示 ListView 的完整列表。
  5. 但现在选择的是列表 AHMD 中的第一项。 (我们打算选择WF)

基本上, ListView 中数据的位置在我们执行搜索后不会刷新。

代码:

 public static class ListViewFragment extends Fragment {

private ListView myListView;
private EditText editsearch;
private CheckBox selectAll;
private String SELECTED_FILTER;
private ArrayList<String> SelectedFilter1;
private ArrayList<String> checkekprev=new ArrayList<String>();
private ArrayList<String> CheckedFitlers=new ArrayList<String>();

ArrayAdapter<String> adapter_list;

OnCheckedItemsLister onCheckedItemsLister;

public ListViewFragment() {
// Empty constructor required for fragment subclasses
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onCheckedItemsLister = (OnCheckedItemsLister) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
SelectedFilter1=getArguments().getStringArrayList("ArrayList");
SELECTED_FILTER=getArguments().getString("SELECTED_FILTER");
checkekprev=getArguments().getStringArrayList("checkedprevlist");




if(checkekprev.size()==SelectedFilter1.size())
selectAll.setChecked(true);
else
selectAll.setChecked(false);

adapter_list = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_multiple_choice,SelectedFilter1);

adapter_list.notifyDataSetChanged();

myListView.setAdapter(adapter_list);

myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

myListView.setTextFilterEnabled(true);

try{
if(checkekprev.isEmpty()==false){
Log.i("Adapter set", "Yes");
for(int j=0;j<checkekprev.size();j++){
Log.i("Adapter set", "Yes:"+j);
int pos=adapter_list.getPosition(checkekprev.get(j));
Log.i("Adapter set", "Pos:"+pos);
myListView.setItemChecked(pos,true);
}

}
}catch(NullPointerException e){
Log.i("Null set", "Yes");
}

editsearch.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter_list.getFilter().filter(text);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});

selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

Log.i("CheckBOX","Is Checked:"+isChecked);
if(isChecked)
{
for ( int i=0; i< myListView.getCount(); i++ )
myListView.setItemChecked(i, true);

}else
{
for ( int i=0; i< myListView.getCount(); i++ )
myListView.setItemChecked(i, false);
}
SparseBooleanArray checked = myListView.getCheckedItemPositions();
CheckedFitlers.clear();
for(int i=0;i<=checked.size();i++){
if (checked.get(i)){
CheckedFitlers.add(SelectedFilter1.get(i));
}
}
onCheckedItemsLister.someEvent(CheckedFitlers,SELECTED_FILTER);
}
}
);

myListView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,int position, long id){
setItemChecked(position);
onCheckedItemsLister.someEvent(CheckedFitlers,SELECTED_FILTER);

}
});

}

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

if(savedInstanceState!=null){
checkekprev=savedInstanceState.getStringArrayList("Checked_Values");
}
}

public void setItemChecked(int position){

//if(SelectedFilter1.get(position).)

//int len = myListView.getCount();
SparseBooleanArray checked = myListView.getCheckedItemPositions();

for(int i=0;i<=checked.size();i++){
Log.i("Check Sparse Array","Check Sparse Array: "+checked.get(i));
}

//for (int i = 0; i < len; i++){
Log.i("SelectedFilter1.get(position)","SelectedFilter1.get(position):::"+SelectedFilter1.get(position));

Log.i("checked.get(position))","checked.get(position)):::"+checked.get(position));

for (int i=0; i<CheckedFitlers.size();i++){
Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.get(i));
}
for (int i=0; i<checkekprev.size();i++){
Log.i("checkekprev","checkekprev:::" + checkekprev.get(i));
}
Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.size());
Log.i("checkekprev","checkekprev:::" + checkekprev.size());

if(CheckedFitlers.size() == 0 && checkekprev.size() != 0){
CheckedFitlers = checkekprev;
}

Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.size());

if (checked.get(position)) {
Log.i("Adding","Adding::"+checked.get(position));

CheckedFitlers.add(SelectedFilter1.get(position));
for(int i=0;i<checkekprev.size();i++){
//Log.i("Checked Filters FOr loop","Checked Filters:"+CheckedFitlers.get(i));
}
} else
{
Log.i("Removing","Removing::"+checked.get(position)+":"+SelectedFilter1.get(position));
CheckedFitlers.remove(SelectedFilter1.get(position));
for(int i=0;i<checkekprev.size();i++){
//Log.i("Checked Filters for loop","Checked Filters:"+CheckedFitlers.get(i));
}
}
Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.size());

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);

myListView=(ListView) rootView.findViewById(R.id.listView_brand);

editsearch = (EditText) rootView.findViewById(R.id.search);

selectAll=(CheckBox) rootView.findViewById(R.id.checkBox1);

selectAll.setFocusable(false);

return rootView;
}

public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);

Log.i("savedInstanceState in listfragment","savedInstanceState in listfragment");
//onCheckedItemsLister.someEvent(CheckedFitlers,SELECTED_FILTER);
onCheckedItemsLister.getSelectedFilter(SELECTED_FILTER);

}

}

最佳答案

当您在编辑文本中键入“W”时,我们会显示两个值,即 WF 和 WH。为 WF 做出选择(显示的第一个项目)。

但是您已经对 ListView 的第一行执行了操作,现在 ListView 的复选框被选中,当清除编辑文本中的字符时,您会看到 ListView 的完整列表,其中第一个框被选中。这是正确的。

如果您想避免此问题,请创建自定义适配器并覆盖 getfilter 方法。

否则,创建您自己的逻辑并调用 listView.setItemChecked( stored positions, true/false);在 ontextchanged 中。

关于android - ListView 中的搜索过滤器未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25926248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com