0

我无法使用我的 json 响应加载自定义列表视图。这是我的代码。

// 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;
}

}

当我在调试模式下运行应用程序并检查以下代码中的值时,adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);

数组为空。需要帮忙!!

4

1 回答 1

1

您似乎没有在eventResponseArray任何地方初始化,所以它当然是空的。

于 2013-04-15T21:00:49.157 回答