2017-10-17 16 views
0

サーバから取得したデータを表示しようとしていますが、取得したデータをrecyclerViewに表示できません。データは有効であり、解析(JSON Parsing)は正常に機能していますが、私のアクティビティは白い画面しか表示しません。 いくつかのダミーデータアプリケーションでアプリケーションを実行すると、問題はサーバーから解析したデータを設定しようとすると発生します。ここ は、次の2つの方法上記のコードloadDummyData();loadData();何ら問題がない第一の方法と試みるがある見ることができる私MainClassRecyclerViewのアイテムを動的データで表示できません

public class JSONDataViewer extends AppCompatActivity { 

    Context context; 
    // private List<Movie> movieList 
    private RecyclerView mRecyclerView; 
    private CustomAdapter mAdapter; 
    private List<MyData> dataList = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_jsondata_viewer); 
     this.context = this; 
     AndroidNetworking.initialize(getApplicationContext()); 

     mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 
//  mRecyclerView.setHasFixedSize(true); 

//  loadDummyData(); 
     loadData(); 
     Log.e("DataListPopulated", "Data list populated"); 
     mAdapter = new CustomAdapter(dataList); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     mRecyclerView.setLayoutManager(mLayoutManager); 
     mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
     Log.e("SettingAdapter", "Setting Adapter"); 
     mRecyclerView.setAdapter(mAdapter); 
     Log.e("AdapterSet", "Adapter Set Success"); 
    } 

    private void loadDummyData() { 

     String question = "Question "; 
     int qNo = 1; 
     for (int i = 0; i < 10; i++) { 
      qNo++; 
      MyData data = new MyData("" + qNo, question + qNo); 
      Log.e("Question" + " " + qNo, 
        "\nQuestion id = " + data.getQuestionId() + 
          " Question text = " + data.getQuestionText()); 
      dataList.add(data); 

     } 
    } 

    private void loadData() { 
     AndroidNetworking.post("http://www.skafs.com/survey/api/index.php") 
       .addBodyParameter("tag", "getquestion") 
       .addBodyParameter("category_id", "2") 
       .setPriority(Priority.MEDIUM) 
       .build() 
       .getAsJSONObject(new JSONObjectRequestListener() { 
        @Override 
        public void onResponse(JSONObject response) { 
         Log.i("LoadingData", "" + "Successful" + response); 
         Toast.makeText(context, "Response:\n" + "Loading Data" + response, 
           Toast.LENGTH_LONG).show(); 
         boolean success = response.optBoolean("success"); 
         if (success) { 
          Log.e("InsideSuccess", "Success = " + success); 
          try { 
           JSONArray questions = 
             response.optJSONArray("questions"); 
           Log.e("Questions", questions.toString()); 
           for (int i = 0; i < questions.length(); i++) { 
            JSONObject obj = questions.getJSONObject(i); 
            MyData data = new MyData(
              obj.getString("question_id"), 
              obj.getString("question_text")); 
            Log.e("Question" + " " + i, 
              "\nQuestion id = " + data.getQuestionId() + 
                " Question text = " + data.getQuestionText()); 
            dataList.add(data); 
           } 
           Log.e("ListData", "List Size is = "+dataList.size()); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 

        @Override 
        public void onError(ANError anError) { 
         anError.printStackTrace(); 
         Log.e("ParsingJSON", "" + "Failed getting json"); 
        } 
       }); 
    } 
} 

です。
私は、サーバーからのデータが有効であることを示しているとDataListコントロールは、ここ9 の大きさを持っているいくつかのログ・ステートメントを持っている私のデータは、私が何か間違ったことをやっている方向に私を指すしてくださいログイン

10-17 21:25:25.217 3078-3078/hostflippa.com.opencart_android E/InsideSuccess: Success = true 
10-17 21:25:25.219 3078-3078/hostflippa.com.opencart_android E/Questions: [{"question_id":"1","question_text":"License Status","question_type":"radio","options":[{"option_id":"1","option_text":"Valid","option_value":"5"},{"option_id":"2","option_text":"Expired","option_value":"0"},{"option_id":"3","option_text":"Without License","option_value":"0"}]},{"question_id":"2","question_text":"License date of validity","question_type":"editText","options":[]},{"question_id":"3","question_text":"Type of License","question_type":"radio","options":[{"option_id":"4","option_text":"A-Category","option_value":"5"},{"option_id":"5","option_text":"B-Category","option_value":"2"},{"option_id":"6","option_text":"C-Category","option_value":"2"},{"option_id":"7","option_text":"Dispensar","option_value":"1"},{"option_id":"457","option_text":"20\/21","option_value":"1"}]},{"question_id":"4","question_text":"License Ownership","question_type":"radio","options":[{"option_id":"8","option_text":"Individual","option_value":"0"},{"option_id":"9","option_text":"Chain Pharmacy","option_value":"0"},{"option_id":"10","option_text":"Company","option_value":"0"}]},{"question_id":"5","question_text":"License Number","question_type":"editText","options":[]},{"question_id":"6","question_text":"Computerized License No","question_type":"editText","options":[]},{"question_id":"7","question_text":"NTN Present","question_type":"checkbox","options":[{"option_id":"11","option_text":"Yes","option_value":"1"},{"option_id":"12","option_text":"No","option_value":"0"}]},{"question_id":"8","question_text":"NTN No.","question_type":"editText","options":[]},{"question_id":"9","question_text":"Visit Type","question_type":"radio","options":[{"option_id":"13","option_text":"First visit","option_value":"0"},{"option_id":"14","option_text":"Second visit","option_value":"0"}]}] 
10-17 21:25:25.219 3078-3078/hostflippa.com.opencart_android E/Question 0: Question id = 1 Question text = License Status 
10-17 21:25:25.219 3078-3078/hostflippa.com.opencart_android E/Question 1: Question id = 2 Question text = License date of validity 
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 2: Question id = 3 Question text = Type of License 
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 3: Question id = 4 Question text = License Ownership 
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 4: Question id = 5 Question text = License Number 
10-17 21:25:25.220 3078-3078/hostflippa.com.opencart_android E/Question 5: Question id = 6 Question text = Computerized License No 
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/Question 6: Question id = 7 Question text = NTN Present 
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/Question 7: Question id = 8 Question text = NTN No. 
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/Question 8: Question id = 9 Question text = Visit Type 
10-17 21:25:25.221 3078-3078/hostflippa.com.opencart_android E/ListData: List Size is = 9 

を形成しています。

私はです。例外はありません。ここに私のCustomAdapterクラス

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> { 

    private List<MyData> dataList; 

    public CustomAdapter(List<MyData> dataList) { 
     this.dataList = dataList; 
     Log.e("Constructor", "Working"); 
     Log.e("Constructor", "DataList Size = " + dataList.size()); 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.card_lay, parent, false); 
     Log.e("LayoutInflated", "Working"); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     Log.e("OnBIndMethod", "OnBind Working"); 
     MyData data = dataList.get(position); 
     holder.questionId.setText(data.getQuestionId()); 
     holder.questionText.setText(data.getQuestionText()); 
    } 

    @Override 
    public int getItemCount() { 
     return dataList.size(); 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder { 

     public TextView questionId, questionText; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      questionId = (TextView) itemView.findViewById(R.id.question_id); 
      questionText = (TextView) itemView.findViewById(R.id.question_text); 
      Log.e("FindViewById", "Working"); 
     } 
    } 
} 

MyDataクラスには、2種類の文字列のフィールドとそのgetter/setterメソッドを持っています。

答えて

3
for (int i = 0; i < questions.length(); i++) { 
           JSONObject obj = questions.getJSONObject(i); 
           MyData data = new MyData(
             obj.getString("question_id"), 
             obj.getString("question_text")); 
           Log.e("Question" + " " + i, 
             "\nQuestion id = " + data.getQuestionId() + 
               " Question text = " + data.getQuestionText()); 
           dataList.add(data); 
          } 
// notify data set change call missing 
mAdapter.notifyDataSetChanged(); 
+1

または適切な任意のは、あなたが私の一日保存方法https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html – Raghunandan

+0

@Such感謝を知らせます。私たちがダミー値をロードする場合、ちょうど興味深いホエイのrecyclerViewはこのメソッドなしで動作しますか? –

+0

ほんの少しのダンプミスが私の一日を辛らつにしました。 oh –

関連する問題