0

アンドロイドでラジオグループでcardviewを作成しました。学生出席システムを作っています。私はすでにバックエンドに参加していました。問題は現時点で、あるいは学生がいない場合は、各カードに4つのラジオボタンを備えたラジオグループを使用していました。しかし、いくつかのラジオボタンが既に確認されていると私は、スクロールしていたときに、その後のラジオボタンのチェックの付いている位置も変更される: - 。一つ一つのカードの コードがcheckboxsample.xmlラジオグループのカードビューで、ラジオボタンが既にチェックされています

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="center" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="wrap_content" 
      android:text="Check All" 
      android:layout_marginRight="7dip" 
      android:id="@+id/btnCheckAll" 
      android:layout_height="wrap_content"> 
     </Button> 
     <Button 
      android:layout_width="wrap_content" 
      android:text="Clear All" 
      android:id="@+id/btnClearAll" 
      android:layout_height="wrap_content"> 
     </Button> 
    </LinearLayout> 

    <ListView 
     android:layout_width="fill_parent" 
     android:id="@+id/lvCheckBox" 
     android:fadingEdge="none" 
     android:cacheColorHint="@null" 
     android:layout_height="fill_parent"> 
    </ListView> 
</LinearLayout> 
ため

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    card_view:cardCornerRadius="5dp" 

    card_view:cardUseCompatPadding="true" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" > 

     <TextView 
      android:id="@+id/tvName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:text="name" 
      android:textColor="@android:color/black" 
      android:textSize="18sp" /> 

     <TextView 
      android:id="@+id/tvEmailId" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/tvName" 
      android:text="email" 
      android:textColor="@android:color/black" 
      android:textSize="18sp" /> 
<RadioGroup android:id="@+id/rg" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 


     <RadioButton 
      android:id="@+id/chkSelected" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:text="A" 
      android:checked="false"> 


     </RadioButton> 

     <RadioButton 
      android:id="@+id/chkSelected2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="64dp" 
      android:layout_marginEnd="64dp" 
      android:layout_centerVertical="true" 
      android:layout_toLeftOf="@+id/chkSelected" 
      android:layout_toStartOf="@+id/chkSelected" 
      android:text="P" 
      android:checked="false"> 


     </RadioButton> 

</RadioGroup> 

    </RelativeLayout> 

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

とコードでありますstudent.javaためactivity_main.xml

<?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="match_parent" 
    android:orientation="vertical" > 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/my_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:scrollbars="vertical" /> 

    <Button 
     android:id="@+id/btnShow" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:text="Show Selected" 
     android:background="#00796B" 
     android:textColor="@color/TextPrimaryColor"/> 

</LinearLayout> 

コードの

コード

cardviewactivity.java

package com.example.administrator.cardviewcheckbok; 

import java.util.ArrayList; 
import java.util.List; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class CardViewActivity extends ActionBarActivity { 

    private Toolbar toolbar; 

    private RecyclerView mRecyclerView; 
    private RecyclerView.Adapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 

    private List<Student> studentList; 

    private Button btnSelection; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     btnSelection = (Button) findViewById(R.id.btnShow); 

     studentList = new ArrayList<Student>(); 

     for (int i = 1; i <= 15; i++) { 
      Student st = new Student("Student " + i, "androidstudent" + i 
        + "@gmail.com", false); 

      studentList.add(st); 
     } 

     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
      getSupportActionBar().setTitle("Android Students"); 

     } 

     mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 

     // use this setting to improve performance if you know that changes 
     // in content do not change the layout size of the RecyclerView 
     mRecyclerView.setHasFixedSize(true); 

     // use a linear layout manager 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 

     // create an Object for Adapter 
     mAdapter = new CardViewDataAdapter(studentList); 

     // set the adapter object to the Recyclerview 
     mRecyclerView.setAdapter(mAdapter); 

     btnSelection.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String data = ""; 
       List<Student> stList = ((CardViewDataAdapter) mAdapter) 
         .getStudentist(); 

       for (int i = 0; i < stList.size(); i++) { 
        Student singleStudent = stList.get(i); 
        if (singleStudent.isSelected() == true) { 

         data = data + "\n" + singleStudent.getName().toString(); 
         /* 
         * Toast.makeText(CardViewActivity.this, " " + 
         * singleStudent.getName() + " " + 
         * singleStudent.getEmailId() + " " + 
         * singleStudent.isSelected(), 
         * Toast.LENGTH_SHORT).show(); 
         */ 

       //// 
               } 

       } 

       Toast.makeText(CardViewActivity.this, 
         "Selected Students: \n" + data, Toast.LENGTH_LONG) 
         .show(); 
      } 
     }); 

    } 

} 

