Please tell me about how to change or apply different icons with different text in the android grid view tutorial. My code is
public class Home extends Activity
{
//---the images to display---
Integer[] imageIDs = {
};
GridView grid_main;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid_main = (GridView) findViewById(R.id.gridview);
grid_main.setAdapter(new ImageAdapter(this));
grid_main.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c)
{
mContext = c;
}
//---returns the number of images---
public int getCount() {
return 12;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text1);
tv.setText("Profile "+position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image1);
iv.setImageResource(R.drawable.bo_1);
}
else
{
v = convertView;
}
return v;
}
}
}
This code is running perfectly but all the images nd text are same so i need to change it. please tell me how to do it ? Any help would be appreciated!!
Much, much, much better for ImageAdapter is:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(mTextsIds[position]);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(mThumbIds[position]);
} else {
v = (View) convertView;
}
return v;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4
};
// references to our texts
private String[] mTextsIds = {
"Text 1", "Text 2", "Text 3", "Text 4"
};
}
Please, note that images and texts are in variables mThumbIds and mTextsIds
The grid_item.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal"
>
<ImageView
android:id="@+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColorHighlight="#656565"
/>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With