2016-08-24 11 views
1

Die Daten werden nicht auf dem Android-Bildschirm angezeigt und es wird auch kein Fehler in LogCat angezeigt. In LogCat werden die Daten angezeigt, aber nicht auf dem Bildschirm angezeigt. Ich habe viele Dinge durchsucht, aber dafür keine Antwort bekommen. Ich verwende RecyclerView, um die Daten anzuzeigen. HierDaten werden nicht angezeigt, wenn recyclerView verwendet wird

ist mein Code - MainActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second_activity); 
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    gAdapter = new GetJobDetailsAdapter(detailsJob); 
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    recyclerView .setLayoutManager(mLayoutManager); 
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    String url = "url" 

    AQuery mAQuery = new AQuery(SecondActivity.this); 
    mAQuery.ajax(url, String.class, new AjaxCallback<String>() { 
     @Override 
     public void callback(String url, String data, AjaxStatus status) { 
      super.callback(url, data, status); 
      if (null != data && status.getCode() != -101) { 

       String StringData = "" + data; 
       try { 
        JSONArray rootArray = new JSONArray(StringData); 
        int len = rootArray.length(); 
        for (int i = 0; i < len; ++i) { 
         JSONObject json = rootArray.optJSONObject(i); 

         GetJobDetailsJobs b1 = new GetJobDetailsJobs(); 
         b1.About_Company = json.optString("Req_Additional_Details"); 
         b1.Designation = json.optString("Req_Designation_Role"); 
         b1.Eligibility = json.optString("JobTitle"); 
         b1.JobLocation = json.optString("Job_Location"); 
         b1.JobDescription=json.optString("Req_Project_Desc"); 


         detailsJob.add(b1); 


        } 
       } catch (JSONException e) { 
        Toast.makeText(SecondActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 

       } 
      } 
     } 
    }); 
} 

Und unten ist mein Adapter Code:

public class GetJobDetailsAdapter extends RecyclerView.Adapter<GetJobDetailsAdapter.MyViewHolder> { 
    public List<GetJobDetailsJobs> detailsJob; 
    public class MyViewHolder extends RecyclerView.ViewHolder{ 

     public TextView About_Company,Designation,Eligibility,JobLocation,JobDescription,KeySkills,EventDate,EventLocation,Salary,Comp_Name; 
     public MyViewHolder(View view){ 
      super(view); 
      About_Company=(TextView) view.findViewById(R.id.About_Company); 
      Designation=(TextView) view.findViewById(R.id.Designation); 
      Eligibility=(TextView) view.findViewById(R.id.Eligibility); 
      JobLocation=(TextView) view.findViewById(R.id.JobLocation); 
      JobDescription=(TextView) view.findViewById(R.id.JobDescription); 
      KeySkills=(TextView) view.findViewById(R.id.KeySkills); 
      EventDate=(TextView) view.findViewById(R.id.EventDate); 
      EventLocation=(TextView) view.findViewById(R.id.EventLocation); 
      Salary=(TextView) view.findViewById(R.id.Salary); 
      Comp_Name=(TextView) view.findViewById(R.id.Comp_Name); 
     } 
    } 
    public GetJobDetailsAdapter(List<GetJobDetailsJobs>detailsJob){ 
     this.detailsJob=detailsJob; 
    } 
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.jobdetails, parent, false); 

     return new MyViewHolder(itemView); 
    } 
    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     GetJobDetailsJobs b1 = detailsJob.get(position); 
     holder.About_Company.setText(b1.getAbout_Company()); 
     holder.Designation.setText(b1.getDesignation()); 
     holder.Eligibility.setText(b1.getEligibility()); 

    } 

    @Override 
    public int getItemCount() { 
     return detailsJob.size(); 
    } 
} 
+0

bitte vollständigen Adaptercode hinzufügen – HourGlass

+0

Haben Sie 'genannt notifyDataSetChanged()' nach Hinzufügen von Elementen zur Liste? – Emil

+2

fügen Sie bitte 'recyclerview.setadapter (gAdapter)' nach 'detailsJob.add (b1);' in 'Callback' Methode –

Antwort

0

Sie sind nicht Adapter Einstellung in Ihrem Code recyclerview. "recyclerView.setAdapter (gAdapter)" setze es entweder in onCreate oder in Ajax Callback.

Nachdem Sie die JobDetails von Ajax Callback erhalten, müssen Sie es auf Adapter festlegen.

Dann wird es die Daten festlegen und notifyDataSetChanged aufrufen.

Adapter: Dies sollte ein setJobDetailsAdapter sein.

public void setJobDetailsAdapterData(List<GetJobDetailsJobs>detailsJob){ 
     this.detailsJob=detailsJob; 
     notifyDataSetChanged(); 
    } 
+0

bereits gesetzt, aber es zeigt auch nicht – tiya

+0

Aufruf' notifyDataSetChanged(); 'im Adapter Konstruktor doesn tu es dir nicht gut. Sie sollten es nach dem Hinzufügen von Elementen zur Liste aufrufen. – Emil

+0

@Boss ja, es sollte eine Methode sein, wo ich SetData und dann notifyDataSetChanged aufrufen muss. – HourGlass

0

Sie haben vergessen, zwei Dinge 1. Ihren Adapter 2. und rufen Sie die notifyDataSetChanged gesetzt, nachdem neues Element hinzugefügt

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second_activity); 
     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     gAdapter = new GetJobDetailsAdapter(detailsJob); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView .setLayoutManager(mLayoutManager); 
     recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(gAdapter) ; //You must set your adapter 
     String url = "url" 

     AQuery mAQuery = new AQuery(SecondActivity.this); 
     mAQuery.ajax(url, String.class, new AjaxCallback<String>() { 
      @Override 
      public void callback(String url, String data, AjaxStatus status) { 
       super.callback(url, data, status); 
       if (null != data && status.getCode() != -101) { 

        String StringData = "" + data; 
        try { 
         JSONArray rootArray = new JSONArray(StringData); 
         int len = rootArray.length(); 
         for (int i = 0; i < len; ++i) { 
          JSONObject json = rootArray.optJSONObject(i); 

          GetJobDetailsJobs b1 = new GetJobDetailsJobs(); 
          b1.About_Company = json.optString("Req_Additional_Details"); 
          b1.Designation = json.optString("Req_Designation_Role"); 
          b1.Eligibility = json.optString("JobTitle"); 
          b1.JobLocation = json.optString("Job_Location"); 
          b1.JobDescription=json.optString("Req_Project_Desc"); 


          detailsJob.add(b1); 

          gAdapter.notifyDataSetChanged(); //call notifyDataSetChanged , every time you add an item 
         } 
        } catch (JSONException e) { 
         Toast.makeText(SecondActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 

        } 
       } 
      } 
     }); 
    } 
} 
+0

'RecyclerView' enthält keine Methode' notifyDataSetChanged() '. Sie sollten 'adapter.notifyDataSetChanged() 'aufrufen' – Emil

+0

ja, es wurde ... recyclerview.setadapter (gAdapter) nach detailsJob.add (b1) hinzugefügt; – tiya

+0

@Boss, tut mir leid, mein Fehler, ich sollte es tun. Ich bearbeite meine Antwort. Danke, dass du es aufgezeigt hast. –

Verwandte Themen