Search code examples
androidandroid-listviewsimplecursoradapteronitemclicklisteneronitemclick

Making onClick of SimpleCursorAdapter


I am using this code to get data from database & showing it directly to the custom listview which displays properly. This is how I did it using 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());
    }

}

I tried making its on click by this

    QueryFragment.listview1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

inside the while loop, outside the if condition, into my activity also but it gives me this exception.

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)

So, I used setOnItemClickListener like this

        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) {

        }
});

But still setOnItemSelectedListener event is not triggered,Any Idea, How to solve this problem?


Solution

  • Your problem is here..

    new OnItemSelectedListener()  {
    
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,.....
    

    Just change it to

    listview1.setOnItemClickListener(new OnItemClickListener(){
    
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
         //do your task
       }
    }
    

    Here position will help you to findout the selected item..so no need to drain brain in selected and not selected..