Question

i cannot send data to second activity. mainactivity must send img url and img title to second activity but doesnt. in gridview i see both of them. but gives me null data, i dont know why

main activity: (clicklistener)

// Click event for single list row
            gridView.setOnItemClickListener(new OnItemClickListener() {

                @SuppressWarnings("unchecked")
                @Override
                 public void onItemClick(AdapterView<?> parent, View view,
                         int position, long id) {


                    HashMap<String, String> map2 = (HashMap<String, String>) gridView.getAdapter().getItem(position);

                    Intent in = new Intent(getApplicationContext(), FullSize.class);

                    in.putExtra(KEY_THUMB_URL, map2.get(KEY_THUMB_URL));
                    in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));

                    startActivity(in);
                 }
             });

LazyAdapter :

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position); 
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.grid_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title2); // title
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image2); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(CustomizedListView.KEY_TITLE));

        imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
        return vi;
    }
    public ImageLoader getImageLoader() {
        return imageLoader;
    }
}

full main activity :

public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "http://mysite .com/images/rss.xml";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "item";
    static final String KEY_THUMB_URL = "thumb_url";


    GridView gridView;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);


        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }


        gridView= (GridView) this.findViewById(R.id.list2);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);    

        gridView.setAdapter(adapter);


        // Click event for single list row
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @SuppressWarnings("unchecked")
            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {


                HashMap<String, String> map2 = (HashMap<String, String>) gridView.getAdapter().getItem(position);

                Intent in = new Intent(getApplicationContext(), FullSize.class);

                in.putExtra(KEY_THUMB_URL, map2.get(KEY_THUMB_URL));
                in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));

                startActivity(in);
             }
         });
    }   
}
Was it helpful?

Solution

You are accessing your extra wrong in your 2nd Activity. The key is not "KEY_TITLE", it is "item". Remove the quotes and access the variable KEY_TITLE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top