Q:

Android: pass JSON values to new Activity via CustomListview and OnItemClickListener

I got a little problem with my code, maybe its becaus I'm new to android or maybe I'm starring at it for to long.
I wanted to write a code to get content from a JSON, create a ListView from one of the objects values and pass onClick the other value to a new Activity.
It's a bit like a newsapp, having the headline in the ListView and the content in the next Activity.
Now I'm stuck in a for-loop .....
try {
        jsonObject = new JSONObject(uebergabe);
        jsonArray = jsonObject.getJSONArray("Fux");

        for (int i = 0; i < jsonArray.length(); i++) {

            HashMap<String, String> Hashmap = new HashMap<String, String>();
            JSONObject jO = jsonArray.getJSONObject(i);

            String headline = jO.optString("headline");
            Hashmap.put("headline", headline);

            final String content = jO.optString("content");
            Hashmap.put("content", content);

            arrayList.add(Hashmap);

            list = (ListView) findViewById(R.id.list_fuxenfibel);
            ListAdapterFibel adapterFibel = new ListAdapterFibel(this, R.layout.layout_fibel, arrayList);
            list.setAdapter(adapterFibel);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,int position, long id)
                {
                       Intent intent = new Intent(Fuxenfibel.this, Fuxenfibel_Inhalt.class);
                       intent.putExtra("content", content);
                       startActivity(intent);
                }
            });
        }

    } catch (JSONException e)

Problem: Every listitem passes the same value ... the one wich is created last in the for-loop ... surprise ... after hours of googling and trying I need help to integrad the variable (i)  to the onclick method.
I tried like this
//[....]
       final int x=i;
//[.....]
       @Override
       public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

       try {
       JSONObject jObj = jsonArray.getJSONObject(x);
       String content = jObj.optString("content");
       Intent intent = new Intent(Fuxenfibel.this, Fuxenfibel_Inhalt.class);
       intent.putExtra("content", content);
       startActivity(intent);
       }catch (JSONException e){}
       //[...]

same result.
Please explain what i'm doing wrong, or better explain on an example.
Thanks
Just one final question for the understanding, in my code the onItemClick method is within the forloop and passes only the last value. Does this mean, its only cast onClick, or is there another reason ?

A:

Never initialize Listview and adapter inside the loop. Your listview and adapter will be created many time.
Check the code below
    try {
            jsonObject = new JSONObject(uebergabe);
            jsonArray = jsonObject.getJSONArray("Fux");

         // set all data for your arraylist

            for (int i = 0; i < jsonArray.length(); i++) {

                HashMap<String, String> Hashmap = new HashMap<String, String>();
                JSONObject jO = jsonArray.getJSONObject(i);

                String headline = jO.optString("headline");
                Hashmap.put("headline", headline);

                final String content = jO.optString("content");
                Hashmap.put("content", content);

                arrayList.add(Hashmap);
            }

           // initialize listview and create adapter

           list = (ListView) findViewById(R.id.list_fuxenfibel);
                ListAdapterFibel adapterFibel = new ListAdapterFibel(this, R.layout.layout_fibel, arrayList);
                list.setAdapter(adapterFibel);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,int position, long id)
                    {
                           Intent intent = new Intent(Fuxenfibel.this, Fuxenfibel_Inhalt.class);
                          // put value of selected position
                           intent.putExtra("content", arrayList.getPosition(i).get("content"));
                           startActivity(intent);
                    }
             });

        } catch (JSONException e)

Hope this help

