Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup the onClickListener for the imageview in the Listview

Tags:

android

There are a lot of discussions for this topic. But none of them work for me. I implemented a customized ListView with an ImageView in the ListView and the ListView is extended from the ListFragment. My ImageView is clickable so that when the user click the ImageView, the Camera activity is activated and the ImageView is updated with the image captured. The XML is as shown below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_background_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:clickable="true"
            android:focusable="false"
            android:contentDescription="@string/image_view_description"
            android:layout_height="50dip"
            android:src="@drawable/icons_preview"
            />

    </LinearLayout>  

</RelativeLayout>

Now is I need to differentiate the user's click which is for the list item or for the ImageView. I setup onListItemClick that can catch user's click for the list item in the ListView, but when I click for the ImageView, it does not work. How can I detect when the user click the ImageView. My ListFragment class is as shown below.

public class MyListFragment extends ListFragment {
    private customList_Adaptor adapter;
    private static final int CAMERA_REQUEST = 0;
    private static final int RESULT_OK = 0;
    private static MyListFragmentInterface mCallback = null;
    static Context thisListFragmentContext = null;
    private String[] mListItems;
    private TrackerDBAdapter dbHelper;
    private List<String> listIDs;   
    public MyListFragment() {   

    }

    public MyListFragment(MyListFragmentInterface callback) {
        mCallback = callback;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.my_list_fragment, container, false); 
        dbHelper = new TrackerDBAdapter(getActivity());
        dbHelper.open();
        Cursor cursor = dbHelper.fetchAllTrackerInTheList();
        mListItems = new String[dbHelper.getLastInsertedID()];
        thisListFragmentContext = getActivity();
        int i = 0;
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            mListItems[i++] = cursor.getString(1);

        dbHelper.close();
        listIDs = new ArrayList<String>(); 
        listIDs = Arrays.asList(mListItems);
        adapter = new customList_Adaptor(thisListFragmentContext, R.layout.list_row, listIDs);
        ListView lv = (ListView) view.findViewById(android.R.id.list);      
        lv.setAdapter(adapter);     



        return view;
    }

    public void addNewTrackerintoListDisplay(TrackerInfo newTracker){
        mListItems = append(mListItems, newTracker.getIdnumber());      
        setListAdapter(new ArrayAdapter<String>(getActivity(), 
                android.R.layout.simple_list_item_1,
            mListItems));
    }

    @SuppressLint("NewApi")
    private String[] append(String[] mListItems2, String idnumber) {
        // TODO Auto-generated method stub
        final int N = mListItems2.length;
        mListItems2 = Arrays.copyOf(mListItems2, N + 1);
        mListItems2[N] = idnumber;
        return mListItems2;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        mCallback.itemSelected(mListItems[position]);
    }


}

My adapter class is as follow.

public class customList_Adaptor extends ArrayAdapter<String>{


    protected static final int CAMERA_REQUEST = 0;

    private Context context;

    private List<String> listIDs = new ArrayList<String>();

    public customList_Adaptor(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        this.setContext(context);
        this.listIDs = objects;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(listIDs != null)
            return listIDs.size();
        else
            return 0;
    }

    @Override
    public String getItem(int arg0) {
        // TODO Auto-generated method stub
        if(listIDs != null)
            return listIDs.get(arg0);
        else
            return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder viewHolder=new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(vi==null){
            vi = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.id=(TextView)vi.findViewById(R.id.title);
            viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
            viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
            vi.setTag(viewHolder);
        }
        else
            viewHolder=(ViewHolder) vi.getTag();


//      viewHolder.thumbnailImage.setOnClickListener(new OnClickListener(){
//          
//          @Override
//          public void onClick(View arg0) {
//              // TODO Auto-generated method stub
//              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
//              ((Activity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST);
//          }
//          
//      }); 

        // Setting all values in listview
        viewHolder.id.setText(listIDs.get(position));

        return vi;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


}
like image 755
batuman Avatar asked Jun 24 '13 04:06

batuman


3 Answers

