I have an ExpandableListView that has groups and itens, each iten has a checkbox that can be selected and I want to know how can I iterate over all itens and get their values when I click the button.
So far I have created the list, I already can select the checkboxes and everything is going well, but I cant find a solution to get the values from the checkboxes when I click the button. This is like a questionary, when the user finishes picking his choices, he will submit the values.
I have a MainActivity, an ExpandableListAdapter that is my list adapter implementation and I have 3 xml, activity_main.xml, list_group.xml and list_itens.xml.
Here is my code so far:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listaDePerguntas;
private HashMap<String, List<String>> listaDeRespostas;
private CheckBox check;
public ExpandableListAdapter(Context context, List<String> listaDePerguntas, HashMap<String, List<String>> listaDeRespostas) {
this.context = context;
this.listaDePerguntas = listaDePerguntas;
this.listaDeRespostas = listaDeRespostas;
}
@Override
public int getGroupCount() {
return listaDePerguntas.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return listaDeRespostas.get(listaDePerguntas.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return listaDePerguntas.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return listaDeRespostas.get(listaDePerguntas.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String resposta = (String) getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_itens, null);
}
TextView text = (TextView) convertView.findViewById(R.id.listItem);
text.setText(resposta);
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String resposta = (String) getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_itens, null);
}
TextView text = (TextView) convertView.findViewById(R.id.listItem);
text.setText(resposta);
CheckBox check = (CheckBox) convertView.findViewById(R.id.listCheckItem);
//check.setFocusable(false);
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(buttonView.getContext(), "Selected! - Question number: " + groupPosition + "answer: " + childPosition + "ischecked: " + isChecked, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
My MainActivity class:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> perguntas;
private HashMap<String, List<String>> respostas;
private Button enviarRespostas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ExpandableListView) findViewById(R.id.expandableListView);
carregarDados();
listAdapter = new ExpandableListAdapter(this, perguntas, respostas);
listView.setAdapter(listAdapter);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicou no:" + groupPosition + " item: " + childPosition, Toast.LENGTH_SHORT).show();
return false;
}
});
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Toast.makeText(MainActivity.this, "Clicou no:" + groupPosition, Toast.LENGTH_SHORT).show();
return false;
}
});
enviarRespostas = (Button) findViewById(R.id.buttonEnviarRespostas);
enviarRespostas.setOnClickListener(this);
}
private void carregarDados() {
perguntas = new ArrayList<>();
respostas = new HashMap<>();
perguntas.add("Pertunta 1");
perguntas.add("Pertunta 2");
perguntas.add("Pertunta 3");
perguntas.add("Pertunta 4");
List<String> respostasPergunta1 = new ArrayList<>();
respostasPergunta1.add("Sim");
respostasPergunta1.add("Não");
respostasPergunta1.add("Talvez");
List<String> respostasPergunta2 = new ArrayList<>();
respostasPergunta2.add("10");
respostasPergunta2.add("20");
respostasPergunta2.add("30");
respostasPergunta2.add("40");
List<String> respostasPergunta3 = new ArrayList<>();
respostasPergunta3.add("Sim");
respostasPergunta3.add("Não");
List<String> respostasPergunta4 = new ArrayList<>();
respostasPergunta4.add("10");
respostasPergunta4.add("20");
respostasPergunta4.add("30");
respostasPergunta4.add("40");
respostasPergunta4.add("50");
respostasPergunta4.add("60");
respostas.put(perguntas.get(0), respostasPergunta1);
respostas.put(perguntas.get(1), respostasPergunta2);
respostas.put(perguntas.get(2), respostasPergunta3);
respostas.put(perguntas.get(3), respostasPergunta4);
}
@Override
public void onClick(View v) {
//todo
}
}
And here are my 3 .xml files:
1) activity_main.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:weightSum="1">
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="437dp" />
<Button
android:id="@+id/buttonEnviarRespostas"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="Button" />
</LinearLayout>
2) list_group.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="@android:color/white">
<TextView
android:id="@+id/header"
android:paddingLeft="?
android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="16dp"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="/>
</LinearLayout>
3) list_itens.xml
<TableLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0">
<TableRow>
<TextView
android:id="@+id/listItem"
android:textSize="16dp"
android:paddingLeft="?
android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/listCheckItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp" />
</TableRow>
</TableLayout>
Aucun commentaire:
Enregistrer un commentaire