AutocompleteTextview中的Android Listview无响应

时间:2017-02-04 07:18:55

标签: java android listview autocompletetextview onitemclicklistener

我的申请表有问题。当我尝试搜索我的应用程序时,当我点击建议的列表时,它将回复不幸关闭。为什么是这样 ?我该如何解决 ?

我会告诉你我的代码和logcat。

MainActivity.java

public class MainActivity extends Activity  {

AutoCompleteTextView autocomplete;
Button clear;

ListView lv;
String[] animals_names;

ArrayList<Main> animalslist;
AnimalsAdapter adapter;

@SuppressWarnings("rawtypes")
Class[] classes = {
    Aa.class,
    Ab.class,
    Ac.class,
    Ad.class,
    Ae.class,
    Af.class,



};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b_activity_main);

    lv = (ListView) findViewById(R.id.list_view);
    autocomplete = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    clear = (Button) findViewById(R.id.clear);
    clear.setVisibility(View.INVISIBLE);

    animals_names = getResources().getStringArray(R.array.animals_names);


    animalslist = new ArrayList<Main>();
    for (int i = 0; i < animals_names.length; i++) {
        Main country = new Main(i,animals_names[i], classes[i]);
        animalslist.add(country);
    }

    adapter = new AnimalsAdapter(getApplicationContext(), animalslist);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>  
      (this,android.R.layout.simple_list_item_1, animals_names);


    autocomplete.setAdapter(this.adapter);
      autocomplete.setThreshold(1);

      autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          AnimalsAdapter adapter1;

          public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Main item = (Main) adapter1.getItem(position);

                Intent d = new Intent(MainActivity.this, item.getClazz());
                startActivity(d);

                /** Fading Transition Effect */
                MainActivity.this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

            }
        });

    autocomplete.addTextChangedListener(new TextWatcher() {

          @Override
          public void afterTextChanged(Editable s) {
              //do nothing
          }

          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              //do nothing
          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              if(s.length() != 0) {
                  clear.setVisibility(View.VISIBLE);
              } else {
                  clear.setVisibility(View.GONE);
              }
          }
      });

}

}

Main.java

public class Main {
String name;
int id;
Class<?> clazz;

Main(int id, String name, Class<?> clazz) {
    this.name = name;
    this.clazz = clazz;
    this.id = id;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public void setClazz(Class<?> clazz) {
    this.clazz = clazz;
}

public Class<?> getClazz() {
    return clazz;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


}

AnimalsAdapter.java

public class AnimalsAdapter extends BaseAdapter implements Filterable {

Context context;
ArrayList<Main> animalslist;
ArrayList<Main> mStringFilterList;
ValueFilter valueFilter;

AnimalsAdapter(Context context, ArrayList<Main> animalslist) {
    this.context = context;
    this.animalslist = animalslist;
    mStringFilterList = animalslist;
}

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

@Override
public Object getItem(int position) {
    return animalslist.get(position);
}

@Override
public long getItemId(int position) {
    return animalslist.indexOf(getItem(position));
}

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

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    convertView = null;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.b_animals_item, null);

        TextView name_tv = (TextView) convertView.findViewById(R.id.name);

        Main animal = animalslist.get(position);

        name_tv.setText(animal.getName());

    }
    return convertView;
}

@Override
public Filter getFilter() {
    if (valueFilter == null) {
        valueFilter = new ValueFilter();
    }
    return valueFilter;
}

private class ValueFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (constraint != null && constraint.length() > 0) {
            ArrayList<Main> filterList = new ArrayList<Main>();
            for (int i = 0; i < mStringFilterList.size(); i++) {
                if ((mStringFilterList.get(i).getName().toUpperCase())
                        .contains(constraint.toString().toUpperCase())) {

                    Main country = new Main(mStringFilterList.get(i).getId(),mStringFilterList.get(i)
                                    .getName(), mStringFilterList.get(i).getClazz());


                    filterList.add(country);
                }
            }
            results.count = filterList.size();
            results.values = filterList;
        } else {
            results.count = mStringFilterList.size();
            results.values = mStringFilterList;
        }
        return results;

    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        animalslist = (ArrayList<Main>) results.values;
        notifyDataSetChanged();
    }

}

}

logcat的

02-04 02:07:17.079: E/AndroidRuntime(1035): FATAL EXCEPTION: main
02-04 02:07:17.079: E/AndroidRuntime(1035): Process: com.example.scientificnames, PID: 1035
02-04 02:07:17.079: E/AndroidRuntime(1035): java.lang.ClassCastException: android.widget.ArrayAdapter cannot be cast to android.view.Menu
02-04 02:07:17.079: E/AndroidRuntime(1035):     at com.example.scientificnames.MainActivity$1.onItemClick(MainActivity.java:247)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.os.Handler.handleCallback(Handler.java:733)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.os.Looper.loop(Looper.java:136)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at java.lang.reflect.Method.invoke(Method.java:515)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-04 02:07:17.079: E/AndroidRuntime(1035):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

ClassCastException使用

Main item = (Main) adapter.getItem(position);

而不是

Main item = (Main) ((Menu) adapter).getItem(position);

修改

你有两个适配器,一个是本地的(字符串类型)另一个是全局的(主类型)

autocomplete.setAdapter(this.adapter);

不是

autocomplete.setAdapter(adapter);

这里也使用局部变量使用全局

Main item = (Main) adapter.getItem(position);

你有两个适配器,一个是本地的,另一个是全局的

  
      
  1. Main类型的Global AnimalAdapter,是BaseAdapter的孩子
  2.   
  3. String类型的本地ArrayAdapter
  4.   


我可以知道为什么这两个??

答案 1 :(得分:0)

尝试:

 Main item = (Main) parent.getAdapter().getItem(position);

而不是:

Main item = (Main) ((Menu) adapter).getItem(position);

你的Arrayadapter启动应该是:

final ArrayAdapter<Main> adapter_new = new ArrayAdapter<Main>  
  (this,android.R.layout.simple_list_item_1, animalslist);

  autocomplete.setAdapter(adapter_new);

您使用String作为错误的参数