2017-10-17 22 views
0

Ich versuche, einige Daten anzuzeigen, die ich vom Server abrufe. Ich kann die abgerufenen Daten jedoch nicht in recyclerView anzeigen. Daten sind gültig und das Parsen (JSON Parsing) funktioniert gut, aber meine Aktivität zeigt nur einen weißen Bildschirm. Wenn ich die App mit einigen Dummy-Daten App funktioniert, funktioniert das Problem, wenn ich versuche, Daten, die ich vom Server analysiert habe, zu setzen. Hier ist mein MainclassEinträge in RecyclerView mit dynamischen Daten können nicht angezeigt werden

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

Sie gibt es zwei Methoden der obige Code loadDummyData(); und loadData(); mit ersten Methode Versuch sehen kann, gibt es kein Problem.
Ich habe einige Log-Anweisungen, die Daten vom Server zeigt gültig ist und Datalist hat eine Größe von 9 Hier wird bilden meine Daten Log

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 

mich zeigen Sie bitte die Richtung, wo ich etwas falsch tue.

Ich bin keine Ausnahme bekommen. Hier ist meine CustomAdapter Klasse

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 Klasse hat zwei Felder vom Typ String und deren Getter/Setter-Methoden.

Antwort

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

oder eine der entsprechenden Methoden benachrichtigen https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html – Raghunandan

+0

@Such Dank Sie meinen Tag gerettet. Nur neugierig Whey RecyclerView funktionieren ohne diese Methode, wenn wir Dummy-Werte laden? –

+0

Nur ein kleiner Dump Fehler machte meinen Tag hart. oh –

Verwandte Themen