我创建了一个自定义ListView,并试图通过EditText创建一个自定义过滤器/搜索过滤器。
DictionaryFragment:
EditText editText;
ListView listView;
MyAdapter adapter;
MyAdapter myAdapter;
ArrayList<SingleRow> mylist;
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myAdapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_dictionary, container, false);
listView = view.findViewById(R.id.dictionaryList);
editText = (EditText)view.findViewById(R.id.edit_search);
editText.addTextChangedListener(textWatcher);
mylist = new ArrayList<>();
SingleRow singleRow;
for (int i = 0;i<mFind.length;i++){
singleRow = new SingleRow(mTitle[i],mDescriprion[i],images[i]);
mylist.add(singleRow);
}
myAdapter = new MyAdapter(getActivity(),mylist);
listView.setAdapter(myAdapter);
return view;
}
public class MyAdapter extends BaseAdapter implements Filterable {
Context c;
ArrayList<SingleRow> originalArray,tempArray;
CustomFilter cs;
public MyAdapter(Context c, ArrayList<SingleRow> originalArray){
this.c = c;
this.originalArray = originalArray;
this.tempArray = originalArray;
}
@Override
public Object getItem(int i) {
return originalArray.get(i);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row,null);
ImageView images = row.findViewById(R.id.imageViewKanji);
TextView myTitle = row.findViewById(R.id.textView1);
TextView myDescription = row.findViewById(R.id.textView2);
myTitle.setText(originalArray.get(i).getTitle());
myDescription.setText(originalArray.get(i).getDescription());
images.setImageResource(originalArray.get(i).getImage());
return row;
}
@Override
public int getCount() {
return originalArray.size();
}
@Override
public long getItemId(int i) {
//return originalArray.indexOf(getItemId(i));
return i;
}
@Override
public Filter getFilter() {
if (cs == null) {
cs = new CustomFilter();
}
return cs;
}
class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
if (charSequence != null && charSequence.length() > 0) {
charSequence = charSequence.toString().toUpperCase();
ArrayList<SingleRow> filters = new ArrayList<>();
for (int i = 0; i < tempArray.size(); i++) {
if (tempArray.get(i).getTitle().toUpperCase().contains(charSequence)) {
SingleRow singleRow = new SingleRow(tempArray.get(i).getTitle(), tempArray.get(i).getDescription(), tempArray.get(i).getImage());
filters.add(singleRow);
}
}
results.count = filters.size();
results.values = filters;
}
else
{
results.count = tempArray.size();
results.values = tempArray;
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
originalArray = (ArrayList<SingleRow>)filterResults.values;
notifyDataSetChanged();
}
}
SingleRow类
public class SingleRow {
String title;
String description;
int image;
public SingleRow(String title, String description,int image){
this.title = title;
this.description = description;
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
2019-05-12 17:06:15.359 17262-17262/com.example.kanjilearn E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kanjilearn, PID: 17262
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kanjilearn/com.example.kanjilearn.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2794)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2872)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6600)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
at com.example.kanjilearn.DictionaryFragment.onCreateView(DictionaryFragment.java:419)
如果这还不够,那就是我的GitHub
我是Android开发的新手,所以请详细解释并尽可能详细地说明解决该问题的方法。