2017-07-25 4 views
0

Je travaille dans un recycleurView j'essaye de faire un recycleurView qui a 2 disposition mais le problème est que quand je cours l'émulateur et clique sur le bouton pour montrer des articles dans le recyclerview l'application se ferme tout à coup est ici le code de l'adaptateurMon application se ferme soudainement avant de montrer RecyclerView

package com.example.abdelmagied.myapplication; 

import android.content.Context; 
import android.database.Cursor; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.util.ArrayList; 

/** 
* Created by AbdELMagied on 7/25/2017. 
*/ 
public class recycleradapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 


    public Context context; 
    public Cursor mycursor; 
    public ArrayList<String> mydata; 
    private static final int VIEW_TYPE_DARK = 0; 
    private static final int VIEW_TYPE_LIGHT = 1; 

    public recycleradapter(Context context, ArrayList<String> mydata) { 
     this.context = context; 
     this.mydata = mydata; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     int viewid; 
     switch (viewType) { 
      case VIEW_TYPE_DARK: 
       viewid = R.layout.recycler_row_dark; 
       break; 
      case VIEW_TYPE_LIGHT: 
       viewid = R.layout.recycler_row_light; 
       break; 
      default: 
       throw new IllegalArgumentException("Invaild Value type : " + viewType); 
     } 
     View view = LayoutInflater.from(context).inflate(viewid, parent, false); 

     if(viewid == VIEW_TYPE_DARK){ 
      return new ViewHolder0(view); 
     }else{ 
      return new ViewHolder1(view); 
     } 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     int typeid = getItemViewType(position); 
     String xx = mydata.get(position); 
     String[] x = xx.split(","); 

      switch (holder.getItemViewType()) 
      { 
       case VIEW_TYPE_DARK:{ 
        ViewHolder0 viewholder = (ViewHolder0) holder; 
        viewholder.txt6.setText(x[1]); 
        viewholder.txt7.setText(x[2]); 
        break; 
       } 
       case VIEW_TYPE_LIGHT:{ 
        ViewHolder1 viewholder = (ViewHolder1) holder; 
        viewholder.txt8.setText(x[1]); 
        viewholder.txt9.setText(x[2]); 
        break; 
       } 
      } 
    } 


    @Override 
    public int getItemViewType(int position) { 
     if (position % 2 == 0) 
      return VIEW_TYPE_DARK; 
     return VIEW_TYPE_LIGHT; 
    } 


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


    // for the recycler_row_light layout.. 
    class ViewHolder0 extends RecyclerView.ViewHolder 
    { 
     public TextView txt6; 
     public TextView txt7; 
     public ViewHolder0(View itemView) { 
      super(itemView); 
      txt6 = (TextView) itemView.findViewById(R.id.textView6); 
      txt7 = (TextView) itemView.findViewById(R.id.textView7); 
     } 
    } 

    // for the recycler_row_dark layout... 
    class ViewHolder1 extends RecyclerView.ViewHolder{ 
     public TextView txt9; 
     public TextView txt8; 
      public ViewHolder1(View itemView){ 
       super(itemView); 
       txt8 = (TextView) itemView.findViewById(R.id.textView8); 
       txt9 = (TextView) itemView.findViewById(R.id.textView9); 
      } 
    } 
} 

ici l'activité de spectacle où je régler la recyclerview

package com.example.abdelmagied.myapplication; 

import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 

import java.util.ArrayList; 

public class show extends AppCompatActivity{ 

    public RecyclerView mrecyclerview; 
    public RecyclerView.LayoutManager mymanager; 
    public RecyclerView.Adapter myadapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show); 


     // fetch all data from the database and put it in an arraylist 
     mydatabase database = new mydatabase(this); 
     Cursor alldatabase = database.getData(); 
     String data = ""; 
     ArrayList<String> mydata = new ArrayList<String>(); 
     while(alldatabase.moveToNext()){ 
      data += alldatabase.getString(1); 
      data += ","; 
      data += alldatabase.getString(2); 
      data += ","; 
      data += alldatabase.getString(3); 
      data += ","; 
      data += alldatabase.getString(4); 
      data += ","; 
      data += alldatabase.getString(5); 
      mydata.add(data); 
      data = ""; 

     } 
     mrecyclerview = (RecyclerView) findViewById(R.id.RecyclerId); 
     mymanager  = new LinearLayoutManager(this); 
     myadapter  = new recycleradapter(this , mydata); 
     mrecyclerview.setLayoutManager(mymanager); 
     mrecyclerview.setAdapter(myadapter); 

    } 

} 

est ici le spectacle d'activité qui contiennent le recyclerview

<?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:id="@+id/activity_show" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.abdelmagied.myapplication.show"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:id="@+id/RecyclerId" /> 

</RelativeLayout> 

est ici la première mise en page qui a appelé recycler_row_dark

<?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" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@android:color/holo_blue_bright" 
     android:id="@+id/textView8"> 

     <ImageView 
      android:layout_width="90dp" 
      android:layout_height="90dp" 
      app:srcCompat="@android:drawable/stat_notify_missed_call" 
      android:id="@+id/imageView" /> 

     <TextView 
      android:text="NAme" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView8" 
      android:textSize="35sp" /> 

     <TextView 
      android:text=": Number" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView9" 
      android:layout_weight="1" 
      android:textSize="25sp" /> 

    </LinearLayout> 

</LinearLayout> 

VOICI les s mise en page de euxième qui a appelé recycler_row_light

<?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" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
     android:background="@android:color/darker_gray"> 

     <ImageView 
      android:layout_width="90dp" 
      android:layout_height="90dp" 
      app:srcCompat="@android:drawable/stat_notify_missed_call" 
      android:id="@+id/imageView" /> 

     <TextView 
      android:text="NAme" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView6" 
      android:textSize="35sp" /> 

     <TextView 
      android:text=": Number" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView7" 
      android:layout_weight="1" 
      android:textSize="25sp" /> 

    </LinearLayout> 

</LinearLayout> 

est ici une photo de mon logcat enter image description here

et cette photo en essayant de faire le débogage enter image description here

+2

Avez-vous quelque chose dans logcat? – litelite

+0

@litelite oui il y a une erreur j'ai mis à jour la question avec shotscreen –

+1

La trace de la pile que vous avez posté est incomplète. Nous avons besoin du message d'erreur complet. – litelite

Répondre

1

Changer votre code comme cette

Cursor alldatabase = database.getData(); 
String data = ""; 
ArrayList<String> mydata = new ArrayList<String>(); 

if (alldatabase.moveToFirst()) { 
    while(!alldatabase.isAfterLast()) { 
    // If you use alldatabase.moveToNext() here, you will bypass the first row, which is WRONG 
    ... 
    alldatabase.moveToNext(); 
    } 
} 

Le problème est que vous ne vérifiez pas si le curseur renvoie une valeur nulle ou non. donc s'il vous plaît vérifier d'abord, puis faire une opération sur cela.

Et cette entrée de journal n'est également pas utile car elle n'affiche aucun journal lié au problème d'application.

Mise à jour

problème plus on est dans votre mise en page sombre que vous avez donné même identifiant à disposition linéaire et textview à la fois de manière apporter cette correction aussi.