2016-04-01 10 views
0

Schauen Sie sich meinen Code bekommen:versuche, ich Recycler Ansicht mit mehreren Ansichten zu implementieren, aber ich bin nicht das gewünschte Ergebnis

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.w3c.dom.ls.LSException; 

import java.util.ArrayList; 
import java.util.Arrays; 

/** 
* Created by Ankit on 3/25/2016. 
*/ 
public class CustomAdapterRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 
    Context context; 
    private ArrayList<String> questionsList; 
    LayoutInflater layoutInflater; 

    public CustomAdapterRecyclerView(Context context, ArrayList<String> questionsList) { 
     this.context = context; 
     this.questionsList = questionsList; 
     layoutInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     switch (viewType) { 
      case 1: 
       final View view = layoutInflater.inflate(R.layout.recycler_view_list, parent, false); 
       MyViewHolder myViewHolder = new MyViewHolder(view, context); 
       return myViewHolder; 
     default: 
      final View view1 = layoutInflater.inflate(R.layout.single_item_in_list, parent, false); 
      MyExpandedViewHolder myExpandedViewHolder= new MyExpandedViewHolder(view1, context,new ArrayList<String>(Arrays.asList("A","Aa"))); 
      return myExpandedViewHolder; 
     } 
} 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     switch (holder.getItemViewType()) 
     { 
      case 1: 
       MyViewHolder vh = (MyViewHolder) holder; 
       vh.questionTitle.setText(questionsList.get(position)); 
       break; 
      default: 
       MyExpandedViewHolder viewHolder= (MyExpandedViewHolder) holder; 
       viewHolder.getListView(); 
     } 
    } 
/* 
    @Override 
    public void onBindViewHolder(RecyclerView.V holder, int position) { 
     holder.questionTitle.setText(questionsList.get(position)); 
    }*/ 

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

    @Override 
    public int getItemViewType(int position) { 
    if(position ==0|| position==1|| position==2) 
    { 
     return 0; 
    } 
     else 
     return 1; 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     TextView questionTitle; 
     private Context context; 

     public MyViewHolder(final View itemView, Context context) { 
      super(itemView); 
      this.context = context; 
      itemView.setOnClickListener(this); 
      questionTitle = (TextView) itemView.findViewById(R.id.question); 
      questionTitle.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(context, "" + getAdapterPosition() + questionTitle.getText().toString(), Toast.LENGTH_LONG).show(); 
      Log.d("TAG", "" + getAdapterPosition()); 
      Intent intent = new Intent(context, ProgramViewer.class); 
      intent.putExtra("titlePos",getAdapterPosition()); 
      intent.putExtra("title",questionTitle.getText().toString()); 
      context.startActivity(intent); 
     } 
    } 
    public class MyExpandedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 
    { 
     private Context context; 
     ListView listView; 
     public MyExpandedViewHolder(View itemView, Context context,ArrayList<String> mobileArray) { 
      super(itemView); 
      this.context = context; 
      ArrayAdapter adapter = new ArrayAdapter<String>(context, R.layout.expanded_list_inside_recycler_view, mobileArray); 
      listView= (ListView) itemView.findViewById(R.id.list_view); 
      listView.setAdapter(adapter); 
     } 
     public ListView getListView() 
     { 
      return this.listView; 
     } 
     @Override 
     public void onClick(View v) { 
     Toast.makeText(context,getAdapterPosition()+"",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

Was ich versuchte, die RecyclerView Artikel auf Klick zu tun ist, zu erweitern, aber zuerst ich müssen Recycleransicht und Listenansicht zusammen implementieren. Ich bekomme NullPointerException. Bitte sagen Sie mir das Problem in meinem Code. Die Antwort würde sehr geschätzt werden.

Log-Datei:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
at kole.technicalbird.CustomAdapterRecyclerView$MyExpandedViewHolder.<init>(CustomAdapterRecyclerView.java:113) 
at kole.technicalbird.CustomAdapterRecyclerView.onCreateViewHolder(CustomAdapterRecyclerView.java:43) 
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5476) 
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4701) 
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(

Layout-Datei: single_item_in_list.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:text="a"/> 
    </LinearLayout> 


**recycler_view_list** 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <android.support.v7.widget.CardView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="true" 
      android:elevation="8dp"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:id="@+id/question" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        android:fontFamily="sans-serif" 
        android:text="ques" 
        android:padding="8dp" 
        android:layout_marginRight="32dp" 
        android:textSize="16dp" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_marginRight="8dp" 
        android:fontFamily="sans-serif" 
        android:layout_centerVertical="true" 
        android:padding="8dp" 
        android:text=">" /> 
      </RelativeLayout> 

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

    </LinearLayout> 
    <ImageView 
     android:src="@android:drawable/divider_horizontal_dark" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:paddingBottom="0.2dp" 
     android:paddingTop="0.2dp" /> 
</LinearLayout> 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/list_view"> 
</ListView> 
</LinearLayout> 

expanded_list_inside_recycler_view

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/list_view"> 
</ListView> 
</LinearLayout> 
+0

können Sie das Protokoll und die Initialisierung Ihrer Recycler-Ansicht – TUSHAR

+0

in Frage gestellt. –

+0

@AnkitArora können Sie auch ein Layout erstellen? –

Antwort

0

Wo Ihre Listenansicht in Ihrem Einzel ist _item_in_list.xml. Ihre single_item_in_list.xml hat keine Listenansicht. Wenn Sie also getListView() verwenden, ist die Listenansicht null. Ich schätze, das ist der Grund!

+0

überprüfen ArrayAdapter, sein Code auch @jevely geändert –

Verwandte Themen