2016-04-09 80 views
1

我試圖使用AsyncTask &顯示數據顯示爲ListView。它從來沒有要求getView(),我查getCount()回報總是0ListView不顯示任何數據,getCount()返回0,getView()永遠不會調用

MainActivityFragment.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); 
     } 
    } 
} 

SourceAdapter.java

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

請幫助。

回答

1

您沒有將從AsyncTask檢索的數據設置爲適配器。

此方法添加到您的適配器類:

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

,並更改onPostExecuteAsyncTask到:

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

太感謝你了。有效。 –

相關問題