-1

我有一个列表,每行有一个文本视图和 2 个单选按钮。我正在使用自定义数组适配器来填充列表视图。我已经实现了列表上的项目单击侦听器,但它没有返回任何内容。事实上它根本不执行点击方法。这是代码:

适配器类:

public class MyAdapter extends ArrayAdapter<String> {
 Context context; 
 public static ArrayList<String> values=new ArrayList<String>();
 public static ArrayList<String> attendance=new ArrayList<String>();
 RadioGroup radioGroup;
  RadioButton present,absent;
public MyAdapter(Context context,ArrayList values) {
    super(context,org.example.attendance.R.layout.list_radio,values);
    // TODO Auto-generated constructor stub


    this.context=context;
    this.values=values;
}

public  View getView(int pos,View convertview,ViewGroup parent)
    {
        LayoutInflater inflator=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView=inflator.inflate(org.example.attendance.R.layout.list_radio,parent,false);
        radioGroup=(RadioGroup)rowView.findViewById(R.id.radioGroup1);
        present=(RadioButton)rowView.findViewById(org.example.attendance.R.id.radio0);
        absent=(RadioButton)rowView.findViewById(org.example.attendance.R.id.radio1);
        TextView textview=(TextView)rowView.findViewById(org.example.attendance.R.id.textView1);
    //  textview.setText(values[pos]);
        //String s=values[pos];

            textview.setText(values.get(pos));
        //int selectedId = radioGroup.getCheckedRadioButtonId();
        //if(selectedId==R.id.radio0)
        return rowView;


    }
 public boolean areAllItemsEnabled() 
    {
        return true;
    }

    @Override
    public boolean isEnabled(int arg0) 
    {
        return true;
    }

 }

主类:

public class Students_List extends Activity {

Intent i1;
String tablename;
RadioGroup radioGroup;
RadioButton present,absent;
ArrayList<String> student_name = new ArrayList<String>() ;
public static ArrayList<String> attendance=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_students__list);
    final ListView listview = (ListView) findViewById(R.id.listview);
    i1=getIntent();
    radioGroup=(RadioGroup)findViewById(R.id.radioGroup1);
    present=(RadioButton)findViewById(org.example.attendance.R.id.radio0);
    absent=(RadioButton)findViewById(org.example.attendance.R.id.radio1);
    tablename=i1.getStringExtra("class")+"_student";
    //Log.d("student","table"+tablename);
    String temp=getServerData(KEY_12);
    listview.setAdapter(new MyAdapter(this,student_name));
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int     position,
                long arg3) {
            // TODO Auto-generated method stub
            Log.d("view","clicked");
            if(present.isChecked())
            {
            attendance.add("present");
            }
            if(absent.isChecked())
            {
                attendance.add("absent");
            }

}

    });
}

list_radio.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="match_parent" >

  <TextView
     android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="24dp" />

  <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginRight="35dp"
    android:orientation="horizontal" >
     <RadioButton
    android:id="@+id/radio0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:paddingRight="45dp"
    android:checked="true" />

  <RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

   </RadioGroup>


  </RelativeLayout>

最后是列表视图 xml 文件:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" 
   android:descendantFocusability="blocksDescendants">

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="@string/present" 
        android:paddingRight="25dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="@string/absent" />
</LinearLayout>

   <ListView
      android:id="@+id/listview"
      android:layout_width="match_parent"
      android:layout_height="0dip" 
       android:layout_weight="1"
        android:cacheColorHint="#f8f8ff"
        android:divider="#000000"
        android:fadeScrollbars="true"
        android:choiceMode="singleChoice"
        android:fastScrollEnabled="true"
        android:footerDividersEnabled="true"
        android:textFilterEnabled="true"
        android:textStyle="normal" />


<Button
    android:id="@+id/Button01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/submit" 
    android:onClick="submit"/>

</LinearLayout>

我应该怎么做才能获得单选按钮的状态?

4

2 回答 2

0

如果您的列表项,请将此元素添加到布局中:

android:descendantFocusability="blocksDescendants"

也许将其从您的主布局中删除?

于 2013-09-29T08:54:23.243 回答
0

在这里获取此代码并将更改应用于您的需求。你需要自己工作一些。

此代码是一个微调列表。有关更多代码,您可以访问 此处

@Override
public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
    // On selecting a spinner item
    String item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

}

public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
    // TODO Auto-generated method stub

}
于 2013-09-29T09:05:42.023 回答