【发布时间】:2014-04-01 18:39:40
【问题描述】:
我有一个带有自定义列表行的 list-view。在每个 list-view 的 行中,有一个 Radio-group 和 2 个 Radio-buttons。
我需要知道 RadioButtons 的状态,对于 ListView 的每一行(选中与否),当用户单击其中一个strong>RadioGroup 的 选项。
我的主要xml:
<?xml version="1.0" encoding="utf-8"?>
<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="@string/titulo_cabecalho"
android:textColor="#ff000000"
android:textSize="36sp" >
</TextView>
</RelativeLayout>
<!-- Footer aligned to top -->
<include layout="@layout/footer" />
<RelativeLayout
android:id="@+id/selectEscola"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginBottom="80dp"
android:scrollbars="none"
android:background="@drawable/border" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/listChamada"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:padding="2dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
我的自定义行是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:padding="2dip"
android:background="@drawable/border2" >
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="34dip"
android:layout_alignParentLeft="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="@drawable/list" />
<ImageView
android:id="@+id/list_image"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginLeft="20dip"
android:layout_marginTop="2dip"
android:src="@drawable/boy" />
</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" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="4dp"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radiogroupChamada"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
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:button="@null"
android:drawableRight="@drawable/btn_check"
android:paddingLeft="22dip"
android:text="Presente" >
</RadioButton>
<RadioButton
android:id="@+id/radiobuttonAusente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableRight="@drawable/btn_check"
android:paddingLeft="22dip"
android:text="Ausente" >
</RadioButton>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
这就是我填充 ListView 的方式:
1 - 将从服务器接收到的数据以 Json 格式放入数组和 hashmap 中的代码(我使用数组来引用 ListView 和 hashmap 中存在的数据来填充它):
if (success2 == 1) // If data from server was sent without errors
{
int lenght = jArray.length();
optionsMatriculas = new ArrayList<String>(lenght);
optionsNomesAlunos = new ArrayList<String>(lenght);
optionsSexoAlunos = new ArrayList<String>(lenght);
for (int i = 0; i < jArray.length(); i++)
{
try
{
JSONObject oneObject = jArray.getJSONObject(i);
resultDataCodigoAluno = oneObject.getString("cdaluno");
resultDataNomes = oneObject.getString("nome");
resultDataSexo = oneObject.getString("sexo");
optionsMatriculas.add(resultDataCodigoAluno);
optionsNomesAlunos.add(resultDataNomes);
optionsSexoAlunos.add(resultDataSexo);
HashMap<String, String> hm = new HashMap<String, String>();
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);
alunosLista.add(hm);
}
catch (JSONException e)
{
Log.e("log_tag", "Error in parsing JSon: " + e.toString());
}
}
Message msg = myHandler.obtainMessage();
myHandler.sendMessage(msg);
}
这是填充列表视图的代码:
final Handler myHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
try
{
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), alunosLista, R.layout.list_row, from, to);
listaChamada.setAdapter(adapter);
int lenght = adapter.getCount();
int totalHeight = 0;
for (int i = 0; i < lenght; i++)
{
View listItem = adapter.getView(i, null, listaChamada);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listaChamada.getLayoutParams();
params.height = totalHeight + 4 + (listaChamada.getDividerHeight() * (adapter.getCount() - 1));
listaChamada.setLayoutParams(params);
listaChamada.requestLayout();
}
catch (Exception e)
{
Log.e("log_tag", "Erro no ListView: " + e.toString());
}
}//handleMessage
};//myHandler
我在 Google 中进行了搜索,但仍然没有运气。 感谢您提供有关此的任何线索。
【问题讨论】:
-
请为每一行添加您的listview相关创建和绑定代码,以及自定义适配器代码
标签: android listview radio-button radio-group