Imageview上的Android自定义listview点击获取textview数据并打开 Activity

标签 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();


                    }
                }});

我想在缩略图 onclick 上打开一个新 Activity 。当第一次选择项目时它工作正常。如果我在没有选择项目的情况下单击缩略图,它会得到旧值

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

最佳答案

如果您的列表项点击没有被识别,那是因为您的自定义列表项中有许多可点击的项目(imagebutton,button...)。为这些项目制作 android:focusable=false ,您的 ListView 点击就可以了。

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

关于Imageview上的Android自定义listview点击获取textview数据并打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17804310/

相关文章:

android - Stripe 结账模态键盘覆盖小型设备上的输入和支付按钮

android - 在 Nexus (7.1.1) 上获取导航栏方向(左/右)

android - 无法获得项目位置以关闭列表项目

android - 在 ImageView 上设置图像不起作用 - Android

android - 如何使用可移动图像获得正确的 ImageView 坐标

android - 为什么某些 Android API 没有显示在 AVD Manager 目标选项卡中?

android - 删除行后 SQLite id 行不遵循顺序

android - 在 Button 的 Onclick 中从 Adapter 类重新加载 ListView 数据

android - 如何在android中一次对2个列表进行排序

android - 如何将位图图像从 Activity 传输到 Fragment