2016-04-09 20 views
1

AsyncTask を使用してデータを取得しようとしています。ListViewに表示されています。それは、常に0を助けてくださいListViewはデータを表示しません。getCount()は0を返します。getView()は呼び出しません。

public class SourceAdapter extends BaseAdapter { 
    private final String LOG_TAG = SourceAdapter.class.getSimpleName(); 

    Context context; 
    ArrayList<SourceObject> objects = new ArrayList<SourceObject>(); 

    public SourceAdapter(Context context) { 
     this.context = context; 
    } 

    @Override 
    public int getCount() { 
     Log.d(LOG_TAG,"getCount called "+objects.size()); 
     return objects.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     Log.d(LOG_TAG,"getItem called"); 
     return objects.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d(LOG_TAG,"get view method is called"); 
     SourceObject sourceObject = (SourceObject) getItem(position); 

     if (convertView == null){ 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.list_item_source,parent,false); 
     } 

     TextView personName = (TextView) convertView.findViewById(R.id.person_name); 
     TextView commit = (TextView) convertView.findViewById(R.id.xxx); 
     TextView commitMessage = (TextView) convertView.findViewById(R.id.commit_message); 

     personName.setText(sourceObject.getPersonName()); 
     commit.setText(sourceObject.getCommit()); 
     commitMessage.setText(sourceObject.getCommitMessage()); 

     return convertView; 
    } 
} 

SourceAdapter.java

public class MainActivityFragment extends Fragment { 

    private final String LOG_TAG = MainActivityFragment.class.getSimpleName(); 

    SourceAdapter adapter; 

    public MainActivityFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     adapter = new SourceAdapter(getActivity()); 

     ListView listView = (ListView) rootView.findViewById(R.id.listView); 
     listView.setAdapter(adapter); 
     Log.d(LOG_TAG,"after list view set to adapter"); 
     return rootView; 
    } 

    @Override 
    public void onStart() { 
     new FetchDataTask().execute(); 
     super.onStart(); 
    } 

    public class FetchDataTask extends AsyncTask<String, Void, SourceObject[]>{ 

     private final String LOG_TAG = FetchDataTask.class.getSimpleName(); 

     private SourceObject[] getSourceDataFromJson(String jsonStr)throws JSONException{ 

      JSONArray jsonArray = new JSONArray(jsonStr); 
      SourceObject[] sourceObjects = new SourceObject[jsonArray.length()]; 

      for (int i=0; i<jsonArray.length();i++){ 
       sourceObjects[i] = new SourceObject(
         jsonArray.getJSONObject(i).getJSONObject("commit").getJSONObject("author").getString("name"), 
         jsonArray.getJSONObject(i).getJSONObject("commit").getJSONObject("author").getString("name"), 
         jsonArray.getJSONObject(i).getJSONObject("commit").getString("message") 
       ); 
      } 
      return sourceObjects; 
     } 

     @Override 
     protected SourceObject[] doInBackground(String... params) { 

      HttpURLConnection urlConnection = null; 
      BufferedReader reader = null; 
      String jsonStr = null; 

      try { 
       String baseUrl = "https://api.github.com/repos/rails/rails/commits"; 

       URL url = new URL(baseUrl); 
       Log.d(LOG_TAG,"URL IS "+url); 

       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("GET"); 
       urlConnection.connect(); 

       InputStream inputStream = urlConnection.getInputStream(); 
       StringBuffer buffer = new StringBuffer(); 

       if (inputStream == null) 
        return null; 

       reader = new BufferedReader(new InputStreamReader(inputStream)); 
       String line; 
       while ((line = reader.readLine()) != null){ 
        buffer.append(line + "\n"); 
       } 

       if (buffer.length() == 0) { 
        return null; 
       } 

       jsonStr = buffer.toString(); 
       Log.d(LOG_TAG,"JSON STRING "+jsonStr); 
      }catch (IOException e){ 
       Log.e(LOG_TAG, "ERROR"+e); 
       return null; 
      }finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
       if (reader != null) { 
        try { 
         reader.close(); 
        } catch (final IOException e) { 
         Log.e(LOG_TAG, "Error closing stream", e); 
        } 
       } 
      } 
      try { 
       return getSourceDataFromJson(jsonStr); 
      } catch (JSONException e) { 
       Log.e(LOG_TAG, e.getMessage(), e); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(SourceObject[] strings) { 

      adapter.notifyDataSetChanged(); 
      super.onPostExecute(strings); 
     } 
    } 
} 

MainActivityFragment.java

私はgetCount()リターンをチェックし、getView()を呼び出すことはありません。

答えて

1

AsyncTaskから取得したデータをアダプターに設定していません。

アダプタクラスにこのメソッドを追加します。

public void setItems(SourceObject[] items) { 
    this.objects = new ArrayList<SourceObject>(); 
    for(SourceObject item : items){ 
     this.objects.add(item); 
    } 
    this.notifyDataSetChanged(); 
} 

をまたにAsyncTaskonPostExecuteを変更:

@Override 
protected void onPostExecute(SourceObject[] strings) { 
    adapter.setItems(strings); 
    super.onPostExecute(strings); 
} 
+0

はありがとうございました。出来た。 –

関連する問題