Android自定义ListView,当点击ImageView时获取TextView数据并打开Activity

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


                    }
                }});

我希望在缩略图被点击时打开一个新的活动。当我第一次选择项目时,它可以正常工作。但如果我在未选择项目的情况下点击缩略图,它会获取旧值。

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个回答

4
如果您的列表项点击无法识别,则是因为您的自定义列表项中有许多可点击项(imagebutton、button等)。为这些项设置 "android:focusable=false",您的listview点击就会正常了。
然后,如JaredLua所说,您应该在适配器中添加onClickLisener(),因为它仅针对thumb_image,而不是整个列表项。

你在inflate自定义列表项后是否将onclicklistener放入了适配器中?你还需要在yourcustomlistitem.findviewbyId(R.id.ivcatalouge)中进行操作。 - shreyas
在你的 onClickListener 中,你正在执行 v.findViewById(R.id.txtlargeimage);其中 v 是图片视图和列表项。这会导致空指针异常... - shreyas
那么我该如何获取TextView的值? - Naveen Jha
你的TextView在自定义布局中吗?如果是的话,那么应该使用vi.findViewById()来获取它。 - shreyas
你可以在onclicklistener()之前使用findviewbyid(),或将vi声明为final。 - shreyas
显示剩余4条评论

0
我认为你应该把 thumb_image.setOnClickListener(...) 放到你的 Adapter 的 getView 方法中,这样当对应的项目显示时,正确的大图将被设置为 thumb_image 的点击事件。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接