如何从GridView获取文本并将其显示在Fragment上

时间:2016-04-16 07:08:46

标签: android android-fragments gridview onclicklistener

我在Fragment中有一个带有GridView的应用程序,其上的每个项目都有一个名称和一张图片。当我点击某个项目时,我希望获取并显示每张图片的名称作为祝酒词。我该怎么办?

这是我的片段:

public class OneFragment extends Fragment {

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);
    // Get layout GridViewSi and load images from ImageAdapter.
    ExpandableHeightGridView gridViewSi = (ExpandableHeightGridView) view.findViewById(R.id.gridview_si);
    gridViewSi.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
    gridViewSi.setExpanded(true);

    gridViewSi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // This toast should show the picture's name like "This is Buddy" instead of position
            Toast.makeText(getActivity(), "This is " + position, Toast.LENGTH_SHORT).show();
        }
    });
    return view;
  }

}

这是我的ImageAdapter,数据是:

public final class ImageAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;

public ImageAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
    mItems.add(new Item("Moyo",       R.drawable.sample_0));
    mItems.add(new Item("Rex",   R.drawable.sample_1));
    mItems.add(new Item("Buddy", R.drawable.sample_2));
    mItems.add(new Item("Alejandro",      R.drawable.sample_3));
    mItems.add(new Item("Mía",     R.drawable.sample_4));
    mItems.add(new Item("Bobby",      R.drawable.sample_5));
    mItems.add(new Item("José Antonio",     R.drawable.sample_6));
    mItems.add(new Item("Skipper", R.drawable.sample_7));
    mItems.add(new Item("Moyo",       R.drawable.sample_0));
    mItems.add(new Item("Rex",   R.drawable.sample_1));
    mItems.add(new Item("Buddy", R.drawable.sample_2));
    mItems.add(new Item("Alejandro",      R.drawable.sample_3));
}

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

@Override
public Item getItem(int i) {
    return mItems.get(i);
}

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

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

    if (v == null) {
        v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
        v.setTag(R.id.grid_item_picture, v.findViewById(R.id.grid_item_picture));
        v.setTag(R.id.grid_item_name, v.findViewById(R.id.grid_item_name));
    }

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

    Item item = getItem(i);

    picture.setImageResource(item.drawableId);
    name.setText(item.name);

    return v;
}

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

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

我会感激任何帮助,因为我一直坚持下去!非常感谢你提前。

2 个答案:

答案 0 :(得分:0)

执行此操作的另一种方法是在适配器内执行父布局的单击事件。

首先,您需要在适配器内初始化layout.grid_item.xml的父布局。 (我假设你可以这样做。)

然后只执行该特定布局的点击事件。

例如

layout= (RelativeLayout) v.getTag(R.id.main_layout);

layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "This is " + name.getText().toString, Toast.LENGTH_SHORT).show();
            }
        });

你很高兴...

答案 1 :(得分:0)

好吧,多亏了这个优秀的答案:https://stackoverflow.com/a/24703304/5051506 @Raghunandan我明白了怎么做。

  

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

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

有了这个,我只需要将“name”变量添加到我的吐司中并显示它。希望它可以帮助将来遇到同样问题的人。