-1

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>
Community
  • 1
  • 1
linkas
  • 175
  • 5
  • 18

2 Answers2

0

In your SecondActivity.java items list is empty. So, whenever you try to get an item from a position in empty list it will throw error.

First of all, try to add few items to the list and also check whether the size of the list is greater than the position which you want to get from the list.

Once the above corrections are made, you also have to change the following lines in your SecondActivity,

name.setText(items.name);
text.setText(items.text);
imageView.setImageResource(items.drawableId);

to,

name.setText(item.getName());
text.setText(item.getText());
imageView.setImageResource(item.getDrawable());

Bcs, you can get the name,text,drawable values for a single item and not for whole items list.

EDIT 1:

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);
                Item item = (Item) items.get(position);
                // passing index (position of clicked item)
                i.putExtra("id", position);
                i.putExtra("Name",item.getName());
                i.putExtra("Text",item.getText());
                i.putExtra("image",item.getDrawable());
                startActivity(i);
            }
        });

EDIT 2:

i.putExtra("img", R.drawable.ic_launcher);

NextActivity

int img; as global

Intent i = getIntent();

if(i != null)
    img = i.getIntExtra("img", 0);

LinearLayout main_lay = (LinearLayout) findViewById(R.id.main_lay);
main_lay.setBackgroundResource(img);
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • What do you mean by saying "try to add few items to the list"? I think there are enough items, so why do i need to add more? I checked position with LogCat and it's correct. When I copied your lines of code, it underlines 3 lines where are getName(), getText(), getDrawable(). I tried this method earlier, but it didn't work.. Error says: The method getDrawable() is undefined for the type ClipData.Item – linkas Nov 05 '13 at 10:30
  • You checked position with logcat.. thats ok.. Have you tried `Log.v("items_size--",""+items.size())` in your `SecondActivity`? You check that, it ll return 0. – Hariharan Nov 05 '13 at 11:02
  • Okay, it returned me 0. What can I do for that? – linkas Nov 05 '13 at 13:26
  • Please, could you help me? – linkas Nov 07 '13 at 15:15
  • @user2954913 it's not possible to get the values from that. so you can pass all the values on click from the intent. – Hariharan Nov 15 '13 at 05:01
  • Okay, thank you a lot because now i understood my mistake. :) By the way, how to get image via Intent? I think textview i should send converted to String and then write name.setText(intent.getExtras().getString("Name")); but what about image? How to send image via Intent? – linkas Nov 16 '13 at 07:25
0

First you have to pass list of items with that position

here you passed only position of that clicked items.

Intent i = new Intent(getApplicationContext(), SecondActivity.class);
  i.putExtra("id", position);
  startActivity(i);

you are passing null values that's y it showed blank page.

first you have to pass like this

Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("item", items);
i.putExtra("id", position);
startActivity(i);

u can use list as static like this

static List<Items> items=new List<Items>();

in second activity u have to import like this

import static here declare package name.items;

and code like this

items.get(position).getName();
Aravi
  • 391
  • 4
  • 15
  • Now it underlines i.putExtra("item", items); I think you can't send ArrayList with Intent – linkas Nov 05 '13 at 10:32
  • ya u can not intent array list into another activity.instead of that u can use that list as static and in second activity u can access that list so u can get that list in second activity.then we use that list and use position in that list – Aravi Nov 05 '13 at 12:23
  • To write: static List items = new ArrayList(); in both activities? It didn't change anything... :( – linkas Nov 05 '13 at 13:33
  • Please, could you help me? – linkas Nov 07 '13 at 15:16
  • import static package com.second_activity.app name.items; To import like this? When I write items.get(position).getName(); it underlines getName() – linkas Nov 14 '13 at 15:59
  • code like this : public static List items = new ArrayList(); in FirstActivity and List items = FirstActivity.items; imageView.setImageResource(items.get(position).getDrawable()) in second activity it works! – Aravi Nov 16 '13 at 11:46