2016-11-10 17 views
2

Ich benutze Fragment und Recyclerview zusammen. Ich habe auch Datenbank und ich möchte Ergebnisse aus der Datenbank abfragen und die Ergebnisse innerhalb der Aktivität anzeigen.blank Recyclerview in Fragment

Aber jedes Mal, wenn ich versuche, die Anwendung zu starten und zu dem Teil zu wechseln, um die Ergebnisse zu bekommen und zu sehen, scheint ich nichts zu bekommen. Keine Ergebnisse, nur leer. Ich weiß nicht, warum es nicht auftaucht.

Das ist mein voller Code

Sample-Klasse

public class Sample extends Fragment { 
    private View rootView; 

    public Sample() {} 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 

     rootView = inflater.inflate(sample, container, false); 

     RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_sample); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     mRecyclerView.setHasFixedSize(true); 

     DatabaseHandler db= new DatabaseHandler(getActivity()); 
     List<SampleModel> list = db.getResults(); 

     SampleAdapter sampleAdapter = new SampleAdapter(list); 
     mRecyclerView.setAdapter(sampleAdapter); 

     return rootView; 
    } 
} 

Adapter Klasse

public class SampleAdapter extends RecyclerView.Adapter <SampleAdapter.ViewHolder> { 

    private List<SampleModel> list; 
    private Context mContext; 

    public SampleAdapter (List<SampleModel> list) { 
     list = list; 
    } 

    @Override 
    public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(mContext).inflate(R.layout.sample_item, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(SampleAdapter.ViewHolder holder, int position) { 
     SampleModel sample = list.get(holder.getAdapterPosition()); 
     holder.title.setText(sample.getTitle()) 
    } 

    @Override 
    public int getItemCount() { 
     return (list != null? list.size():0); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 

     public TextView title; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      title = (TextView) itemView.findViewById(R.id.title); 
     } 
    } 
} 

XML

sample.xml

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

     <include layout="@layout/sample_recyclerview" /> 

    </android.support.design.widget.CoordinatorLayout> 

sample_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView_sample" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clipToPadding="false"/> 

</RelativeLayout> 

sample_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/cardview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginRight="16dp" 
    android:focusable="true" 
    android:clickable="true" 
    android:foreground="?android:attr/selectableItemBackground" 
    card_view:cardBackgroundColor="#808080"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="16dp"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#ffffff" /> 

    </LinearLayout> 

</android.support.v7.widget.CardView> 

Antwort

0

In Ihrem SampleAdapter Änderung

list = list; 

zu

this.list = list; 
+0

das Verfahren durch den Aufruf Es ist immer noch nicht funktioniert – BXUMZSE

+0

Sie wirklich sicher sind, dass Ihre 'Liste list = db.getResults();' nicht leer ist? – Enzokie

0

Ihr Adapter Konstruktor ist falsch. Sie übergeben die dataSource nicht an Ihren Adapter. Ändern Sie ihn auf

public SampleAdapter (List<SampleModel> list) { 
     this.list = list; 
    } 

Außerdem müssen Sie den Adapter mitzuteilen, dass Sie Ihre dataSource geändert. Sie tun das, notifyDataSetChanged

SampleAdapter sampleAdapter = new SampleAdapter(list); 
mRecyclerView.setAdapter(sampleAdapter); 
sampleAdapter.notifyDataSetChanged(); 
Verwandte Themen