Varun Tewari Varun Tewari - 2 months ago 17
Android Question

Setting OnItemClickListener from Adapter Class

I have a

ListView
in my
MainActivity.java
. The adapter for that list view is being set from The
AsyncTask
class which pulls in data from the
sqlite
database. Now I need to set the
OnItemClickListener
for the
ListView
. I have tried to set the Listener from different classes including
MainActivity
class and the class extending
ArrayAdapter
class but for some reason it is not working.

I also tried to do it from the class extending
AsyncTask
but I get a error saying that I need to set the listener from the
UI thread


@Override
protected void onPostExecute(String s) {
if(s.equals(SUCCESS_FETCH_ALL)){
mList.setAdapter(adapter);
}
else if (s.equals(SUCCESS_INSERT))
mActivity.startActivity(new Intent(mContext, MainActivity.class));
}


I have also tried to create a separate class and make it implement the Click Listener

The entire code is on Github

There are the links for the Classes that are involved in the question


N J N J
Answer

In LectureListAdapter you can set clickListener in getView()

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

           .....

        row.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v){
            // do your stuff here for onlick
         }
        });
        return row;
    }
Comments