0

Avant de définir l'ensemble de données sur l'adaptateur, j'obtiens l'ensemble de données de la base de données Room, puis je règle les données sur l'adaptateur et définissez l'adaptateur sur le recyclerview. Il y a actuellement 2 articles dans la liste. De mes journaux, je vois que toutes les méthodes sont appelées pour le premier élément de la liste, mais pas pour la seconde, et aucun élément ou vue n'est affiché sur l'écran. Je ne suis pas sûr de ce qui se passe, quelqu'un peut-il m'aider à voir le problème? Merci d'avance pour votre aide.La vue de recyclage n'affiche pas les éléments

goals_content.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:background="#ffffff" 
    android:clickable="true" 
    android:orientation="vertical"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/goals_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layoutManager="LinearLayoutManager" 
     android:divider="@android:color/darker_gray" 
     android:dividerHeight="1px" 
     android:visibility="gone" 
     /> 

    <!--view to show if the dataset is empty--> 
    <TextView 
     android:id="@+id/emptyElement" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:visibility="visible" 
     android:text="No Goals Added" 
     android:textColor="#525252" 
     android:textSize="19sp"/> 
</LinearLayout> 

GoalsActivity.java

GoalsRecyclerViewAdapter adapter; 
RecyclerView goalsListView; 
.... 

goalsListView = (RecyclerView) findViewById(R.id.goals_list); 

.... 

private void createGoalsList() { 

     new AsyncTask<Void, Void, List<Goal>>(){ 

      @Override 
      protected List<Goal> doInBackground(Void... params) { 
       List<Goal> returnedGoals = goalDao.getAllGoals(); 
       return returnedGoals; 
      } 

      @Override 
      protected void onPostExecute(List<Goal> returnedGoals) { 

       //check your goals list is empty or not 
       if(returnedGoals.size() > 0){ 

        //make the goals list visible if there is data 
        goalsListView.setVisibility(View.VISIBLE); 
        noGoalsTxt.setVisibility(View.GONE); 

        //set array adapter for the goals list 
        adapter = new GoalsRecyclerViewAdapter(returnedGoals); 
        goalsListView.setAdapter(adapter); 
       }else{ 

        //if there is no data display empty list text. 
        goalsListView.setVisibility(View.GONE); 
        noGoalsTxt.setVisibility(View.VISIBLE); 
       } 
      } 
     }.execute(); 
    } 

GoalsRecyclerviewAdapter.java

public class GoalsRecyclerViewAdapter extends RecyclerView.Adapter<GoalsRecyclerViewAdapter.GoalsViewHolder> { 

    private static final String TAG = GoalsRecyclerViewAdapter.class.getSimpleName(); 

    private List<Goal> mGoals; 

    public GoalsRecyclerViewAdapter(List<Goal> goals) { 
     Log.d(TAG, "adapter initialized"); 
     mGoals = goals; 
    } 

    @Override 
    public GoalsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     Log.d(TAG, "onCreateViewHolder: inside"); 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goal_row, parent, false); 
     return new GoalsViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(GoalsViewHolder holder, int position) { 

     Log.d(TAG, "onBindViewHolder: position " + position); 
     holder.bind(mGoals.get(position)); 
    } 

    @Override 
    public int getItemCount() { 
     Log.d(TAG, "getItemCount: " + mGoals.size()); 
     return mGoals.size(); 
    } 

    class GoalsViewHolder extends RecyclerView.ViewHolder { 

     TextView goalsLabel, goalCount; 

     GoalsViewHolder(View itemView) { 
      super(itemView); 
      goalsLabel = (TextView) itemView.findViewById(R.id.calls_goal_label); 
      goalCount = (TextView) itemView.findViewById(R.id.goal_count); 
     } 

     void bind(Goal goal){ 
      Log.d(TAG, "bind: " + goal.toString()); 
      goalsLabel.setText(goal.getGoalTitle()); 
      goalCount.setText(String.valueOf(goal.getGoalTarget())); 
     } 
    } 

    public List<Goal> getGoals(){ 
     return mGoals; 
    } 

    public void setGoals(List<Goal> goals){ 
     mGoals = goals; 
    } 
} 

mise à jour: le plan de la ligne ajoutée goal_row.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" 
    tools:context="com.ubcma.leadster.activity.GoalsActivity" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <TextView 
     android:id="@+id/calls_goal_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="23sp" 
     android:layout_alignParentLeft="true" 
     android:layout_margin="@dimen/text_margin" 
     tools:text="@string/calls_per_week"/> 

    <TextView 
     android:id="@+id/goal_count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_margin="@dimen/text_margin" 
     android:textSize="23sp" 
     tools:text="4"/> 

</RelativeLayout> 

journaux

