【问题标题】:Not able to get a result on clicking the items of a list that include radio buttons单击包含单选按钮的列表项目时无法获得结果
【发布时间】:2013-10-05 05:55:59
【问题描述】:

我有一个列表,其中每行有一个文本视图和 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>

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

【问题讨论】:

    标签: android android-listview radio-button


    【解决方案1】:

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

    android:descendantFocusability="blocksDescendants"
    

    也许将它从你的主布局中移除?

    【讨论】:

      【解决方案2】:

      在此处获取此代码并根据您的需求应用更改。你需要自己动手。

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

      @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
      
      }
      

      【讨论】:

      • 它给出了一个错误,即 void 不是 onItemSelected 方法的有效返回类型
      • 您是否从我提供的网址下载了整个代码?它对我有用。您能否将整个代码或项目发送给我以供我敏锐的参考?如果是,那么回复我会给你我的电子邮件 ID
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2012-09-09
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多