无法填充数组NewsData [] NewsData_data

时间:2014-01-03 18:02:34

标签: android

通过示例编写Custom ArrayAdapter:http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter 我不能在循环中填充数组。 工作实例:

   Weather weather_data[] = new Weather[]
            {   new Weather("http://www.ezzylearning.com/images/ImagesNew/net_framework.png", "Cloudy"),
                new Weather("http://www.ezzylearning.com/images/ImagesNew/net_framework.png", "Showers")                    
            };

我的代码:

    NewsData[] NewsData_data;
     // build hash set for list view
     public void ListDrwaer() {
      try {
       JSONObject jsonResponse = new JSONObject(jsonResult);
       JSONArray jsonMainNode = jsonResponse.optJSONArray("news");
       for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        String header = jsonChildNode.optString("header");
        String short_text = jsonChildNode.optString("short_text");
        String team = jsonChildNode.optString("team");
        String datatime = jsonChildNode.optString("datatime");
        String photo_url = jsonChildNode.optString("photo_url");

        NewsData_data[i] = new NewsData(header, short_text, team, datatime, photo_url);                   
       }
      } catch (JSONException e) {
       Toast.makeText(getActivity(), "Error" + e.toString(),
         Toast.LENGTH_SHORT).show();
      }       

      NewsDataAdapter adapter = new NewsDataAdapter(getActivity(), 
                        R.layout.news_details, NewsData_data);                  
                listView.setAdapter(adapter);
     }

2 个答案:

答案 0 :(得分:0)

您需要初始化此数组:

NewsData[] NewsData_data;
 // build hash set for list view
 public void ListDrwaer() {
  try {
   JSONObject jsonResponse = new JSONObject(jsonResult);
   JSONArray jsonMainNode = jsonResponse.optJSONArray("news");

   NewsData_data=new NewsData[jsonMainNode.length()];//<------------HERE

   for (int i = 0; i < jsonMainNode.length(); i++) {
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    String header = jsonChildNode.optString("header");
    String short_text = jsonChildNode.optString("short_text");
    String team = jsonChildNode.optString("team");
    String datatime = jsonChildNode.optString("datatime");
    String photo_url = jsonChildNode.optString("photo_url");

    NewsData_data[i] = new NewsData(header, short_text, team, datatime, photo_url);                   
   }
  } catch (JSONException e) {
   Toast.makeText(getActivity(), "Error" + e.toString(),
     Toast.LENGTH_SHORT).show();
  }       

  NewsDataAdapter adapter = new NewsDataAdapter(getActivity(), 
                    R.layout.news_details, NewsData_data);                  
            listView.setAdapter(adapter);
 }

答案 1 :(得分:0)

简化回答:您的阵列未初始化。更长的答案,您必须知道它有多大,然后将其初始化为该大小。一个简单的例子:

    NewsData[] NewsData_data;
    ArrayList<NewsData> newsDataArray = new ArrayList<NewsData>();

    String header =  "header";
    String short_text = "short_text";
    String team = "team";
    String datatime = "datatime";
    String photo_url = "photo_url";

    newsDataArray.add(new NewsData(header, short_text, team, datatime, photo_url));
    newsDataArray.add(new NewsData(header, short_text, team, datatime, photo_url));

    NewsData_data = new NewsData[newsDataArray.size()];
    newsDataArray.toArray(NewsData_data);

    System.out.println(NewsData_data);