我正在使用此代码从数据库获取数据&直接显示到正确显示的自定义列表视图。这就是我使用SimpleCursorAdapter
的方法public void queryArticles(String querytext,Context mContext)
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cur=null;
String strquery="select rowid _id,part,A_name,italic,AS_name,Desc_art from Articles where AS_name LIKE '%"+querytext+"%'";
cur=db.rawQuery(strquery,null);
if(cur!=null&&cur.getCount() > 0)
{
do
{
String[] fromColumns = { "part", "A_name", "italic", "AS_name" };
int[] toViews = { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(mContext,R.layout.query_cell, cur, fromColumns, toViews, 0);
QueryFragment.listview1.setAdapter(adapter);
}
while(cur.moveToNext());
}
}
我尝试点击此处
QueryFragment.listview1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
在while循环中,在if条件之外,也在我的活动中,但它给了我这个例外。
07-29 13:24:22.970: E/AndroidRuntime(25608): FATAL EXCEPTION: main
07-29 13:24:22.970: E/AndroidRuntime(25608): java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.widget.AdapterView.setOnClickListener(AdapterView.java:780)
07-29 13:24:22.970: E/AndroidRuntime(25608): at com.vivekwarde.indianconstitution.QueryFragment.onCreateView(QueryFragment.java:48)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.os.Handler.handleCallback(Handler.java:615)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.os.Looper.loop(Looper.java:137)
07-29 13:24:22.970: E/AndroidRuntime(25608): at android.app.ActivityThread.main(ActivityThread.java:4794)
07-29 13:24:22.970: E/AndroidRuntime(25608): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 13:24:22.970: E/AndroidRuntime(25608): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 13:24:22.970: E/AndroidRuntime(25608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-29 13:24:22.970: E/AndroidRuntime(25608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-29 13:24:22.970: E/AndroidRuntime(25608): at dalvik.system.NativeStart.main(Native Method)
所以,我像这样使用了setOnItemClickListener
listview1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.v("hi","not getting called ? ");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
但仍然没有触发setOnItemSelectedListener事件,Any Idea,如何解决这个问题?
答案 0 :(得分:1)
你的问题在这里..
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,.....
只需将其更改为
即可listview1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//do your task
}
}
此处position
将帮助您找到所选项目。因此无需在选定的项目中排出大脑而未选择..
答案 1 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
<强>的ListView 强>
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
<强>微调强>
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
任何视图(按钮,TextView,ImageView等)
yourView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});