Actually, Android adapters is not that simple. SimpleAdapter
can be applied only in some very simple/trivial cases. Usually, it's preferred to create own BaseAdapter
: it gives much more flexibility and doesn't require lot of code.
In your case I would suggest to use smth like the following (You failed to provide any information about that is from
and to
in your SimpleAdapter
constructor, so I guessed):
Main part is proper adapter, which should store selected radio button id and provide it back to the activity/receiver.
MyAdapter.java
public class MyActivity extends Activity implements MyListAdapter.RadioButtonsListener {
private static final String TAG = "MyActivity";
ArrayList<HashMap<String, String>> mAlunosLista = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listaChamada = (ListView) findViewById(R.id.listChamada);
// Fill list with data
Random sexGen = new Random();
for (int i = 0; i < 20; i++) {
addTextData(sexGen.nextBoolean() ? "M" : "F", Integer.toString(i + 100), "Name" + i + " Surname" + i);
}
final MyListAdapter adapter = new MyListAdapter(mAlunosLista);
listaChamada.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(this);
}
@Override
protected void onPause() {
super.onPause();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(null);
}
/**
* Adds test data to adapter array
*/
private void addTextData(final String resultDataSexo, final String resultDataCodigoAluno, final String resultDataNomes) {
HashMap<String, String> hm = new HashMap<String, String>();
// NOTE: not sure why you convert ids to strings
if (resultDataSexo.equals("M")) {
hm.put("sexo", Integer.toString(R.drawable.boy));
} else {
hm.put("sexo", Integer.toString(R.drawable.girl));
}
hm.put("matricula", resultDataCodigoAluno);
hm.put("nome", resultDataNomes);
mAlunosLista.add(hm);
}
@Override
public void onRadioButtonClicked(final int position, final HashMap<String, String> itemData, final int clickedId) {
Log.d(TAG, "Clicked item at " + position + ", checked id: " + (clickedId == R.id.radiobuttonPresente ? "Presente" : "Ausente"));
}
}
Below other files I've changed to have compilable example (please refer help topic on how to ask questions about code questions body).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidLintUselessParent, AndroidLintContentDescription -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff3f2f2"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#00ffffff"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Title"
android:textColor="#ff000000"
android:textSize="36sp" >
</TextView>
</RelativeLayout>
<!-- Footer aligned to top
<include layout="@layout/footer" />
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/footer"
android:layout_alignParentBottom="true" />
<RelativeLayout
android:id="@+id/selectEscola"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_above="@id/footer"
android:layout_below="@id/header"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="4dp"
android:background="#00ffffff"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Chamada"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#c0000000" />
<ListView
android:id="@+id/listChamada"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@android:drawable/list_selector_background"
android:padding="2dp" />
</RelativeLayout>
</RelativeLayout>
list_row.xml
<?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="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="2dip"
android:background="@android:color/darker_gray" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:orientation="horizontal" >
<ImageView
android:id="@+id/listEdit"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginTop="2dip"
android:src="@android:drawable/ic_menu_add" />
<ImageView
android:id="@+id/list_image"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginLeft="20dip"
android:layout_marginTop="2dip"
android:src="@android:drawable/ic_delete" />
</LinearLayout>
<TextView
android:id="@+id/nomealuno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/matriculaaluno"
android:layout_alignTop="@+id/thumbnail"
android:text="Nome:"
android:textColor="#040404"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/matriculaaluno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nomealuno"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumbnail"
android:text="Matrícula: "
android:textColor="#343434"
android:textSize="10sp" />
<RadioGroup
android:id="@+id/radiogroupChamada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/thumbnail"
android:orientation="vertical"
android:textColor="#343434"
android:textSize="12sp"
android:textStyle="bold" >
<RadioButton
android:id="@+id/radiobuttonPresente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="22dip"
android:text="Presente" />
<!--android:drawableRight="@drawable/btn_check"-->
<RadioButton
android:id="@+id/radiobuttonAusente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="22dip"
android:text="Ausente" />
<!--android:drawableRight="@drawable/btn_check"-->
</RadioGroup>
</RelativeLayout>
MyActivity.java
public class MyActivity extends Activity implements MyListAdapter.RadioButtonsListener {
private static final String TAG = "MyActivity";
ArrayList<HashMap<String, String>> mAlunosLista = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listaChamada = (ListView) findViewById(R.id.listChamada);
// Fill list with data
Random sexGen = new Random();
for (int i = 0; i < 20; i++) {
addTextData(sexGen.nextBoolean() ? "M" : "F", Integer.toString(i + 100), "Name" + i + " Surname" + i);
}
final MyListAdapter adapter = new MyListAdapter(mAlunosLista);
listaChamada.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(this);
}
@Override
protected void onPause() {
super.onPause();
final MyListAdapter adapter = (MyListAdapter) ((ListView) findViewById(R.id.listChamada)).getAdapter();
adapter.setRadioListener(null);
}
/**
* Adds test data to adapter array
*/
private void addTextData(final String resultDataSexo, final String resultDataCodigoAluno, final String resultDataNomes) {
HashMap<String, String> hm = new HashMap<String, String>();
// NOTE: not sure why you convert ids to strings
if (resultDataSexo.equals("M")) {
hm.put("sexo", Integer.toString(R.drawable.boy));
} else {
hm.put("sexo", Integer.toString(R.drawable.girl));
}
hm.put("matricula", resultDataCodigoAluno);
hm.put("nome", resultDataNomes);
mAlunosLista.add(hm);
}
@Override
public void onRadioButtonClicked(final int position, final HashMap<String, String> itemData, final int clickedId) {
Log.d(TAG, "Clicked item at " + position + ", checked id: " + (clickedId == R.id.radiobuttonPresente ? "Presente" : "Ausente"));
} }
PS Also, it looks like You're trying to make some hack to ListView
. I have no idea why do You need it here, but it's better to avoid until completely necessary.