ListView的片段SearchView

时间:2019-03-10 08:09:18

标签: android xml search fragment searchview

试图将搜索功能添加到片段中的ListView中。我有一个教程中的ListView适配器类,该类添加了一个图像(该项目的首字母作为图标)。当我在搜索字段中键入内容时,没有任何反应。 以下是我出现问题的简化片段代码(我认为):

private View view;
private ListView instrumentListView;
private SearchView searchinstrumentText;
private String[] instrumentItems={"1", "2", "3", "4", "5"};
private ArrayList<String> instrumentList;
private ArrayAdapter instrumentAdapter;

public InstrumentListFragment(){
    // Required empty public constructor
}

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


    View view = inflater.inflate(R.layout.fragment_instrumentlist, container, 
false);
    final ListView instrumentView = (ListView) 
view.findViewById(R.id.instrumentListView);
    SearchView searchinstrumentText=(SearchView) 
view.findViewById(R.id.searchinstrumentText);


    //The following is a custom adapter!
    final ArrayList<String> instrumentList = new ArrayList<String>();
    instrumentList.addAll( Arrays.asList(instrumentItems));
    final ArrayAdapter instrumentAdapter = new ListAdapter(getActivity(), 
R.layout.custom_listview, instrumentList);
    instrumentView.setAdapter(instrumentAdapter);


    //SEARCH
    searchinstrumentText.setQueryHint("Search");
    searchinstrumentText.setOnQueryTextListener(new 
SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String txt) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String txt) {
            instrumentAdapter.getFilter().filter(txt);
            return false;
        }
    });
    return view;
}

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".InstrumentListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_fragment">

<SearchView
    android:id="@+id/searchinstrumentText"
    style="@style/TextEdit"
    android:ems="10"
    android:hint="Search" />

<ListView
    android:id="@+id/instrumentListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchinstrumentText"
    android:layout_marginTop="1dp" />


</RelativeLayout>

这是Adapter类:

public class ListAdapter extends ArrayAdapter<String> {
Context context;
List<String> instrumentList;
List<String> methodsList;


public ListAdapter(@NonNull Context context, @LayoutRes int resource, 
List<String> list) {
    super(context, resource,list);

    this.context = context;
    this.instrumentList = list;
    this.methodsList = list;

}

@Override
public int getCount() {
    return instrumentList.size();
}

@Nullable
@Override
public String getItem(int position) {
    return instrumentList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {

    ViewHolder viewHolder;

    LayoutInflater inflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null){

        convertView = 
inflater.inflate(R.layout.custom_listview,parent,false);

        viewHolder = new ViewHolder(convertView);

        convertView.setTag(viewHolder);
    }else{

        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.textView.setText(getItem(position));

    String letters = String.valueOf(getItem(position).charAt(0));

    ColorGenerator colorGenerator = ColorGenerator.MATERIAL;

    int color = colorGenerator.getColor(getItem(position));

    TextDrawable textDrawable = TextDrawable.builder()
            .buildRound(letters,color);

    viewHolder.imageView.setImageDrawable(textDrawable);
    return convertView;
 }

 public class ViewHolder{
    ImageView imageView;
    TextView textView;

    public ViewHolder(View iteView) {
        imageView = iteView.findViewById(R.id.image_view);
        textView = iteView.findViewById(R.id.text_view);
    }
}
}

我真的很感谢一些指导,因为我是应用程序开发的新手。 先感谢您, 爱德华

1 个答案:

答案 0 :(得分:0)

结果是,在“ return convertView;}”之后,我在适配器类中缺少部分代码:

    public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    instrumentList.clear();
    if (charText.length() == 0) {
        instrumentList.addAll(searchList);
    } else {
        for (String s : searchList) {
            if (s.toLowerCase(Locale.getDefault()).contains(charText)) {
                instrumentList.add(s);
            }
        }
    }
    notifyDataSetChanged();
    }

,并且不得不将片段中的搜索方法修改为:

//SEARCH
searchinstrumentText.setQueryHint("Search");
searchinstrumentText.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String txt) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String txt) {
        if (TextUtils.isEmpty(txt)){
            ((ListAdapter) ListAdapter).filter("");
            instrumentView.clearTextFilter();
        }else{
            ((ListAdapter) ListAdapter).filter(txt);
        }
        return true;
    }
});

也许这会帮助有类似问题的人。