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

How to get text from GridView and display it on Fragment

我有一个在 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;
    }
}
}

我将感谢任何形式的帮助,因为我已经坚持了一整天!非常感谢您。

另一种方法是在适配器中执行父布局的点击事件。

首先,您需要在适配器中初始化 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();
            }
        });

你很高兴..

好吧,多亏了这个出色的答案: @Raghunandan 我想出了如何去做。

Use the position param and get the item at that position

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

and you can move the below to separate .java file

public class Item 
{
         String name;
         int drawableId;

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

这样,我只需将 "name" 变量添加到我的 toast 中,它就会显示出来。希望它可以帮助将来遇到同样问题的人。