添加带有图像和文本的gridview

时间:2015-04-03 09:07:25

标签: android gridview fragment android-fragmentactivity

我正在尝试在Android Studio中使用图像和文本创建gridview。我找到了几个解决方案但是它们特定于单个活动而不是片段活动。我可以使用以下代码填充网格中的文本。但我也想要图像。

我该怎么办?我在Android开发方面有一点经验,我需要在两周后提交这个项目。我已经注释了一些可能的解决方案,但即使它们不起作用。请检查整个代码。

这是我对占位符片段的主要活动。

public static class PlaceholderFragment extends Fragment {
        private ArrayAdapter<String> gridAdapter;
       //gridView gridAdapter;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        final String[] mainMenuArray = {
                "Create a new class",
                "Take Attendance",
                "Check Up Status",
                "Update/Modify Students",
                "Delete/Update class",
                "Add students"

               } ;
        int[] imageId = {
               R.mipmap.ic_launcher,
               R.mipmap.ic_launcher,

        };
        List<String> mainMenu = new ArrayList<String>(Arrays.asList(mainMenuArray));
      //  gridAdapter = new gridView(getActivity(), mainMenuArray, imageId);
                gridAdapter =
                                new ArrayAdapter<String>(
                                            getActivity(), // The current context (this activity)
                                            R.layout.grid_items, // The name of the layout ID.
                                            R.id.grid_text, // The ID of the textview to populate.
                                            mainMenuArray);
        GridView grid;

        //gridView gridAdapter = new gridView(getActivity(), mainMenuArray, imageId);
        grid=(GridView)rootView.findViewById(R.id.grid);
        grid.setAdapter(gridAdapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getActivity(), "You Clicked at " +    mainMenuArray[+position], Toast.LENGTH_SHORT).show();
            }
        });
           return rootView;

    }
}

适用于适配器。

    public class gridView extends BaseAdapter{
private Context mContext;
private final String[] mainMenuArray;
private final int[] Imageid;
public gridView(Context c,String[] mainMenuArray,int[] Imageid ) {
    mContext = c;
    this.Imageid = Imageid;
    this.mainMenuArray = mainMenuArray;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mainMenuArray.length;
}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_items, null);
        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
        textView.setText(mainMenuArray[position]);
        imageView.setImageResource(Imageid[position]);
    } else {
        grid = (View) convertView;
    }
    return grid;
  }
}

1 个答案:

答案 0 :(得分:0)

public static class PlaceholderFragment extends Fragment {
        private ArrayAdapter<String> gridAdapter;
       gridView gridAdapter;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        GridView grid=(GridView)rootView.findViewById(R.id.grid)

        final String[] mainMenuArray = {
                "Create a new class",
                "Take Attendance",
                "Check Up Status",
                "Update/Modify Students",
                "Delete/Update class",
                "Add students"

               } ;
        int[] imageId = {
               R.mipmap.ic_launcher,
               R.mipmap.ic_launcher,
               0,0,0,0

        };

        gridAdapter = new gridView(getActivity(), mainMenuArray, imageId);
        grid.setAdapter(gridAdapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getActivity(), "You Clicked at " +    mainMenuArray[+position], Toast.LENGTH_SHORT).show();
            }
        });
           return rootView;

    }
}

试试这个让我知道:))