2016-10-07 2 views
1

私は初心者であり、この問題を解決するためにあなたの助けが必要です。RecyclerViewのビューの複製に直面しています。ビューが5回再生されるたびに、ビューが自動的に繰り返されます。 androiddevも同様に役立つことはありません。 基本的に私は学生の意見を取り込むためにフィードバックフォームを作成しています。これが私の道に入り、解決できません。 問題の要点を得るためにスクリーンショットを添付しています。 以下のスクリーンショットの比較コードをご覧ください。に関連するRecyclerViews Android

public class GenericViewholder extends CarViewholder { 

TextView ques; 
TextView tv1, tv2, tv3, tv4, tv5; 
LinearLayout counter; 
CardView cardview1; 
// Firebase mref; 
// Button bottomb; 
int count = 0; 
String database = new String(); 

public GenericViewholder(View itemView) { 
    super(itemView); 

    // cardview1 = (CardView) itemView.findViewById(R.id.card_view1) ; 
    counter = (LinearLayout) itemView.findViewById(R.id.tv_counter); 
    ques = (TextView) itemView.findViewById(R.id.setQuestion); 
    tv1 = (TextView) itemView.findViewById(R.id.one); 
    tv2 = (TextView) itemView.findViewById(R.id.two); 
    tv3 = (TextView) itemView.findViewById(R.id.three); 
    tv4 = (TextView) itemView.findViewById(R.id.four); 
    tv5 = (TextView) itemView.findViewById(R.id.five); 
    // bottomb = (Button) itemView.findViewById(R.id.button); 

    View.OnClickListener clicklistener = new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      switch (view.getId()) { 
       case R.id.one: 
        if (tv4.getBackground() != null || tv2.getBackground() != null || tv3.getBackground() != null 
          || tv5.getBackground() != null) { 
         tv4.setBackgroundResource(R.color.standardwhite); 
         tv2.setBackgroundResource(R.color.standardwhite); 
         tv3.setBackgroundResource(R.color.standardwhite); 
         tv5.setBackgroundResource(R.color.standardwhite); 
        } 


        tv1.setBackgroundResource(R.drawable.fill); 
        count = 1; 
        database = "1"; 
        break; 
       case R.id.two: 
        if (tv4.getBackground() != null || tv1.getBackground() != null || tv3.getBackground() != null 
          || tv5.getBackground() != null) { 
         tv4.setBackgroundResource(R.color.standardwhite); 
         tv1.setBackgroundResource(R.color.standardwhite); 
         tv3.setBackgroundResource(R.color.standardwhite); 
         tv5.setBackgroundResource(R.color.standardwhite); 
        } 

        tv2.setBackgroundResource(R.drawable.fill); 
        count = 1; 
        database ="2"; 
        break; 
       case R.id.three: 
        if (tv1.getBackground() != null || tv2.getBackground() != null || tv4.getBackground() != null 
          || tv5.getBackground() != null) { 
         tv1.setBackgroundResource(R.color.standardwhite); 
         tv2.setBackgroundResource(R.color.standardwhite); 
         tv4.setBackgroundResource(R.color.standardwhite); 
         tv5.setBackgroundResource(R.color.standardwhite); 
        } 

        tv3.setBackgroundResource(R.drawable.fill); 
        count= 1; 
        database = "3"; 
        break; 
       case R.id.four: 
        if (tv1.getBackground() != null || tv2.getBackground() != null || tv3.getBackground() != null 
          || tv5.getBackground() != null) { 
         tv1.setBackgroundResource(R.color.standardwhite); 
         tv2.setBackgroundResource(R.color.standardwhite); 
         tv3.setBackgroundResource(R.color.standardwhite); 
         tv5.setBackgroundResource(R.color.standardwhite); 
        } 

        tv4.setBackgroundResource(R.drawable.fill); 
        count =1; 
        database = "4"; 

        break; 
       case R.id.five: 
        if (tv4.getBackground() != null || tv2.getBackground() != null || tv3.getBackground() != null 
          || tv1.getBackground() != null) { 
         tv1.setBackgroundResource(R.color.standardwhite); 
         tv2.setBackgroundResource(R.color.standardwhite); 
         tv3.setBackgroundResource(R.color.standardwhite); 
         tv4.setBackgroundResource(R.color.standardwhite); 
        } 

        tv5.setBackgroundResource(R.drawable.fill); 
        count = 1; 
        database ="5" ; 
        // b5.setBackgroundColor(Color.BLUE); 
        break; 
       default: 
        count = 0; 
        database ="0" ; 

      } 
      //Toast.makeText(view.getContext(), "Rating set : " + value.getRating(), Toast.LENGTH_SHORT).show(); 

     } 
    }; 

    if(tv1.getBackground() != null ||tv2.getBackground() != null ||tv3.getBackground() != null ||tv4.getBackground() != null ||tv5.getBackground() != null) 
    { 
     count = 1; 
    } 
    else 
     count = 0; 

    tv1.setOnClickListener(clicklistener); 
    tv2.setOnClickListener(clicklistener); 
    tv3.setOnClickListener(clicklistener); 
    tv4.setOnClickListener(clicklistener); 
    tv5.setOnClickListener(clicklistener); 


} 

}