09-27 23:10:08.071 1606-1818/? D/ActivityManager: resumeTopActivityInnerLocked() : #1 prevTask=TaskRecord{46a33bdd0 #6227 A=com.ubcma.leadster U=0 StackId=1 sz=2} next=ActivityRecord{8396555d0 u0 com.ubcma.leadster/.activity.GoalsActivity t6227} mFocusedStack=ActivityStack{43432a4d0 stackId=1, 38 tasks} 
09-27 23:10:08.128 24667-24667/com.ubcma.leadster D/[email protected][GoalsActivity]: setView = [email protected][GoalsActivity] touchMode=true 
09-27 23:10:08.211 1606-4253/? D/WindowManager: finishDrawingWindow: Window{94ca436d0 u0 com.ubcma.leadster/com.ubcma.leadster.activity.GoalsActivity} mDrawState=DRAW_PENDING 
09-27 23:10:08.213 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: adapter initialized 
09-27 23:10:08.214 24667-24667/com.ubcma.leadster D/[email protected][GoalsActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1 
09-27 23:10:08.214 24667-24667/com.ubcma.leadster D/[email protected][GoalsActivity]: MSG_WINDOW_FOCUS_CHANGED 1 
09-27 23:10:08.225 1606-1917/? I/ActivityManager: Displayed com.ubcma.leadster/.activity.GoalsActivity: +153ms 
09-27 23:10:08.229 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: getItemCount: 2 
09-27 23:10:08.229 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: getItemCount: 2 
09-27 23:10:08.230 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: getItemCount: 2 
09-27 23:10:08.230 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: onCreateViewHolder: inside 
09-27 23:10:08.233 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: onBindViewHolder: position 0 
09-27 23:10:08.234 24667-24667/com.ubcma.leadster D/GoalsRecyclerViewAdapter: bind: Goal{id=1, goalType='r', goalTitle='Recruit', goalFrequency='Month', goalTarget=12} 
09-27 23:10:08.247 1606-12625/? D/WindowManager: finishDrawingWindow: Window{94ca436d0 u0 com.ubcma.leadster/com.ubcma.leadster.activity.GoalsActivity} mDrawState=HAS_DRAWN 
+0

J'ai mes doutes sur la layout_height être match_parent, avez-vous essayé de vérifier si l'élément est représenté en définissant une couleur de fond à cette disposition relative? – diedu

+0

@diedu Je n'ai pas essayé ça. Laisse-moi essayer. – androgirl

+0

@diedu après avoir ajouté la couleur d'arrière-plan à la disposition relative, j'ai pu voir que la vue de l'élément occupait tout l'écran.Comment savez-vous quand utiliser wrap_content ou match_parent dans recyclerview? C'est vraiment difficile à dire. ou dans les vues enfants imbriquées – androgirl

Répondre

0

hauteur de Tenue de l'élément de recycleur comme match_parent montre qu'une seule vue par écran. Changez la hauteur de votre objet recycleur de match_parent à wrap_content.

Supprimez également l'application: layout_behavior = "@ string/appbar_scrolling_view_behavior".

Vérifiez ce code mis à jour.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<TextView 
    android:id="@+id/calls_goal_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="23sp" 
    android:layout_alignParentLeft="true" 
    android:layout_margin="20dp" 
    android:text="sample" 
/> 

<TextView 
    android:id="@+id/goal_count" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_margin="20dp" 
    android:textSize="23sp" 
    tools:text="4"/> 

+0

Si elle ne montre que 1 élément sur l'écran, ne devrais-je pas au moins voir l'étiquette textview? – androgirl

+0

omg c'était tout. pouvez-vous expliquer pourquoi cela a pris tout l'écran? Je pensais que la hauteur pour la vue elle-même. – androgirl

+0

oui la hauteur de la vue elle-même causait problème que vous l'aviez donné comme match_parent qui a pour résultat que chaque élément de recycleur affiche en plein écran ... Aussi app: layout_behavior = "@ string/appbar_scrolling_view_behavior" c'était du code inutile. – Anonymous

0

Set android:visibility="visible" dans votre RecyclerView code xml

changement

<android.support.v7.widget.RecyclerView 
    android:id="@+id/goals_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layoutManager="LinearLayoutManager" 
    android:divider="@android:color/darker_gray" 
    android:dividerHeight="1px" 
    android:visibility="gone" 
    /> 

Pour

<android.support.v7.widget.RecyclerView 
    android:id="@+id/goals_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layoutManager="LinearLayoutManager" 
    android:divider="@android:color/darker_gray" 
    android:dividerHeight="1px" 
    android:visibility="visible" 
    /> 

Modifier

Ajouter layoutManager au code.

goalsListView.setLayoutManager(new LinearLayoutManager(this)); 

Et enlever app:layoutManager="LinearLayoutManager" dans le code xml.

+0

recyclerview sera visible s'il y a des données dans la liste, s'il n'y a pas de données, la vue de texte sera affichée. – androgirl

0

Vous devez spécifier gestionnaire de mise en page avant la fixation d'un adaptateur dans recyclerview comme suit

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
mRecyclerView.setLayoutManager(mLayoutManager); 

ou spécifier dans la mise en page comme suit

app:layoutManager="android.support.v7.widget.LinearLayoutManager" 
+0

le gestionnaire de mise en page est défini dans le xml – androgirl

+0

OK, mais essayez aussi par programme et supprimer de XML peut être le problème de faire quelque temps en XML. –