2016-05-16 59 views
1

我正在开发Android考勤应用程序,教师可以参加考勤。 Here is the image。我以here为例。但我没有得到,当单击SUBMIT按钮时,如何获得单选按钮的值。如何从listview中获取广播组的值?

Attendance.class

public class Attendance extends Activity { 

    private ListView listView1; 
    Button submit; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.attendance); 
     submit = (Button) findViewById(R.id.testbutton); 

     Option weather_data[] = new Option[] 
       { 
         new Option("Student1"), 
         new Option("Student2"), 
         new Option("Student3"), 
         new Option("Student4"), 
         new Option("Student5"), 
         new Option("Student6") 

       }; 
     RadioGroupAdapter adapter = new RadioGroupAdapter(this, 
       R.layout.listitem, weather_data); 
     listView1 = (ListView)findViewById(R.id.list); 
     listView1.setAdapter(adapter); 
    } 



} 

RadioGroupAdapter.class

public class RadioGroupAdapter extends ArrayAdapter<Option> { 

    Context context; 
    int layoutResourceId; 
    Option data[] = null; 

    public RadioGroupAdapter(Context context, int layoutResourceId, 
          Option[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     MatrixHolder holder = null; 

     if (row == null) { 
      LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new MatrixHolder(); 
      holder.txtTitle = (TextView) row.findViewById(R.id.heading); 
      holder.group = (RadioGroup) row.findViewById(R.id.radio_group1); 
      final RadioButton[] rb = new RadioButton[2]; 
      for(int i=0; i<2; i++){ 
       rb[i] = new RadioButton(context); 
       //rb[i].setButtonDrawable(R.drawable.single_radio_chice); 
       rb[i].setId(i); 
       RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
         0, LayoutParams.WRAP_CONTENT); 
       params.weight=1.0f; 
       params.setMargins(5, 0, 5, 10); 
       holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout 
      } 
      row.setTag(holder); 
     } else { 
      holder = (MatrixHolder) row.getTag(); 
     } 

     Option option = data[position]; 
     holder.txtTitle.setText(option.title); 
     return row; 
    } 

    static class MatrixHolder { 
     TextView txtTitle; 
     RadioGroup group; 
     int position; 
    } 
} 

option.class

public class Option { 
    public String title; 
    int selectedId=-1; 
    public Option(){ 
     super(); 
    } 

    public Option(String title) { 
     super(); 
     this.title = title; 

    } 

} 

logcat的

05-17 13:17:13.254 5141-5141/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: org.pitechnologies.teacher, PID: 5141 
               java.lang.IllegalStateException: Could not execute method of the activity 
                at android.view.View$1.onClick(View.java:4020) 
                at android.view.View.performClick(View.java:4780) 
                at android.view.View$PerformClick.run(View.java:19866) 
                at android.os.Handler.handleCallback(Handler.java:739) 
                at android.os.Handler.dispatchMessage(Handler.java:95) 
                at android.os.Looper.loop(Looper.java:135) 
                at android.app.ActivityThread.main(ActivityThread.java:5257) 
                at java.lang.reflect.Method.invoke(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:372) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                Caused by: java.lang.reflect.InvocationTargetException 
                at java.lang.reflect.Method.invoke(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:372) 
                at android.view.View$1.onClick(View.java:4015) 
                at android.view.View.performClick(View.java:4780)  
                at android.view.View$PerformClick.run(View.java:19866)  
                at android.os.Handler.handleCallback(Handler.java:739)  
                at android.os.Handler.dispatchMessage(Handler.java:95)  
                at android.os.Looper.loop(Looper.java:135)  
                at android.app.ActivityThread.main(ActivityThread.java:5257)  
                at java.lang.reflect.Method.invoke(Native Method)  
                at java.lang.reflect.Method.invoke(Method.java:372)  
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  
                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.pitechnologies.teacher.Option[] org.pitechnologies.teacher.RadioGroupAdapter.getItems()' on a null object reference 
                at org.pitechnologies.teacher.Attendance.onClick(Attendance.java:40) 
                at java.lang.reflect.Method.invoke(Native Method)  
                at java.lang.reflect.Method.invoke(Method.java:372)  
                at android.view.View$1.onClick(View.java:4015)  
                at android.view.View.performClick(View.java:4780)  
                at android.view.View$PerformClick.run(View.java:19866)  
                at android.os.Handler.handleCallback(Handler.java:739)  
                at android.os.Handler.dispatchMessage(Handler.java:95)  
                at android.os.Looper.loop(Looper.java:135)  
                at android.app.ActivityThread.main(ActivityThread.java:5257)  
                at java.lang.reflect.Method.invoke(Native Method)  
                at java.lang.reflect.Method.invoke(Method.java:372)  
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  
+0

你可以在适配器xml中的'RadioGroup'中添加RadioButton' ..为什么编程? – Bharatesh

+0

是你的问题解决? – Bharatesh

+0

没有我仍然在努力,而不是编程方式,我正在通过xml ... – user3810679

回答

0

由于ListView会重新使用这些视图,因此您需要维护每个视图相对于该视图(行)的位置。

RadioGroupAdapter.java

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    MatrixHolder holder = null; 

    if (row == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder = new MatrixHolder(); 
     holder.txtTitle = (TextView) row.findViewById(R.id.heading); 

     holder.group = (RadioGroup) row.findViewById(R.id.radio_group); 

     holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, int i) { 
       int pos = (int) radioGroup.getTag(); 
       data[pos].selectedId = radioGroup.getCheckedRadioButtonId(); 
      } 
     }); 
     row.setTag(holder); 
    } else { 
     holder = (MatrixHolder) row.getTag(); 
    } 

    Option option = data[position]; 
    holder.txtTitle.setText(option.title); 
    holder.group.setTag(position); 
    if (option.selectedId != -1) {//check at-least one is selected 
     holder.group.check(option.selectedId); 
    } 
    return row; 
} 

//get all items attached to adapter 
public Option[] getItems() { 
    return data; 
} 

Attendance.java

public void onClick(View view) { 
    if (view.getId() == R.id.submit) { 
     Option[] items = adapter.getItems(); 
     for (Option opt : items) { 
      if (opt.selectedId != -1) { 
       Log.i("TAG", "Title " + opt.title + " Selected " + (opt.selectedId == R.id.radio_a ? "A" : "P")); 
      } else { 
       Log.i("TAG", "Title " + opt.title + " Not Selected Any Button"); 
      } 
     } 
    } 
} 

Option.java

public class Option { 
    String title; 
    int selectedId=-1; 
    //your constructor 
} 

happyCoding;

+0

非常感谢,但什么是适配器(选项[] [ items = adapters.getItems())在attendace.java – user3810679

+0

这是'RadioGroupAdapter'实例,使'adapter'全局变量。 – Bharatesh

+0

我知道了,但应用程序崩溃 – user3810679

0

试图在自定义Arrayadapter radio[i].setOnCheckedChangeListener(...)和内部侦听器中存储value(boolean)在一些变量中执行,并包括getter函数来检索它。

0

你需要实现这样

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);   
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     // checkedId is the RadioButton selected 
    } 
}); 

然后保存在你喜欢的数据结构中的检查按钮的状态,单选按钮的监听器。 之后,当您按下提交按钮时,您可以读取您的数据结构并发送您选中/未选中的数据。

我希望这有助于!