ためcardviewdataadapter.java

package com.example.administrator.cardviewcheckbok; 

import java.util.List; 

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.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.CompoundButton.OnCheckedChangeListener; 

public class CardViewDataAdapter extends 
     RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> { 

    private List<Student> stList; 

    public CardViewDataAdapter(List<Student> students) { 
     this.stList = students; 

    } 

    // Create new views 
    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, 
      int viewType) { 
     // create a new view 
     View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
       R.layout.cardview_row, null); 

     // create ViewHolder 

     ViewHolder viewHolder = new ViewHolder(itemLayoutView); 

     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder viewHolder, int position) { 

     final int pos = position; 

     viewHolder.tvName.setText(stList.get(position).getName()); 

     viewHolder.tvEmailId.setText(stList.get(position).getEmailId()); 

     viewHolder.chkSelected.setSelected(stList.get(position).isSelected()); 

     viewHolder.chkSelected.setTag(stList.get(position)); 
////new checkselected2 

     viewHolder.chkSelected2.setSelected(stList.get(position).isSelected()); 

     viewHolder.chkSelected2.setTag(stList.get(position)); 

viewHolder.checkrgg.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     RadioGroup rgg=(RadioGroup)v; 
int selbutid=rgg.getCheckedRadioButtonId(); 

    RadioButton rb= (RadioButton) rgg.getChildAt(selbutid); 
Toast.makeText(v.getContext(),""+rb.getText()+"",Toast.LENGTH_LONG).show(); 

    } 
}); 

     viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       RadioButton cb = (RadioButton) v; 

       Student contact = (Student) cb.getTag(); 

       contact.setSelected(cb.isSelected()); 
       stList.get(pos).setSelected(cb.isSelected()); 

    /*   Toast.makeText(
         v.getContext(), 
         "Clicked on Checkbox: " + "" + pos + cb + " is " 
           + cb.isSelected(), Toast.LENGTH_LONG).show(); 
    */ 
        } 

      }); 


     viewHolder.chkSelected2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       RadioButton cb = (RadioButton) v; 

       Student contact = (Student) cb.getTag(); 

       contact.setSelected(cb.isSelected()); 
       stList.get(pos).setSelected(cb.isSelected()); 
/* 
       Toast.makeText(
         v.getContext(), 
         "2222222Clicked on Checkbox: "+""+pos +cb + " is " 
           + cb.isSelected(), Toast.LENGTH_LONG).show(); 
       */ 
        } 
     }); 



    } 

    // Return the size arraylist 
    @Override 
    public int getItemCount() { 
     return stList.size(); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 

     private final RadioGroup checkrgg; 
     public TextView tvName; 
     public TextView tvEmailId; 

     public RadioButton chkSelected,chkSelected2; 

     public Student singlestudent; 

     public ViewHolder(View itemLayoutView) { 
      super(itemLayoutView); 

      tvName = (TextView) itemLayoutView.findViewById(R.id.tvName); 

      tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId); 
      chkSelected = (RadioButton) itemLayoutView 
        .findViewById(R.id.chkSelected); 

      ///here putted code 
      chkSelected2 = (RadioButton) itemLayoutView 
        .findViewById(R.id.chkSelected2); 
//// 
      checkrgg=(RadioGroup)itemLayoutView.findViewById(R.id.rg); 

     } 

    } 

    // method to access in activity after updating selection 
    public List<Student> getStudentist() { 
     return stList; 
    } 

} 

