gpt4 book ai didi

java - AutocompleteTextview 中的 Android Listview 没有响应

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:03 25 4
gpt4 key购买 nike

我的应用程序出现问题。当我尝试搜索我的应用程序并单击建议列表时,它会响应“不幸的是 close”。为什么是这样 ?我该如何解决 ?

我将向您展示我的代码和 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)

最佳答案

尝试:

 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作为参数,这是错误的

关于java - AutocompleteTextview 中的 Android Listview 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42037677/

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