    public class MyListFragment extends ListFragment implements OnItemClickListener {
        private customList_Adaptor adapter;
        private static final int CAMERA_REQUEST = 0;
        private static final int RESULT_OK = 0;
        private static MyListFragmentInterface mCallback = null;
        static Context thisListFragmentContext = null;
        private String[] mListItems;
        private TrackerDBAdapter dbHelper;
        private List<String> listIDs;   
        public MyListFragment() {   

        }

        public MyListFragment(MyListFragmentInterface callback) {
            mCallback = callback;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view =  inflater.inflate(R.layout.my_list_fragment, container, false); 
            dbHelper = new TrackerDBAdapter(getActivity());
            dbHelper.open();
            Cursor cursor = dbHelper.fetchAllTrackerInTheList();
            mListItems = new String[dbHelper.getLastInsertedID()];
            thisListFragmentContext = getActivity();
            int i = 0;
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
                mListItems[i++] = cursor.getString(1);

            dbHelper.close();
            listIDs = new ArrayList<String>(); 
            listIDs = Arrays.asList(mListItems);
            adapter = new customList_Adaptor(thisListFragmentContext, R.layout.list_row, listIDs,this);
            ListView lv = (ListView) view.findViewById(android.R.id.list);      
            lv.setAdapter(adapter);     

            lv.setOnItemClickListener(this);

            return view;
        }

        public void addNewTrackerintoListDisplay(TrackerInfo newTracker){
            mListItems = append(mListItems, newTracker.getIdnumber());      
            setListAdapter(new ArrayAdapter<String>(getActivity(), 
                    android.R.layout.simple_list_item_1,
                mListItems));
        }

        @SuppressLint("NewApi")
        private String[] append(String[] mListItems2, String idnumber) {
            // TODO Auto-generated method stub
            final int N = mListItems2.length;
            mListItems2 = Arrays.copyOf(mListItems2, N + 1);
            mListItems2[N] = idnumber;
            return mListItems2;
        }

            @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
             mCallback.itemSelected(mListItems[position]);

        }
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {


        }

      public void imagepos(int i){
       //onImage click comes here
       }

    }
-----------------------------------------------------
 public class customList_Adaptor extends ArrayAdapter<String>{


        protected static final int CAMERA_REQUEST = 0;

        private Context context;
        MyListFragment mMyListFragment;
        private List<String> listIDs = new ArrayList<String>();

        public customList_Adaptor(Context context, int textViewResourceId,
                List<String> objects, MyListFragment mMyListFragment) {
            super(context, textViewResourceId, objects);
            this.setContext(context);
            this.listIDs = objects;
            this.mMyListFragment= mMyListFragment
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(listIDs != null)
                return listIDs.size();
            else
                return 0;
        }

        @Override
        public String getItem(int arg0) {
            // TODO Auto-generated method stub
            if(listIDs != null)
                return listIDs.get(arg0);
            else
                return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder viewHolder=new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(vi==null){
                vi = inflater.inflate(R.layout.list_row, parent, false);
                viewHolder.id=(TextView)vi.findViewById(R.id.title);
                viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
                viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
                vi.setTag(viewHolder);
            }
            else
                viewHolder=(ViewHolder) vi.getTag();


          viewHolder.thumbnailImage.setOnClickListener(new onMyClick(position));

            // Setting all values in listview
            viewHolder.id.setText(listIDs.get(position));

            return vi;
        }

public class onMyClick implements OnClickListener {

        private final int pos;
        public onMyClick(int pos) {
            this.pos = pos;
        }

        @Override
        public void onClick(View v) {
            mMyListFragment.imagepos(pos);
        }

    }

        public Context getContext() {
            return context;
        }

        public void setContext(Context context) {
            this.context = context;
        }


    }
like image 145
Sandeep P Avatar answered Nov 07 '22 05:11

Sandeep P


use onItemClickListener instead of onListItemClick

 lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        mCallback.itemSelected(mListItems[position]);

    }
 });
like image 1
Preet_Android Avatar answered Nov 07 '22 05:11

Preet_Android


In your Adapter class there will be getView that contains imageview in that view create onclick listener for the imageview

Imageview imageView=(ImageView)view.findviewByid(your id);

imageView.setOnClickListener(new OnClickListener() {

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

            }
        });
like image 1
Venkatesh S Avatar answered Nov 07 '22 05:11

Venkatesh S