public class CardAdapter extends RecyclerView.Adapter<CarViewholder> { 
/* 
Important thing to notice here is that the multiple viewholders are needed to be checked and aligned 
with respect to the position and viewtype of the layout 
Viewtype: returns default value 0 for similar layout so we overrided it with two values 
one for the cardview and one for the Master submit section 
Pl note :::: Always create different child classes and extend it to the master class to get better result and 
better judge of the execution taking place. 
OncreateViewholder creates and inflates the view so here we need to distinctly create different objects of 
child classes to get different results 

*/ 

List<String> dbObjList = new ArrayList<>(); 
public static int CARD_VAL = 1; 
public static int BUTTON_VAL = 2; 
int countercount = 0; 
String[] ques = {"How would you grade the Teachers", 
     "How Did You Find The Study Material", 
     "How Was The Coaching Infrastructure", 
     "How Innovative Were The Teachers", 
     "Ease Of Travel To The Class", 
     "Previous Years' Results", 
     "Extra-Curricular Activities", 
     "Counselling During Admission", 
     "Clear Your Doubts Sessions", 
     "How Were The Tests Conducted", 
     "How Were The Seminars", 
     "How Were The Powerpoint Presentations", 
     "Quality of tests in regular time intervals", 
     "Syllabus coverage in tests", 
     "Test results/feedback on time", 
     "Doubt clearance after tests", 
     "Remedial classes after tests"}; 
Context context; 
LayoutInflater inflater; 
// View iview; 

public CardAdapter(Context context) { 

    this.context = context; 
    inflater = LayoutInflater.from(context); 
} 

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

    CarViewholder viewholder ; 
    View iview; 
    if (viewType == CARD_VAL) { 
      iview = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false); 
      viewholder = new GenericViewholder(iview); 
      iview.setTag(viewholder); 
     return viewholder; 
    } else { 
     iview = LayoutInflater.from(parent.getContext()).inflate(R.layout.button, parent, false); 
     viewholder = new ButtonViewHolder(iview); 
     return viewholder; 

    } 
} 

@Override 
public void onBindViewHolder(CarViewholder holder, int position) { 
    if (getItemViewType(position) == BUTTON_VAL || position == getItemCount() + 1) { 
     Log.i("DEBUG", "hun" + getItemViewType(position)); 

     ButtonViewHolder buttonViewHolder = (ButtonViewHolder) holder; 
     Log.i("DEBUG", "" + getItemViewType(position)); 

     Log.i("DEBUG", "hun" + getItemViewType(position)); 

     buttonViewHolder.bottombutton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       int countt = 0; 
       Log.i("Checking the database", dbObjList.toString()); 
       for (int i = 0; i< dbObjList.size();i++) 
       { 
        if(dbObjList.get(i).equals("") || dbObjList.get(i).equals("0")) 
         countt++; 
       } 
       Log.i("Checking the database", countt + ""); 
       if (countt == 0) { 
        new AlertDialog.Builder(view.getContext()) 
          .setTitle("Title") 
          .setMessage("Do you really want to submit?") 
          .setIcon(android.R.drawable.ic_dialog_alert) 
          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 

           public void onClick(DialogInterface dialog, int whichButton) { 
            Toast.makeText(context, "Submitted your response", Toast.LENGTH_SHORT).show(); 
           } 
          }) 
          .setNegativeButton(android.R.string.no, null).show() 
       } else { 
        Toast.makeText(view.getContext(), "Still left to submit", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 

    if (getItemViewType(position) == CARD_VAL && position <= getItemCount()) { 
     Log.i("DEBUG", "" + getItemViewType(position)); 
     GenericViewholder genericViewholder = (GenericViewholder) holder; 
     dbObjList.add(position,genericViewholder.database); 
     if(genericViewholder != null) 
     if(genericViewholder.count == 1){ 
      countercount++; 
      Log.i("THE value of count :",countercount+""); 
     } 
     genericViewholder.ques.setText(ques[position]); 
    } 

} 

@Override 
public int getItemCount() { 
    return ques.length + 1; 
} 

@Override 
public long getItemId(int position) { 
    return super.getItemId(position); 
} 

@Override 
public int getItemViewType(int position) { 
    //check for the termination point 
    //for the last point insert button 
    return (position < ques.length) ? CARD_VAL : BUTTON_VAL; 


} 

}

Find the screenshots here

答えて

0

onBindViewHolderビューが をバインドするたびに実行されるためとして onBindViewHolder外のごbuttonviewホルダーを宣言してみてください。これは

+0

こんにちはラビ、私は非常にあなたの助けに感謝しますが、ボタンビューの所有者が私によって使用される前にもビューの繰り返しが起こっていた。 eボタンビューホルダー、私は同じ変更に直面しています。私はその理由を再構築した後、それが繰り返されているので、私はそれらを削除またはリセットする方法を知っていないので再利用したので、 – DrManhattan

0

を変更しようと繰り返すようにあなたの意見を引き起こしている可能性がありますclassCardAdapterクラスに

public classCardAdapter extends RecyclerView.Adapter<classCardAdapter.RecyclerViewHolder> 

を拡張し、方法変更:

@Override 
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
//all stuff 

} 

をすべてのビューを保持するRecyclerViewHolderのクラスを作成します。データ

+0

こんにちは@ singh.indolia、試してみましたが、まだ動作していません。 – DrManhattan

関連する問題