在自定义列表视图中单击一个时,将自动选择多个单选按钮

时间:2018-08-14 19:31:33

标签: android android-studio listview radio-button radio-group

我正在构建测验应用程序,并且正在从Firebase数据库中获取数据。在自定义列表视图中,我使用了单选组和单选按钮。

当我在自定义列表视图中单击一个时,将自动选择多个单选按钮。例如,如果我在列表1中选择第一个单选按钮,则会自动选择列表5中的相同单选按钮。

这是我的customListView

$("table").find(".table-times .date-span").eq(i).prepend(elem.html());
// ----------------------------------------^^^^^

这是我的customListView适配器类

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

<TextView
    android:id="@+id/ques"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="13dp"
    android:text="Question"
    android:textAppearance="@style/TextAppearance.AppCompat"
    android:textColor="@android:color/black"
    android:textSize="17sp" />

<RadioGroup
    android:id="@+id/choose"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp">

    <RadioButton
        android:id="@+id/opt_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:buttonTint="@android:color/black"
        android:text="Option A"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@android:color/black"
        android:textSize="17sp" />

    <RadioButton
        android:id="@+id/opt_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:buttonTint="@android:color/black"
        android:text="Option B"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@android:color/black"
        android:textSize="17sp"/>

    <RadioButton
        android:id="@+id/opt_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Option C"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@android:color/black"
        android:buttonTint="@android:color/black"
        android:textSize="17sp"/>

    <RadioButton
        android:id="@+id/opt_d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Option D"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@android:color/black"
        android:buttonTint="@android:color/black"
        android:textSize="17sp"/>
</RadioGroup>

<TextView
    android:id="@+id/ans"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/ques"
    android:layout_below="@+id/choose"
    android:text="Answer"
    android:visibility="gone" />
</RelativeLayout>

此类将数据发送到customListView Adapter类

 package sk.droidgen.studyzone;

 import android.content.Context;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.RadioButton;
 import android.widget.RadioGroup;
 import android.widget.TextView;
 import android.widget.Toast;

 import java.util.ArrayList;

 public class TestQuesList extends ArrayAdapter<Test> {

 private customButtonListener buttonListener;
 public interface customButtonListener{
    void onRadioButtonClicked(String selected,String answer);
 }
 void setButtonListener(customButtonListener listener){
    this.buttonListener=listener;
 }

 TestQuesList(Context context, ArrayList<Test> list){
    super(context, R.layout.quiz_listview,list);
 }
 @NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    final ViewHolder holder;
    final Test test;
    if (convertView==null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.quiz_listview, parent, false);
        holder=new ViewHolder();
        holder.radioGroup=convertView.findViewById(R.id.choose);
        holder.ques = convertView.findViewById(R.id.ques);
        holder.ans = convertView.findViewById(R.id.ans);
        holder.op1 = convertView.findViewById(R.id.opt_a);
        holder.op2 = convertView.findViewById(R.id.opt_b);
        holder.op3 = convertView.findViewById(R.id.opt_c);
        holder.op4 = convertView.findViewById(R.id.opt_d);
        convertView.setTag(holder);
    }
    else {
        holder=(ViewHolder) convertView.getTag();
    }
    test = getItem(position);
    holder.ques.setText(test.getQuestion());
    holder.ans.setText(test.getAns());
    holder.op1.setText(test.getOpt_a());
    holder.op2.setText(test.getOpt_b());
    holder.op3.setText(test.getOpt_c());
    holder.op4.setText(test.getOpt_d());
    holder.finalConvertView = convertView;
    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            holder.op1.setEnabled(false);
            holder.op2.setEnabled(false);
            holder.op3.setEnabled(false);
            holder.op4.setEnabled(false);
            RadioButton selectedRadioButton = holder.finalConvertView.findViewById(i);
           buttonListener.onRadioButtonClicked(selectedRadioButton.getText().toString(),holder.ans.getText().toString());
        }
    });
    return convertView;
}
public class ViewHolder {
    TextView ques,ans;
    RadioButton op1,op2,op3,op4;
    RadioGroup radioGroup;
    View finalConvertView;
}
}

这是listview xml文件

package sk.droidgen.studyzone;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

public class TakeTest extends AppCompatActivity implements      TestQuesList.customButtonListener {

private FirebaseDatabase database;
private NetworkStatus networkStatus;
private ArrayList<Test> list;
private ListView listView;
private DatabaseReference myRef;
private TestQuesList quesList;
private int mark=0;

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

    database = FirebaseDatabase.getInstance();
    list = new ArrayList<>();
    listView = findViewById(R.id.quiz_view);

    networkStatus = new NetworkStatus(getApplicationContext());
    if (!networkStatus.isConneted) {
        Toast.makeText(getApplicationContext(), "No Internet Connection\nPlease Turn On Internet", Toast.LENGTH_SHORT).show();
    } else {
        checkDb();
    }
}

private void checkDb() {
    myRef = database.getReference("test");
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String ques=dataSnapshot.child("/"+"question").getValue(String.class);
            String ans=dataSnapshot.child("/"+"ans").getValue(String.class);
            String op1=dataSnapshot.child("/"+"opt_a").getValue(String.class);
            String op2=dataSnapshot.child("/"+"opt_b").getValue(String.class);
            String op3=dataSnapshot.child("/"+"opt_c").getValue(String.class);
            String op4=dataSnapshot.child("/"+"opt_d").getValue(String.class);
            Test test = new Test(ques,op1,op2,op3,op4,ans);
            list.add(test);
            quesList=new TestQuesList(getApplicationContext(),list);
            listView.setAdapter(quesList);
            quesList.setButtonListener(TakeTest.this);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

public void onSubmitClicked(View view) {
    Toast.makeText(getApplicationContext(), mark, Toast.LENGTH_SHORT).show();
}

@Override
public void onRadioButtonClicked(String selected, String answer) {
    if (selected.equals(answer)){
     mark=mark+1;;
    }
}
}

0 个答案:

没有答案