2017-11-04 3 views
2

Ich versuche, die Liste der Zeichenfolgen in der Recycleransicht anzuzeigen. Aus irgendeinem Grund funktioniert es nicht. Es zeigt nichts. Ich weiß nicht, warum es nicht funktioniert, es ist nicht das erste Mal, dass ich das benutze, normalerweise habe ich es mit Firebase Recycler gemacht.Recyclerview zeigt nichts?

Hier ist mein Code

rvTypeMusic = (RecyclerView)findViewById(R.id.rvMusicType); 
    ArrayList<String> songs = new ArrayList<String>(); 
    songs.add("PHP"); 
    songs.add("C#"); 
    songs.add("Java"); 
    songs.add("JavaScript"); 
    songs.add("Android"); 

    adapter = new TypeMusicListAdapter(this,songs); 
    rvTypeMusic.setAdapter(adapter); 
    rvTypeMusic.setLayoutManager(new GridLayoutManager(this,2)); 
} 


    public static class TypeMusicListAdapter extends 
       RecyclerView.Adapter<TypeMusicListViewHolder>{ 
    private LayoutInflater inflater; 
    private Context context; 
    private List<String> data; 

    public TypeMusicListAdapter(Context context, List<String> data){ 
     this.context = context; 
     this.inflater = LayoutInflater.from(context); 
     this.data = data; 
    } 

    @Override 
    public TypeMusicListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = inflater.inflate(R.layout.type_music_item,parent,false); 
     return new TypeMusicListViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(TypeMusicListViewHolder holder, int position) { 
     String songType = data.get(position); 
     //holder.musicType.setText(songType.getSongName()); 
     holder.musicType.setText(songType); 
    } 

    @Override 
    public int getItemCount() { 
     return 0; 
    } 
} 

public static class TypeMusicListViewHolder extends RecyclerView.ViewHolder { 
    private TextView musicType; 

    public TypeMusicListViewHolder(View itemView) { 
     super(itemView); 
     musicType = (TextView) itemView.findViewById(R.id.musicType); 
    } 
} 

XML Datei

 <android.support.constraint.ConstraintLayout 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" 
tools:context="com.world.bolandian.talent.MainActivity" 
tools:showIn="@layout/app_bar_main"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/rvMusicType" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginBottom="0dp" 
    android:layout_marginLeft="0dp" 
    android:layout_marginRight="0dp" 
    android:layout_marginTop="0dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    tools:listitem="@layout/type_music_item"> 

</android.support.v7.widget.RecyclerView> 
</android.support.constraint.ConstraintLayout> 

XML Artikel

<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="wrap_content" 
    android:layout_height="wrap_content"> 


    <TextView 
     android:id="@+id/musicType" 
     android:padding="25dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:textAppearance="@style/TextAppearance.AppCompat.Display1" /> 
</android.support.v7.widget.CardView> 

</LinearLayout> 

Was bin ich hier fehlt? Ich habe einige Dinge ausprobiert, aber nichts hat funktioniert.

Antwort

1

Problem ist in dieser Zeile.

@Override 
public int getItemCount() { 
    return 0; 
} 

Es sollte

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

arbeitete wie eine Magie! –

0

Fügen Sie diese Zeile nach dem Initialisieren der Recyclingansicht hinzu.

rvTypeMusic.setLayourManager(new LinearLayoutManager()); 

Es wird dann funktionieren.

+0

kehrt ich die Ergebnisse in einem Raster angezeigt werden soll –

+0

Oh ja auch die getItemCount() gesetzt zu –

+0

LinearLayoutManager ist für vertikale Liste. Es ist klar, dass es für Grid Items ist. – ADM

1

die Antwort ist Mischung @abdur Rahman und Zuerst das @ ADM wir das Layout Manager auf RecyclerView, bevor Sie den Adapter wie folgt gesetzt werden müssen.

weil die Recycler-Ansicht beginnt, die Ansichten zu legen, sobald wir den Adapter so eingestellt haben, dass er bis dahin den Layout-Manager haben muss. Zweitens dieser

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

onCreateViewHolder und onBindViewHolder wird nur aufgerufen, wenn es nicht den Wert Null