2014-02-28 48 views
0

我有一个列表视图与自定义列表行。在每个列表视图的行,有一个无线电组与2 无线电按钮如何获取ListView中RadioGroup中一对RadioButton的状态?

我需要知道单选按钮的状态,为的ListView(选中与否),动态的每一行,作为对本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格式中,在数组和散列表中(我使用数组来引用ListView中的数据并使用散列表来填充它):

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); 
} 

这是为了填充ListView代码:

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 

我已搜查,在谷歌,但仍没有运气。 谢谢你的任何线索。

+0

请为每行添加与listview相关的创建和绑定代码,以及自定义适配器代码 – Jack

回答

0

其实,Android适配器并不那么简单。 SimpleAdapter只能应用于一些非常简单/微不足道的情况。通常,最好创建自己的BaseAdapter:它提供了更多的灵活性并且不需要很多代码。 在你的情况我建议使用不便如下内容(你没有提供任何信息,关于在你SimpleAdapter构造fromto,所以我猜):

主要部分是正确的适配器,它应该存储选择单选按钮ID并将其提供回活动/接收器。

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")); 
    } 
} 

下面的其他文件我已经改变了有编译例子(请参阅如何询问有关代码的问题身体问题help topic)。

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此外,它看起来就像你正在试图做一些黑客以ListView。我不知道你为什么需要这里,但最好避免,直到完全有必要。

相关问题