数组适配器中的ArrayList保持为null

时间:2015-07-20 21:50:20

标签: android arrays listview android-arrayadapter

我是Android新手,我的列表视图无法正常工作。该列表将数组列表中的所有数据显示为单行。

我的MainActivity看起来像这样:

    public class MainActivity extends FragmentActivity implements OnClickListener{

..
    ArrayList<StorylineData> storylineData;

    ArrayList<SummaryData> summary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSpinnerAPI = (Spinner) findViewById(R.id.spinnerAPI);
        mButtonSubmit = (Button) findViewById(R.id.buttonSubmit);
        mEditTextResponse = (ListView) findViewById(R.id.editResponse);
        mProgressRequest = (ProgressBar) findViewById(R.id.progressRequest);

        mButtonSubmit.setOnClickListener(this);
     storylineData = new ArrayList<StorylineData>();

  ...

    @Override
    public void onClick(View v) {
        toggleProgress(true);
        switch (mSpinnerAPI.getSelectedItemPosition()) {
            case 0: // Authenticate
              ...
            case 4: // Get Summary Day
                MovesAPI.getSummary_SingleDay(summaryHandler, "20150418", null);//Date changed to "20150117"
                break;
          ...
            case 10: // Get Storyline Day
                MovesAPI.getStoryline_SingleDay(storylineHandler, "20150418", "20140714T073812Z", false);//Date changed to "20150418" "null changed to"20140714T073812Z"
                break;
          ...
                toggleProgress(false);
                break;
        }

    }

   ...

    private MovesHandler<ArrayList<StorylineData>> storylineHandler = new MovesHandler<ArrayList<StorylineData>>() {

        @Override
        public void onSuccess(ArrayList<StorylineData> result) {
            toggleProgress(false);
                 updateResponse(
                           "Date:\t" + result.get(0).getDate() + "\n"
                           + "-----------Activity 1 Summary--------\t" + "\n"
                           + "Activity:\t" + result.get(0).getActivity().toString() + "\n"//returns 1587 with .getCaloriesIdle()
                      ...
                           + "-----------Activity 2 Summary---------\t" + "\n"
                           + "Activity:\t" + result.get(0).getSummary().get(0).getActivity() + "\n"//returns 1587 with .getCaloriesIdle()
 ...


 + "-----------Segments---------\t" + "\n"
                           + "Type:\t" + result.get(0).getSegments().get(0).getType() + "\n"//returns 1587 with .getCaloriesIdle()

...
        }

    public void updateResponse(final String message) {
        MainActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                mButtonSubmit.setText(message);

                StorylineAdapter adapter = new StorylineAdapter(MainActivity.this, R.layout.item_storyline, storylineData);
                mEditTextResponse.setAdapter(adapter);

            }
        });
    }
}

我的ArrayAdapter类如下所示:

public class StorylineAdapter extends ArrayAdapter<StorylineData> {
    private Context context;
    private ArrayList<StorylineData> storylineData;

    public StorylineAdapter(Context context, int resource, ArrayList<StorylineData> objects) {
        super(context, resource, objects);
        this.context = context;
        this.storylineData = objects;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_storyline, parent, false);



        //Display in the TextView widget
        StorylineData storylineData1 = storylineData.get(position);
        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(storylineData1.getDistance());

        return view;
    }

    @Override
    public int getCount()
    {
        if (storylineData!=null)
        {
            return storylineData.size();
        }
        else
        {
            Log.e("Adapter", "Summary array is null");
            return 0;
        }
    }
}

这是我的解析器类:

public class StorylineData extends ArrayList<StorylineData> {

...Getters/Setters...

/**
 * Parse a {@link org.json.JSONObject} from storyline {@link org.json.JSONArray}, then return the corresponding {@link StorylineData} object.
 *
 * @param jsonObject : the storyline JSON object received from server
 * @return corresponding {@link StorylineData}
 */
public static StorylineData parse(JSONObject jsonObject) throws JSONException {
    if (jsonObject != null) {

        StorylineData storylineData = new StorylineData();


        storylineData.date = jsonObject.optString("date");
        storylineData.caloriesIdle = jsonObject.optInt("caloriesIdle");
        storylineData.lastUpdate = jsonObject.optString("lastUpdate");
        storylineData.summary = new ArrayList<SummaryData>();
        storylineData.segments = new ArrayList<SegmentData>();



        /**Get the data associated with the array named summary **To get a specific JSONArray: Summary*/
        JSONArray summariesJsonArray = jsonObject.optJSONArray("summary");

        if (summariesJsonArray != null)
        for (int i = 0; i < summariesJsonArray.length(); i++) {


            /**each time through array will need to get a reference of current object*/
            JSONObject summaryJsonObject = summariesJsonArray.optJSONObject(i);

                    if (summaryJsonObject != null) {
                    /**===============Translate data from json to Java=================*/
                        /**Create a new OBJECT OF ARRAY STORYLINE/SUMMARY*/
                        ArrayList<SummaryData> summary = new ArrayList<SummaryData>(); // initialisation must be outside the loop
                        storylineData.setSummary(summary);

                        /**Get summary from json into java*/
                        summariesJsonArray.getJSONObject(i).getString("distance");

                        /**Get date from json into java*/
                        String date = summaryJsonObject.optString("date");
                        storylineData.setDate(date);

                        /**Get group from json into java*/
                        String group = summaryJsonObject.optString("group");//Get name using key e.g. date
                        storylineData.setGroup(group);

               ...


                        storylineData.summary.add(SummaryData.parse(summaryJsonObject));
                        Log.d("StorylineDataCls \t sJo", summaryJsonObject.toString() + "Log\n");
                        System.out.println("print distance" + summariesJsonArray.getJSONObject(i).getString("distance"));
                        System.out.println("print summary" + summaryJsonObject);


                }
            }

        JSONArray segmentsJsonArray = jsonObject.optJSONArray("segments");
        if (segmentsJsonArray != null) {

            for (int i = 0; i < segmentsJsonArray.length(); i++) {
                JSONObject segment = segmentsJsonArray.optJSONObject(i);

                if (segment != null) {

                    ArrayList<SegmentData> segments = new ArrayList<SegmentData>(); // initialisation must be outside the loop
                    storylineData.setSegments(segments);

                    String type = segment.optString("type");
                    storylineData.setType(type);

                  ...

                    storylineData.segments.add(SegmentData.parse(segment));


                    Log.d("StorylineDataCls \t sSo", segment.toString());

                }
            }
        }
        return storylineData;
    }
    return null;


}


}

ArrayAdapter items display in a single row

0 个答案:

没有答案