Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid GridView doesn't show item onclick with text and image
    text
    copied!<p>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.</p> <p>I used this answer <a href="https://stackoverflow.com/questions/15261088/gridview-with-two-columns-and-auto-resized-images">Gridview with two columns and auto resized images</a> to make my gridview nice and suitable for multiple screens.</p> <p>I used something from this one: <a href="https://stackoverflow.com/questions/19541942/i-want-the-images-which-are-on-the-gridview-should-show-in-full-view">I want the Images which are on the gridView should show in full View</a>.</p> <p>My code: FirstActivity:</p> <pre><code>public class FirstActivity extends SherlockActivity { ActionBar actionbar; List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); @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&lt;?&gt; 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); } }); } </code></pre> <p>My ImageAdapter class:</p> <pre><code>public class ImageAdapter extends BaseAdapter { private List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); private LayoutInflater inflater; public ImageAdapter(Context context, List&lt;Item&gt; 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; } } </code></pre> <p>SecondActivity:</p> <pre><code> public class SecondActivity extends Activity { List&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); @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; } } </code></pre> <p>Item class:</p> <pre><code>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; } } </code></pre> <p>My layouts: first:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;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" /&gt; &lt;/FrameLayout&gt; </code></pre> <p>second:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/picture" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /&gt; &lt;TextView android:id="@+id/text_small" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/text_big" android:layout_width="wrap_content" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>squareimageview:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;com.zva.app.SquareImageView android:id="@+id/picture" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /&gt; &lt;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" /&gt; &lt;/FrameLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload