Verwenden Sie eine API zum Ausfüllen eines Fragebogens App in Android. Ich benutze erweiterbare Listenansicht, basierend auf der API bekomme ich Fragekategorie (Eltern) und Fragen (Kind). Frage (untergeordnet) kann sich je nach Fragentyp ändern (Zieltyp: Radiobutton, ja oder nein, Texttyp: Text bearbeiten).Android Expandable Listview mit Optionsfeld
Ich habe statisch ein benutzerdefiniertes Layout XML erstellt und dieses Layout in getChildView-Methode oder erweiterbare Listview aufblasen.
aber wenn ich die erweiterbare Listenansicht erweitern/reduzieren, gehen alle Optionsfeldauswahl falsch. zB: wenn ich für ein Kind der 1. Kategorie antworte, wird es auch für eine andere Kategorie beantwortet.
Bitte holen Sie mir eine Lösung dafür. Muss ich die Optionsschaltfläche/Radio-Gruppe dynamisch erstellen oder das ist genug. Basierend auf dem FrageTyp verstecke ich die Widgets. Den Code finden Sie unten.
Mein HashMap Wert ist wie:
HashMap<String, ArrayList<QListModel>> expandableListDetail = new HashMap<>();
Arraylist enthält: Frage, Fragetyp, QuestionID etc
Hier ist mein Code: My Adapter Class
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, ArrayList<QListModel>> expandableListDetail;
public ExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String,
ArrayList<QListModel>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public int getChildrenCount(int listPosition) {
// return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
// .size();
Log.e("VCVCV",String.valueOf(this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size()));
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expand_parent, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.groupText);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
QListModel expandedListText = (QListModel) getChild(listPosition, expandedListPosition);
Log.e("ChildQuestions",expandedListText.getqList());
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expand_child_selection, null);
}
TextView expandedListTextView = (TextView) convertView.findViewById(R.id.childText);
EditText expandedListEditText = (EditText) convertView.findViewById(R.id.editTextAdditional);
expandedListTextView.setText(expandedListText.getqList());
RadioGroup rgp = (RadioGroup) convertView.findViewById(R.id.radioGroup);
//NotUsing
RadioButton radioYes=(RadioButton) convertView.findViewById(R.id.radioYes);
RadioButton radioNo=(RadioButton) convertView.findViewById(R.id.radioNo);
if (expandedListText.getqType().equals("Y/N")){
rgp.setVisibility(View.VISIBLE);
expandedListEditText.setVisibility(View.GONE);
}
else {
expandedListEditText.setVisibility(View.VISIBLE);
rgp.setVisibility(View.GONE);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
mein Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/childText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="@color/colorBlackLight"
android:text=""/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorBlackLight"
android:text="Yes"/>
<RadioButton
android:id="@+id/radioNo"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorBlackLight"
android:text="No"/>
</RadioGroup>
<EditText
android:id="@+id/editTextAdditional"
android:layout_width="match_parent"
android:padding="5dp"
android:textColor="@color/colorBlackLight"
android:layout_marginBottom="10dp"
android:layout_marginRight="20dp"
android:background="@color/colorFrameBLueLight"
android:textSize="12sp"
android:layout_height="wrap_content" />
</LinearLayout>
Problem: Wenn ich eine Auswahl in der ersten Kategorie in allen Kategorie-Element ausgewählt ist, muss ich die Radio-Button und Radio-Gruppe dynamisch verwalten? Wenn ja, in welcher Methode muss ich das tun. Wenn ich sie in getChildView erstelle, wird es jedes Mal aufgerufen, wenn ich eine Kategorie erweitere. Die vorherige Auswahl wird nicht gespeichert. Bitte holen Sie mir eine Lösung. Vielen Dank im Voraus.
Verwenden viewholder oder recyclerview , dies geschieht, weil die Ansicht wiederverwendet wird, also das gleiche Objekt abgerufen wird, verwenden Sie ein Sparseintarray, um die Benutzerauswahl zu verfolgen –