コードについて

package com.example.administrator.cardviewcheckbok; 

import java.io.Serializable; 

public class Student implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    private String name; 

    private String emailId; 

    private boolean isSelected; 

    public Student() { 

    } 

    public Student(String name, String emailId) { 

     this.name = name; 
     this.emailId = emailId; 

    } 

    public Student(String name, String emailId, boolean isSelected) { 

     this.name = name; 
     this.emailId = emailId; 
     this.isSelected = isSelected; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getEmailId() { 
     return emailId; 
    } 

    public void setEmailId(String emailId) { 
     this.emailId = emailId; 
    } 

    public boolean isSelected() { 
     return isSelected; 
    } 

    public void setSelected(boolean isSelected) { 
     this.isSelected = isSelected; 
    } 

} 

コードこれらは大きな問題問題ですしてください。実際にリストを使ってやると同じエラーが出るので、コミュニティからの助けを借りて、これらの問題についていくつかご提案ください。 「リストにあるラジオグループとカードビュー」

答えて

0

は、ここに私のサンプルコードです:

イベント「onBindViewHolder」、「RecyclerViewAdapter.java」をご覧ください。

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private static RecyclerViewAdapter adapter; 
    private LinearLayoutManager layoutManager; 
    private static RecyclerView recyclerView; 
    private List<Book> data = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     getNextItems(); 
     recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setHasFixedSize(false); 
     recyclerView.setLayoutManager(layoutManager); 
     adapter = new RecyclerViewAdapter(data, this, recyclerView); 
     recyclerView.setAdapter(adapter); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 

     adapter.setOnLoadMoreListener(new RecyclerViewAdapter.OnLoadMoreListener() { 
      @Override 
      public void onLoadMore() { 
       Log.i("LoadMore", "onLoadMore called"); 
       //add progress item 
       data.add(null); 
       adapter.notifyDataSetChanged(); 

       Log.i("LoadMore", "Loading new data... (" + 10 + ") posts"); 
       //remove progress item 
       data.remove(data.size() - 1); 

       getNextItems(); 

       adapter.notifyDataSetChanged(); 
       //add items one by one 

       adapter.setLoaded(); 
      } 
     }); 

    } 

    private void getNextItems() { 
     int itemCount = data.size(); 
     for (int i = itemCount; i <= itemCount + 10; i++) { 
      Book book = new Book(); 
      book.title = "Title " + i; 
      book.author = "Author " + i; 
      book.description = "Description " + i; 
      book.status = -1; 
      data.add(book); 
     } 
    } 

} 

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter { 
    private List<Book> dataSet; 
    private Context context; 

    private final int VIEW_ITEM = 1; 
    private final int VIEW_PROG = 0; 

    // The minimum amount of items to have below your current scroll position before loading more. 
    private int visibleThreshold = 1; 
    private int lastVisibleItem, totalItemCount; 
    private boolean loading; 
    private OnLoadMoreListener onLoadMoreListener; 
    private Map<Integer, Integer> status; 

    public RecyclerViewAdapter(List<Book> data, Context context, RecyclerView recyclerView) { 
     this.dataSet = data; 
     this.context = context; 
     status = new ArrayMap<>(); 
     final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); 
     recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
      @Override 
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
       super.onScrolled(recyclerView, dx, dy); 
       final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); 
       totalItemCount = linearLayoutManager.getItemCount(); 
       lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); 
       if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) { 
        loading = true; 
        // End has been reached 
        // Do something 
        Log.i("AdapterScrolled", "onScrolled: End reached"); 
        if (onLoadMoreListener != null) { 
         onLoadMoreListener.onLoadMore(); 
        } 

       } 
      } 
     }); 

    } 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     TextView description; 
     TextView title; 
     TextView author; 
     ImageView profilePic; 
     RadioGroup radioGroup; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      this.profilePic = (ImageView) itemView.findViewById(R.id.profile_image); 
      this.title = (TextView) itemView.findViewById(R.id.title); 
      this.author = (TextView) itemView.findViewById(R.id.author); 
      this.description = (TextView) itemView.findViewById(R.id.description); 
      radioGroup = (RadioGroup) itemView.findViewById(R.id.radio_group); 
     } 
    } 

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

    @Override 
    public int getItemViewType(int position) { 
     return dataSet.get(position) != null ? VIEW_ITEM : VIEW_PROG; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     RecyclerView.ViewHolder vh; 
     if (viewType == VIEW_ITEM) { 
      View v = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.book_layout, parent, false); 

      vh = new MyViewHolder(v); 
     } else { 
      View v = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.progress_bar_layout, parent, false); 

      vh = new ProgressViewHolder(v); 
     } 
     return vh; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { 
     if (holder instanceof MyViewHolder) { 
//    Binding Views... 

      Book book = dataSet.get(position); 
      ((MyViewHolder) holder).profilePic.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.empty_user)); 
      ((MyViewHolder) holder).title.setText(book.title); 
      ((MyViewHolder) holder).author.setText(book.author); 
      ((MyViewHolder) holder).description.setText(book.description); 

      final RadioGroup radioGroup = ((MyViewHolder) holder).radioGroup; 
      radioGroup.setTag(book); 
      radioGroup.clearCheck(); 
      final RadioButton radioButton1 = (RadioButton) radioGroup.findViewById(R.id.low); 
      final RadioButton radioButton2 = (RadioButton) radioGroup.findViewById(R.id.medium); 
      final RadioButton radioButton3 = (RadioButton) radioGroup.findViewById(R.id.high); 

      int value = book.status; 
      if (value == 0) { 
       radioButton1.setChecked(true); 
      } else if (value == 1) { 
       radioButton2.setChecked(true); 
      } else if (value == 2) { 
       radioButton3.setChecked(true); 
      } 

      radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) { 
        Book tagBook = (Book) radioGroup.getTag(); 
        if (radioButton1.isChecked()) { 
         tagBook.status = 0; 
        } else if (radioButton2.isChecked()) { 
         tagBook.status = 1; 
        } else if (radioButton3.isChecked()) { 
         tagBook.status = 2; 
        } 
       } 
      }); 

     } else { 
      ((ProgressViewHolder) holder).progressBar.setIndeterminate(true); 
     } 
    } 

    public void setLoaded() { 
     loading = false; 
    } 

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { 
     this.onLoadMoreListener = onLoadMoreListener; 
    } 

    public interface OnLoadMoreListener { 
     void onLoadMore(); 
    } 

    public static class ProgressViewHolder extends RecyclerView.ViewHolder { 
     public ProgressBar progressBar; 

     public ProgressViewHolder(View v) { 
      super(v); 
      progressBar = (ProgressBar) v.findViewById(R.id.progressBar); 
     } 
    } 
} 

Book.java

public class Book { 
    String title; 
    String author; 
    String description; 
    int status = -1; 
} 

book_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/book_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="@drawable/border" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <ImageView 
     android:id="@+id/profile_image" 
     android:layout_width="50dp" 
     android:layout_height="50dp" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/author" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" /> 

    <RadioGroup 
     android:id="@+id/radio_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <RadioButton 
      android:id="@+id/low" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Low" /> 

     <RadioButton 
      android:id="@+id/medium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Medium" /> 

     <RadioButton 
      android:id="@+id/high" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="High" /> 
    </RadioGroup> 

</LinearLayout> 
関連する問題