如何从片段中的GridView onItemClick获取ArrayList中的String?

时间:2014-07-11 17:24:42

标签: android gridview android-fragments

我正在尝试为gridview设置一个onclick监听器。 gridview显示文本和图像。单击某个项目时,我希望它从该项目中获取文本并针对某些if语句进行检查。这是我的课。由于这个事实,它在一个片段中,而且我是碎片的新手,我很难搞清楚它。感谢

    IMPORTS HERE...

    public class Items extends SherlockFragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.activity_main, container, false);


            GridView gridView = (GridView)view.findViewById(R.id.gridview);
            gridView.setAdapter(new MyAdapter(getActivity()));
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    if(SOMETHING.equals("Furniture")){
                        Toast.makeText(getActivity(), "Furny", Toast.LENGTH_SHORT).show();
                    //THIS IS WHERE I'M STUCK. I DON'T KNOW WHAT TO REPLACE "SOMETHING" WITH

}
             }
            });


            return view;
        }



        public class MyAdapter extends BaseAdapter
        {
            private List<Item> items = new ArrayList<Item>();
            private LayoutInflater inflater;

            public MyAdapter(Context context)
            {
                inflater = LayoutInflater.from(context);

                items.add(new Item("Weapons", R.drawable.BOMB));
                items.add(new Item("Tools", R.drawable.TOOL));
                items.add(new Item("Furniture", R.drawable.CHAIR));


            }

            @Override
            public int getCount() {
                return items.size();
            }

            @Override
            public Object getItem(int i)
            {
                return items.get(i);
            }

            @Override
            public long getItemId(int i)
            {
                return items.get(i).drawableId;
            }



            @Override
            public View getView(int i, View view, ViewGroup viewGroup)
            {
                View v = view;
                ImageView picture;
                TextView name;

                if(v == null)
                {
                   v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
                   v.setTag(R.id.picture, v.findViewById(R.id.picture));
                   v.setTag(R.id.text, v.findViewById(R.id.text));
                }

                picture = (ImageView)v.getTag(R.id.picture);
                name = (TextView)v.getTag(R.id.text);

                Item item = (Item)getItem(i);
                Options options = new BitmapFactory.Options();
                options.inDither = false;
                options.inScaled = false;
                Bitmap source = BitmapFactory.decodeResource(getResources(), item.drawableId, options);

                final int maxSize = 120;
                int outWidth;
                int outHeight;
                int inWidth = source.getWidth();
                int inHeight = source.getHeight();
                if(inWidth > inHeight){
                    outWidth = maxSize;
                    outHeight = (inHeight * maxSize) / inWidth; 
                } else {
                    outHeight = maxSize;
                    outWidth = (inWidth * maxSize) / inHeight; 
                }

                Bitmap resizedBitmap = Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
                picture.setImageBitmap(resizedBitmap);
                name.setText(item.name);

                return v;


            }

            private class Item
            {
                final String name;
                final int drawableId;

                Item(String name, int drawableId)
                {
                    this.name = name;
                    this.drawableId = drawableId;
                }
            }
        }


    }

1 个答案:

答案 0 :(得分:0)

使用position参数并获取该位置的项目

 Item item= (Item) parent.getItemAtPosition(position);
 String name = item.name;

您可以将以下内容移到单独的.java文件

  public class Item 
  {
             String name;
             int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
   }