Imageview上的Android自定义列表视图单击获取textview数据并打开活动

时间:2013-07-23 07:47:02

标签: android android-listview android-imageview textview

  // 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();


                    }
                }});

我想在onc​​lick上打开一个关于拇指图像的新活动。首选项目时效果很好。如果我单击拇指图像而不选择项目它将获得旧值

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;
    }

现在我在拇指点击时获得空指针异常。 我已从listview

的SetOnItemClickListener中删除了thumb_image onclick

3 个答案:

答案 0 :(得分:4)

如果您的列表商品点击了这一点,那么这是因为您的自定义列表商品中有许多可点击的商品(图片按钮,按钮...)。为这些项目制作android:focusable=false,您的listview点击即可。

然后正如JaredLua所说,你应该在适配器中添加onClickLisener(),因为它仅用于thumb_image而不是整个listitem

答案 1 :(得分:0)

我想也许你应该把thumb_image.setOnClickListener(...)放到你的Adapter的getView方法中,所以当显示相应的项目时,正确的largeimage将设置为thumb_image的click事件。

答案 2 :(得分:0)

我从这里使用这个.. :) 从这三个链接中你可以得到清晰的想法,希望这有帮助。

See Here

If you want to use Image Button in your list view

Refere this last:)