I am trying to create a gridview in Android Studio with images and texts. I found a couple of solutions but they were specific to the a single activity and not with activity with fragment. I can populate the text in grid using the following code. But I want images as well.
What should I do? I have a little experience in android development and I need to submit this project in 2 weeks from now.I have commented out some probable solutions but even they don't work. Please check the whole code.
This is my mainactivity with a placeholder fragment.
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;
}
}
And for the adapter.
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;
}
}