简体   繁体   中英

My array is null when passed from the activity to a dialog

I'm creating a dialog alert that will show a radio group, depending on the chosen option it will populate a list with the contents of one array or another.These arrays are populated on the main activity, so they are not null. My problem is try to populate the list in the dialog, the arrays turn out to be empty, and I don't know how to pass the populated value there.

These are the lines that cause problems:

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

And this is the code for my dialog:

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

These are the methods my pupulateListView class:

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;


    }
}

Just a suggestion! Create a layout file with your radio group,and set this layout to your alert dialogue

 dialog.setContentView(R.layout.yourlayout);

after that refer to your radioi group in layout

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

and get the value of selected item in

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

Hope it helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM