2017-10-23 11 views
1
public class GithubTab extends Fragment implements AdapterView.OnItemClickListener { 

    ListView repoListView; 

    private ListAdapter adapter; 
    private List<RepositoryItem> repoListItems; 

    private List<String> repoNameList; 
    private List<String> userNameList; 
    private List<String> descriptionList; 

    private TextView tvData; 

    private static final String TAG = "Github Tab"; 

    Button buttonHit; 
    TextView resultText; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.github_tab, container, false); 

     repoListView = (ListView) view.findViewById(R.id.repoList); 

     repoListItems = new ArrayList<>(); 
     repoNameList = new ArrayList<>(); 
     userNameList = new ArrayList<>(); 
     descriptionList = new ArrayList<>(); 

     adapter = new ListAdapter(getContext(), repoListItems); 
     repoListView.setAdapter(adapter); 
     tvData = (TextView) view.findViewById(R.id.tvJsonItem); 

     // Clickable: able to open the GitHub webpage of the re 

     repoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getContext(), "Clicked id" + view.getTag(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     new JSONTask().execute("https://api.github.com/users/whyjay17/repos"); 

     for(int i = 0; i < repoNameList.size(); i++) { 
      repoListItems.add(new RepositoryItem(i, repoNameList.get(i), userNameList.get(i), "ddd")); 
     } 

     return view; 

    } 

    public class JSONTask extends AsyncTask<String, String, String> { 
     @Override 

     // Any non-UI thread process is running in this method. After completion, it sends the result to OnPostExecute 
     protected String doInBackground(String... params) { 

      HttpURLConnection connection = null; 
      BufferedReader reader = null; 

      try { 

       .... Code Hidden .... 

       return retreivedJson; 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       //cant close null 

       if (connection != null) { 
        // close both connection and the reader 
        connection.disconnect(); 
       } 
       try { 
        if (reader != null) { 
         reader.close(); 
        } 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      return null; 

     } 


     public void formatJSONArray(String results){ 
      try { 
       JSONArray jsonArray = new JSONArray(results); 
       for(int i = 0; i < jsonArray.length(); i++){ 
        JSONObject jsonObject=jsonArray.getJSONObject(i); 
        if(jsonObject.optString("name") != null) { 
         //tvData.append(jsonObject.getString("name")); 
         repoNameList.add(jsonObject.getString("name")); 

         //Toast.makeText(getContext(), "1 " + repoNameList.get(1), Toast.LENGTH_SHORT).show(); 
        } 

        if(jsonObject.optJSONObject("owner") != null){ 
         JSONObject ownerObject=jsonObject.getJSONObject("owner"); 

         if(ownerObject.optString("login")!=null) { 
          //tvData.append(ownerObject.getString("login")); 
          userNameList.add(ownerObject.getString("login")); 
          //ownerObject.append(ownerObject.getString("avatar_url")); 
         } 
        } 
       } 


      }catch (JSONException jsonException){ 

      } 
     } 

     /* 
     * Called after the background computation finishes. Result of doInBackground is passed in as a parameter. 
     * 
     * */ 
     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

     /* for JSONArray data*/ 
      if(result!=null && !result.isEmpty()) { 
       formatJSONArray(result); 

      } 

     } 
    } 
} 
基本的に上記のコードは https://api.github.com/users/famous/reposからJSONデータを解析しようと

を表示、対応するリストに(レポ名、ID、説明)いくつかの特定の情報を追加し、それを表示しようと私が作成したリストビューに。リストビューに解析されたJSONを追加し、それに

listViewは情報をハードコードすると(listView自体に問題はない)、リスト内のデータを解析しようとすると(解析されたJSON情報が含まれています実際にはリストの中に入っています)、それは私に空のリストを与えます。

どうすればこの作品を作成できますか?

+0

を無効にするnotifyDatasetChange()を呼び出し、その後、formatJSONArray()方法の後、onPostExecute()にListViewコントロールアダプタに要素を追加したコードを移動する必要があるが)'後に新しいを追加します要素? – nhoxbypass

答えて

3

データがとなりましたonCreateView()リストデータがまだアダプタに追加されていない可能性があります。お使いのアダプタの

あなたは(notifyDataSetChanged `電話したのListView

@Override 
    protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      /* for JSONArray data*/ 
      if(result!=null && !result.isEmpty()) { 
       formatJSONArray(result); 
       for(int i = 0; i < repoNameList.size(); i++) { 
        repoListItems.add(new RepositoryItem(i, 
        repoNameList.get(i), userNameList.get(i), "ddd")); 
       } 

       adapter.notifyDatasetChanged(); 
      } 
    } 
+0

ありがとう、助けて! – user6792790

+0

@ user6792790ようこそ。ハッピーコーディング。 – nhoxbypass

1
for(int i = 0; i < repoNameList.size(); i++) { 
    repoListItems.add(new RepositoryItem(i,repoNameList.get(i),userNameList.get(i), "ddd")); 
} 
adapter.notifyDataSetChanged(); 

` あなたのformatJSONArray方法でadapter.notifiDatasetchange()を呼び出すことができます

}catch (JSONException jsonException){ 
1

前に次の行を追加します。それが機能しない場合は

public void formatJSONArray(String results){ 
     try { 
      JSONArray jsonArray = new JSONArray(results); 
      for(int i = 0; i < jsonArray.length(); i++){ 
       JSONObject jsonObject=jsonArray.getJSONObject(i); 
       if(jsonObject.optString("name") != null) { 
        //tvData.append(jsonObject.getString("name")); 
        repoNameList.add(jsonObject.getString("name")); 

        //Toast.makeText(getContext(), "1 " + repoNameList.get(1), Toast.LENGTH_SHORT).show(); 
       } 

       if(jsonObject.optJSONObject("owner") != null){ 
        JSONObject ownerObject=jsonObject.getJSONObject("owner"); 

        if(ownerObject.optString("login")!=null) { 
         //tvData.append(ownerObject.getString("login")); 
         userNameList.add(ownerObject.getString("login")); 
         //ownerObject.append(ownerObject.getString("avatar_url")); 
        } 
       } 
      } 
     adapter.notifiDatasetchange(); 

     }catch (JSONException jsonException){ 

     } 
    } 

、あなたをフォーマットJSONArrayメソッド

でアダプタを再度設定できます
adapter = new ListAdapter(getContext(), repoListItems); 
repoListView.setAdapter(adapter); 

私のために働いた。私はそれがあなたの問題を助けることを願っています

関連する問題