3
  // Adding menuItems to ListView


   mylist=(ListView)findViewById(R.id.lstshowcatalogue);
        ListAdapter adapter=new LazyAdapter(this, menuItems,getApplicationContext());

             mylist.setAdapter(adapter);
             mylist.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> Parent, View view, int position,
                        long id) {
                    // TODO Auto-generated method stub
                    if(position>=0)
                    {
                        TextView c = (TextView) view.findViewById(R.id.txtlargeimage);
                       largeimage  = c.getText().toString();
                        ImageView thumb_image=(ImageView)view.findViewById(R.id.ivcatalouge); // thumb image
                        thumb_image.setOnClickListener(new OnClickListener(){

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if(largeimage.length()>0)
                                {
                                Intent i=new Intent();
                                i.setClass(getApplicationContext(), FrmShowSingleImage.class);
                                i.putExtra("largeimage", largeimage);
                                startActivity(i);
                                //Toast.makeText(getApplicationContext(), largeimage, Toast.LENGTH_SHORT).show();
                                largeimage="";
                                }
                            }}); 


                      //Toast.makeText(getApplicationContext(), largeimage, Toast.LENGTH_SHORT).show();


                    }
                }});

I want to open a new activity on thumb image onclick. It works fine when First Select item. If I click thumb image without selecting item it getting old value

Here is my updated listview adapter
 public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_item, null);

        TextView name = (TextView)vi.findViewById(R.id.name); // name
        TextView desc = (TextView)vi.findViewById(R.id.desciption); // collection
        TextView cost = (TextView)vi.findViewById(R.id.cost); // cost
        TextView category = (TextView)vi.findViewById(R.id.txtcategory); // cost
        TextView spec = (TextView)vi.findViewById(R.id.txtspec); // cost
        TextView largeimg = (TextView)vi.findViewById(R.id.txtlargeimage); // cost

        ImageView thumb_image=(ImageView)vi.findViewById(R.id.ivcatalouge); // thumb image

       thumb_image.setOnClickListener(new OnClickListener(){

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

            TextView c = (TextView) v.findViewById(R.id.txtlargeimage);

             Intent i=new Intent();
                i.setClass(mCtx, FrmShowSingleImage.class);
                i.putExtra("largeimage", c.getText());
                mCtx.startActivity(i);

        }});

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        name.setText(song.get(FrmShowCatlogue.KEY_MODEL));
        desc.setText(song.get(FrmShowCatlogue.KEY_COLLECTION));
        cost.setText(song.get( FrmShowCatlogue.KEY_MRP));
        category.setText(song.get( FrmShowCatlogue.KEY_CATEGORY));
        spec.setText(song.get( FrmShowCatlogue.KEY_SPEC));
        largeimg.setText(song.get( FrmShowCatlogue.KEY_LARGE));
        largeimg.setVisibility(View.GONE);
                try 
        {
            String filename=song.get(FrmShowCatlogue.KEY_IMAGES.toString());
            filename="thumbs/" + filename;
            // get input stream
            InputStream ims = mCtx.getAssets().open(filename);
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            thumb_image.setImageDrawable(d);
        }
        catch(IOException ex) 
        {

            //thumb_image.setVisibility(View.GONE);
        }

        return vi;
    }

Now I am getting null pointer exception on thumb click. I have removed thumb_image onclick from SetOnItemClickListener of listview

Naveen Jha
  • 93
  • 1
  • 9

3 Answers3

4

If your list item click doent get recognized,it is because your custom list item has many clickable items(imagebutton,button...) in it. make android:focusable=false for those items and your listview click will be allright.

then as JaredLua said you should add the onClickLisener() in the adapter since its for the thumb_image only and not for the listitem as a whole

shreyas
  • 2,166
  • 3
  • 18
  • 30
  • Now I am getting null pointer exception on thumb click. I have removed thumb_image onclick from SetOnItemClickListener of listview – Naveen Jha Jul 23 '13 at 08:39
  • did you put the onclicklistener in the adapter after you inflate the custom list item. you need to do yourcustomlistitem.findviewbyId(R.id.ivcatalouge) there also. – shreyas Jul 23 '13 at 08:43
  • in you onClickListener you are doing v.findViewById(R.id.txtlargeimage); where v is the image view and list item. that is causing the null pointer exception... – shreyas Jul 23 '13 at 08:48
  • ImageView thumb_image=(ImageView)vi.findViewById(R.id.ivcatalouge); // thumb image thumb_image.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub **boldTextView c = (TextView) v.findViewById(R.id.txtlargeimage);** Intent i=new Intent(); i.setClass(mCtx, FrmShowSingleImage.class); i.putExtra("largeimage", c.getText()); mCtx.startActivity(i); }}); – Naveen Jha Jul 23 '13 at 08:50
  • Then how can I get value of Textview – Naveen Jha Jul 23 '13 at 08:52
  • your textview is in the customlayout right??? if yes then it should be vi.findviewbyid(); – shreyas Jul 23 '13 at 08:53
  • TextView c = (TextView) vi.findViewById(R.id.txtlargeimage); //Cannot refer to a non-final variable vi inside an inner class defined in a different method – Naveen Jha Jul 23 '13 at 08:55
  • you can do the findviewbyid() before the onclicklistener()or make vi as final – shreyas Jul 23 '13 at 08:58
  • if you make findviewbyid() before onClicklistener() then you will have to make c as final. But if you dont want to make it final you can declare it at class level. – shreyas Jul 23 '13 at 09:04
0

I think maybe you should put thumb_image.setOnClickListener(...) into your Adapter's getView method, so correct largeimage will be set to thumb_image's click event when corresponding item is shown.

JaredLuo
  • 481
  • 3
  • 9
0

I have use this from Here.. :) From this three link you can get clear idea,,Hope this help.

See Here

If you want to use Image Button in your list view

Refere this last :)

Community
  • 1
  • 1
Mit Bhatt
  • 1,605
  • 2
  • 13
  • 24