0

I am not able to load the custom listview with my json response. Here is my code.

// my activity

public class LocalExploreActivity extends Activity {

ListView lvEvents;
    JSONAdapter adapter;

    JSONArray eventResponseArray; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_explore);
lvEvents = (ListView) findViewById(R.id.eventsList);
    adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);
    lvEvents.setAdapter(adapter);

}

private void getEventsList() throws JSONException {


    JSONObject jsonObjSend = new JSONObject();
    JSONObject jsonObjRecv = HttpGetClient.SendHttpPost(URL);


    eventResponseArray = jsonObjRecv.getJSONArray("events");
    System.out.println("events array issssssssssss "+eventResponseArray);
    System.out.println("array len "+eventResponseArray.length());
    JSONObject parkObj;
    JSONObject activityObj;
    JSONArray sessionArray;

    for (int i = 0; i < eventResponseArray.length(); i++) {
        JSONObject eventDetails = eventResponseArray.getJSONObject(i);

        parkObj = eventDetails.getJSONObject("park");

        activityObj = eventDetails.getJSONObject("activity");


        eventName = eventDetails.getString("name");
        System.out.println("the eventName is 33333 "+eventName);

        eventFee = eventDetails.getString("fee");
        System.out.println("the eventFee is 33333 "+eventFee);


        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        EventModel.PREF_EVENTNAME = prefs.getString(eventName, eventName);

        EventModel.PREF_EVENTFEE = prefs.getString(eventFee, eventFee);

    }
}

}



// my Adapter

public class JSONAdapter extends BaseAdapter implements ListAdapter{


private final Activity activity;
private final JSONArray jsonArray;


protected JSONAdapter (Activity activity, JSONArray jsonArray) {
    assert activity != null;
    assert jsonArray != null;

    this.jsonArray = jsonArray;
    this.activity = activity;
}

@Override public int getCount() {
    if(null==jsonArray) 
        return 0;
    else
        return jsonArray.length();
}

@Override public JSONObject getItem(int position) {
    if(null==jsonArray) return null;
    else
        return jsonArray.optJSONObject(position);
}

@Override public long getItemId(int position) {
    JSONObject jsonObject = getItem(position);

    return jsonObject.optLong("id");
}


@Override public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = activity.getLayoutInflater().inflate(R.layout.custom_events_list, null);



    TextView txtEventName = (TextView)convertView.findViewById(R.id.eventTitleTV);
    TextView txtEventFee = (TextView)convertView.findViewById(R.id.eventPriceTV);  

    JSONObject json_data = getItem(position);  
    System.out.println("the json data received is 111111111 "+json_data);
    if(null!=json_data ){
        String eventName = null;
        String eventFee = null;
        try {
            eventName = json_data.getString("name");
            eventFee=json_data.getString("fee");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        txtEventName.setText(eventName); 
        txtEventFee.setText(eventFee);
    }


    return convertView;
}

}

When I run the app in the Debug mode, and check for the value in the below code, adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);

The array is null. Need Help!!

1 Answer 1

1

You don't appear to be initializing the eventResponseArray anywhere so of course its null.

Sign up to request clarification or add additional context in comments.

2 Comments

I have edited the code now. I am getting the eventResponseArray. It is not null.
You were right. The array was null. I set my adapter after I received the response. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.