我正在使用SearchView。当我在Searchview中编写查询时,会显示一个项目列表(建议列表)。当我单击列表项时,列表项的文本不会显示在Searchview中。我已经尝试过建议侦听器,但是应用程序无法通过此方法工作。如何在SearchView中显示单击项的文本。 我也尝试过setquery(),但它仅适用于第一个或某些项目,当我单击另一个项目时它显示为SearchView,它会停止应用程序。当我删除setQuery()时,它工作正常,但名称未显示在SearchView中。 这是代码的一部分。 CustomAdapter.java
convertView.setOnClickListener(new View.OnClickListener() {
TextView txtView = (TextView) ((MainActivity)c).findViewById(R.id.textView);
@Override
public void onClick(View v) {
txtView.setText("");
SearchView sv=(SearchView) ((MainActivity)c).findViewById(R.id.sv);
sv.setQuery(""+planets.get(pos).getName(),false);
sv.clearFocus();
String d=planets.get(pos).getPrice();
int id1=planets.get(pos).getId();
String text=planets.get(pos).getName();
txtView.setText(id1 + System.getProperty ("line.separator") +text + System.getProperty ("line.separator") +d);
planets.clear();
v.setVisibility(View.GONE);
txtView.setVisibility(View.VISIBLE);
}
});
这是调试错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tutorials.hp.sqlitefilterlistview, PID: 22679
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:260)
at java.util.ArrayList.get(ArrayList.java:313)
at com.tutorials.hp.sqlitefilterlistview.mListView.CustomAdapter$1.onClick(CustomAdapter.java:106)
at android.view.View.performClick(View.java:5265)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I/Process: Sending signal. PID: 22679 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
它指向CustomAdapter.java(106)行,即
String d=planets.get(pos).getPrice();
我该如何解决?
答案 0 :(得分:0)
我也尝试过setQuery(),但它仅适用于第一项或某些项, 当我单击另一个项目时,它将停止该应用程序。
是的,您需要it looks like setQuery(text, false)
。现在,该应用程序可用于某些项目和其他崩溃的事实很可能与setQuery(..)
无关。
例如,如果pos
变量超出了planets
列表大小的范围,则对planets.get(pos)
的调用将失败,并返回IndexOutOfBoundsException
,但是检查堆栈跟踪,会告诉您该应用为何崩溃的原因。