2017-09-28 1 views
0

Je souhaite créer un groupe d'éléments défilant à partir de rapports météo en utilisant un Adapter et un RecyclerView. Mon problème est que même si le ArrayList<> que j'utilise pour fournir des données a des données, la vue ne montre rien - et pas d'erreurs, pas de plantages. L'application démarre et rien à montrer.RecyclerView n'affiche aucun élément

Mon ActivityMain.onCreate():

private JSONParser_basic parser; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    parser = new JSONParser_basic(getResources() 
      .getString(R.string.weather_json)); 

    parser.divideToListElements(); 

    NewsRecycleAdapter adapter = new NewsRecycleAdapter(parser.getListElements()); 
    LinearLayoutManager lm = new LinearLayoutManager(this); 
    lm.setOrientation(LinearLayoutManager.VERTICAL); 

    RecyclerView rv = (RecyclerView) findViewById(R.id.recycleView); 
    rv.setLayoutManager(lm); 
    rv.setAdapter(adapter); 

} 

Notez que la JSONParser_basic est une classe simple fait de soi (hors de la pratique, pour obtenir des données à partir d'une chaîne spécifique). Je l'utilise pour obtenir des données d'une chaîne JSON et le charger dans ma collection listElements. Il fait son travail, j'obtiens les données, ce n'est pas le problème.

Ma News classe:

public class News { 

    private String max; 
    private String min; 
    private String weather; 
    private String windForce; 

    public News(String max, String min, String weather, String windForce) { 
     this.max = max; 
     this.min = min; 
     this.weather = weather; 
     this.windForce = windForce; 
    } 

    public String formReport(){ 
     return max + "/" + min + ", " 
       + weather + ", " + 
       "Wind=" + windForce; 
    } 
} 

Je téléchargé cette classe pour montrer la méthode formReport.

Mon adaptateur:

public class NewsRecycleAdapter extends RecyclerView.Adapter<NewsRecycleAdapter.NewsViewHolder> { 

    List<News> items; 

    public NewsRecycleAdapter(List<News> items) { 
     this.items = items; 
    } 

    @Override 
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType)  
    { 
     return new NewsViewHolder(View.inflate(parent.getContext(), 
       R.layout.list_element, 
       null)); 
    } 

    @Override 
    public void onBindViewHolder(NewsViewHolder holder, int position) { 
     News myNews = items.get(position); 
     holder.titleTV.setText(myNews.formReport()); 
    } 

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

    public class NewsViewHolder extends RecyclerView.ViewHolder { 
     public TextView titleTV, leadTV; 
     public NewsViewHolder(View itemView) { 
      super(itemView); 
      titleTV = (TextView) itemView.findViewById(R.id.title); 
     } 
    } 


}  

Le list_element.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="7dp"> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:maxLines="1" 

    android:text="some gibberish for now" 
    android:textStyle="bold" /> 

Et enfin mon main_activity.xml:

<LinearLayout 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" 
tools:context="com.smth.myname.jeeesoooon.MainActivity"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recycleView" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

Et je n'ai cette ligne dans mon application de build.gradle:

compile 'com.android.support:recyclerview-v7:26.0.+' 

Répondre

1

Pourquoi la hauteur et la largeur RecyclerView0dp?

changement à ce qui suit:

android:layout_width="match_parent" 
android:layout_height="wrap_content" 

En outre, dans list_element.xml, changer la hauteur de mise en page wrap_content:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
+0

Heh pourquoi je mets 0DP là, c'est un mystère. Maintenant, cela fonctionne très bien, merci pour le conseil. Eh bien j'ai fait une erreur noobish, si je dois le dire moi-même. – agiro