2016-07-09 6 views
1

Ich benutze Recyclerview in fragmnet.
Fragmnet funktioniert, aber meine Recycler-Daten werden nicht angezeigt.

Mein xml-Code:onCreateViewHolder ruft nicht in fragmnet recyclerview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> 
<android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp"> 
    <LinearLayout android:id="@+id/projectcommends" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" android:padding="@dimen/common_padding"> 
     <TextView style="@style/TextHeading" android:text="Project comments" /> 
     <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> 
      <TextView android:layout_width="180dp" android:layout_height="wrap_content" android:text="Date" android:textSize="17dp" android:textColor="@color/black" android:gravity="center_horizontal" android:textStyle="bold" /> 
      <TextView android:layout_width="180dp" android:layout_height="wrap_content" android:text="Comments" android:textSize="17dp" android:textColor="@color/black" android:gravity="center_horizontal" android:textStyle="bold" /> 
     </LinearLayout> 
     <android.support.v7.widget.RecyclerView android:id="@+id/commendsList" android:layout_width="match_parent" android:layout_height="wrap_content" /> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

Row xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_marginTop="5dp"> 
<TextView android:id="@+id/commentdate" android:layout_width="180dp" android:textSize="15dp" android:layout_height="wrap_content" android:text="20-06-2016" android:gravity="center" /> 
<TextView android:id="@+id/commentdetail" android:layout_width="180dp" android:layout_height="wrap_content" android:textSize="15dp" android:text="project commends testing list" android:gravity="center" /> 

Mein Fragmnet Code:

public class ProjectComments extends Fragment { 
    private List <CommentsData> arraylist; 
    private RecyclerView recyclerView; 
    private RecyclerView.Adapter adapter; 
    private RecyclerView.LayoutManager layoutManager; 
    private String ordered; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.project_comments, container, false); 
     initialize(view); 
     Bundle b = getArguments(); 
     orderid = b.getString("orderid"); 
     recyclerView.setHasFixedSize(true); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     arraylist = new ArrayList < >(); 
     adapter = new CommentsAdapter(arraylist); 
     recyclerView.setAdapter(adapter); 
     getCommentsData(); 
     return view; 
    } 

    private void initialize(View view) { 
     recyclerView = (RecyclerView) view.findViewById(R.id.commendsList); 
    } 

    private void getCommentsData() { 
     String mobilecustomertoken = SharedPreferencesManager.readPreferenceString("MobileCustomerToken", "D/N"); 
     JSONObject commentData = new JSONObject(); 
     try { 
     commentData.put("mobilecustomertoken", mobilecustomertoken); 
     commentData.put("orderid", orderid); 
     JsonObjectRequest commentsObject = new JsonObjectRequest(1, Common.CommentsList, commentData, new Response.Listener <JSONObject>() { 
      @Override 
      public void onResponse(JSONObject commentsResponse) { 
       Log.d("Responsedetail", commentsResponse.toString()); 
       try { 
        int status = commentsResponse.getInt("status"); 
        if (status == 1) { 
        commentsProgress(commentsResponse); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       error.printStackTrace(); 
       Log.d("Response", "PENDING ERROR"); 
      } 
     }); 
     commentsObject.setShouldCache(false); 
     ServiceBellApp.getInstance().addToRequestQueue(commentsObject); 
     } catch (JSONException localJSONException) { 
     localJSONException.printStackTrace(); 
     return; 
     } 
    } 

    private void commentsProgress(JSONObject commentsResponse) throws JSONException, InterruptedException { 
     JSONArray result = commentsResponse.getJSONArray("response"); 
     CommentsData orderListModule; 
     for (int i = 0; i < result.length(); i++) { 
     JSONObject jsonObject = result.getJSONObject(i); 
     orderListModule = new CommentsData(); 
     orderListModule.setCommentsData(jsonObject.getString("commentdate")); 
     orderListModule.setCommentsDetail(jsonObject.getString("comendDetail")); 
     arraylist.add(orderListModule); 
     } 
     adapter.notifyDataSetChanged(); 
     int count = adapter.getItemCount(); 
     if (count != 0) { 
     Log.d("Response", "Test"); 
     } 
    } 
} 

Finale mein Adapter Code:

public class CommentsAdapter extends RecyclerView.Adapter <CommentsAdapter.RecyclerViewHolder> { 
    List <CommentsData> arrayList; 
    public CommentsAdapter(List <CommentsData> list) { 
     this.arrayList = list; 
    } 

    @Override 
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comments_list, parent, false); 
     RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view); 
     return recyclerViewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerViewHolder holder, int position) { 
     CommentsData commentsData = arrayList.get(position); 
     Log.d("ResponseAdapter", commentsData.getCommentsData()); 
     holder.commentdate.setText(commentsData.getCommentsData()); 
     holder.commentDetail.setText(commentsData.getCommentsDetail()); 
    } 

    @Override 
    public int getItemCount() { 
     String dd = Integer.toString(arrayList.size());Log.d("Responsecount", dd);return (null != arrayList ? arrayList.size() : 0); 
    } 

    public class RecyclerViewHolder extends RecyclerView.ViewHolder { 
     public TextView commentdate, commentDetail; 
     public RecyclerViewHolder(View itemView) { 
     super(itemView); 
     Log.d("ResponseAda", "Success"); 
     commentdate = (TextView) itemView.findViewById(R.id.commentdate); 
     commentDetail = (TextView) itemView.findViewById(R.id.commentdetail); 
     } 
    } 
} 

Edited:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.project_comments, container, false); 
    initialize(view); 
    Bundle b = getArguments(); 
    orderid = b.getString("orderid"); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new 
      LinearLayoutManager(getActivity())); 
    arraylist = new ArrayList < >(); 
    getCommentsData(); 
    adapter = new CommentsAdapter(arraylist); 
    recyclerView.setAdapter(adapter); 
    return view; 
} 

Jeder mir bitte helfen, warum meine onCreateViewHolder Funktion nicht aufrufen?

+0

an Adapter zu setzen versuchen, Haben Sie Daten in der Arraylist, die Sie auf Ihren Adapter einstellen? – Raghavendra

+0

Ja, ich habe Arraylist Daten. getCount ist 1 – vinoth12594

+0

Kann diesen recyclerView.setAdapter (Adapter) ausprobieren; Diese Zeile vor dem Aufruf adapter.notifyDatasetChanged und versuchen Sie es einmal – Raghavendra

Antwort

0

Können Sie ersetzen Setzen Sie den obigen Code Schnipsel und die Prüfung wiederholen, ich denke, Sie sind nicht die Daten an, während Einstellung es vorbei, verstopfen die Datensatzes Sie

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.project_comments, container, false); 
    initialize(view); 
    Bundle b = getArguments(); 
    orderid = b.getString("orderid"); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new 
    LinearLayoutManager(getActivity())); 
    arraylist = new ArrayList < >(); 
    getCommentsData(); 
    adapter = new CommentsAdapter(arraylist); 
    recyclerView.setAdapter(adapter); 
    return view; 

}

+0

Ich habe versucht, nicht funktioniert – vinoth12594

+0

Haben Sie die Daten vor der Einstellung zu Adapter ist es sogar dort? – Haroon

Verwandte Themen