I'm new here and I'm completely confused about my Android project. I have done some research, but still I don't have the answer. My problem was: when I clicked on the item in GridView it showed me just black screen. When I changed something, now it shows me an error in Eclipse. I don't know where my mistake is.
I used this answer Gridview with two columns and auto resized images to make my gridview nice and suitable for multiple screens.
I used something from this one: I want the Images which are on the gridView should show in full View.
My code: FirstActivity:
public class FirstActivity extends SherlockActivity {
ActionBar actionbar;
List<Item> items = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
actionbar = getSupportActionBar();
items.add(new Item("Name1", "Text1", R.drawable.pic_12));
items.add(new Item("Name2", "Text2", R.drawable.pic_13));
items.add(new Item("Name3", "Text3", R.drawable.pic_14));
items.add(new Item("Name4", "Text4", R.drawable.pic_15));
items.add(new Item("Name5", "Text5", R.drawable.pic_2));
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this, items));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to SecondActivity
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
// passing index (position of clicked item)
i.putExtra("id", position);
startActivity(i);
}
});
}
My ImageAdapter class:
public class ImageAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawable;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = (Item) items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
return v;
}
}
SecondActivity:
public class SecondActivity extends Activity {
List<Item> items = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
TextView name = (TextView) findViewById(R.id.text_small);
ImageView imageView = (ImageView) findViewById(R.id.picture);
TextView text = (TextView) findViewById(R.id.text_big);
Item item = (Item) items.get(position);
//***************HERE ARE MY ERRORS***************************************
name.setText(items.name);
text.setText(items.text);
imageView.setImageResource(items.drawableId);
//**********************************************************************
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
Item class:
public class Item {
String name;
int drawable;
String text;
public int getDrawable() {
return drawable;
}
public void setDrawable(int drawable) {
this.drawable = drawable;
}
public Item(String name, String text, int id) {
this.name = name;
this.text = text;
this.drawable = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My layouts: first:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</FrameLayout>
second:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/text_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_big"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
squareimageview:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.zva.app.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#55000000"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="10dp"
android:textColor="@android:color/white" />
</FrameLayout>