iamlukeyb
iamlukeyb

Reputation: 6557

Android setlistAdapter error

hi just getting an error and I'm not sure why can anyone help?

when i run it it forces the application to close. not sure what this means as i'm new but hopefully someone can help?

heres the error from the LogCatenter image description here

this is how my json feed is structured i want the news array

{"code":200,"error":null,"data":{"news":[{"news_id":"8086" I'm getting an error here: in the oneOjectsItem

setListAdapter ( new ArrayAdapter(this, R.layout.single_item, oneObjectsItem));

here my code

        // Instantiate a JSON object from the request response
        JSONObject jsonObject = new JSONObject(json);



        JSONArray jArray = jsonObject.getJSONArray("news");


        for (int i=0; i < jArray.length(); i++)
        {
            JSONObject oneObject = jArray.getJSONObject(i);
            // Pulling items from the array
            String oneObjectsItem = oneObject.getString("title");



        }


    } catch(Exception e){
        // In your production code handle any errors and catch the individual exceptions
        e.printStackTrace();
    }




    setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, oneObjectsItem)); 



ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
    }



});

}

Upvotes: 0

Views: 491

Answers (1)

Asahi
Asahi

Reputation: 13506

I am guessing that you have another declaration of String oneObjectsItem which is not initialized. Then you create (by mistake?) another string inside for loop and init it, while the first one is left as null.

If I understand your logic correctly it could be something like this:

 List<String> titles = new ArrayList<String>();

 for (int i=0; i < jArray.length(); i++){ 
       oneObject = jArray.getJSONObject(i);
        // Pulling items from the array
        titles.add(oneObject.getString("title"));
    }

setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, titles)); 

Upvotes: 1

Related Questions