从活动传递到对话框时,我的数组为null

时间:2017-05-26 07:58:44

标签: java android dialog parameter-passing

我正在创建一个对话框警报,显示一个无线电组,根据所选的选项,它将使用一个或另一个数组的内容填充列表。这些数组填充在主要活动上,因此它们是不是空的。我的问题是尝试填充对话框中的列表,数组结果为空,我不知道如何在那里传递填充值。

这些是导致问题的行:

adapter = new populateListView(MainActivity.this, all_times_array, all_runtimes_array);

这是我的对话框的代码:

public void dialog_filter() {

  final String[] grpname = {
   "Today",
   "This Month",
   "This Year",
   "All time"
  };

  AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);

  //alt_bld.setIcon(R.drawable.icon);
  alt_bld.setTitle("See reports from ...");
  alt_bld.setSingleChoiceItems(grpname, -1, new DialogInterface
   .OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
     time_filter = item;
     System.out.println(time_filter);
     Toast.makeText(getApplicationContext(),
      grpname[item] + " selected", Toast.LENGTH_SHORT).show();


     switch (time_filter) {


      case 3:

       adapter = new populateListView(MainActivity.this, all_times_array, all_runtimes_array);
       bannertext = "Total seizures:" + " " + total_seizures;
       banner.setText(bannertext);
       list.setAdapter(adapter);

       break;

      case 0:
       adapter = new populateListView(MainActivity.this, today_times_array, today_runtimes_array);
       bannertext = "Today seizures:" + " " + today_seizures;
       banner.setText(bannertext);
       list.setAdapter(adapter);
       break;

      case 1:
       adapter = new populateListView(MainActivity.this, month_times_array, month_runtimes_array);
       bannertext = "Month seizures:" + " " + month_seizures;
       banner.setText(bannertext);
       list.setAdapter(adapter);
       break;
      case 2:
       adapter = new populateListView(MainActivity.this, year_times_array, year_runtimes_array);
       bannertext = "Year seizures:" + " " + year_seizures;
       banner.setText(bannertext);
       list.setAdapter(adapter);
       break;


     }
     dialog.dismiss();

    }
   });
  AlertDialog alert = alt_bld.create();
  alert.show();

这些是我的pupulateListView类的方法:

class populateListView extends ArrayAdapter <String>

{

    Context context;
    String [] times;
    String [] runtimes;



    populateListView(Context c,String [] tms, String [] rts)
    {
        super(c, seizure_list2,R.id.firstLine,tms);

        this.context=c;
        this.runtimes=rts;
        this.times = tms;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(seizure_list2,parent,false);
        TextView runtime_text = (TextView) row.findViewById(R.id.secondLine);
        TextView time_text = (TextView) row.findViewById(R.id.firstLine);

        time_text.setText(times[position]);
        runtime_text.setText(runtimes[position]);
        return row;


    }
}

1 个答案:

答案 0 :(得分:0)

只是一个建议! 使用您的广播组创建布局文件,并将此布局设置为警报对话

 dialog.setContentView(R.layout.yourlayout);
之后

在布局中引用你的radioi组

RadioGroup youradiogroup = (RadioGroup) dialog.findViewById(R.id.youradiogroupID);

并获取

中所选项目的值
youradiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup group,
                                     int checkedId) {
            if (checkedId == R.id.first_radiobutton) {
                //do something}
            else  if (checkedId == R.id.second_radiobutton) {
                //do something else }
            }
    });

希望它有所帮助!