2017-05-26 8 views
0

我正在創建一個對話框警報,它將顯示一個廣播組,取決於所選擇的選項,它將用一個數組或另一個數組的內容填充列表。這些數組在主要活動中填充,因此它們不爲空。我的問題是嘗試填充對話框中的列表,數組變成空的,我不知道如何在那裏傳遞填充值。從活動傳遞到對話框時,我的數組爲空null

這些是導致問題的行:

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; 


    } 
} 
+0

我檢查了所有類似的問題,沒有一個似乎適合我的情況,因爲。 –

+0

您確定'all_times_array'和'all_runtimes_array'例如不是空的嗎?你可以在創建適配器之前打印它們的長度嗎? – Denny

+0

你已經在你的活動中初始化了這些數組 –

回答

0

只是一個建議! 創建您的單選按鈕組的佈局文件,之後請參閱您的radioi組佈局

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

此佈局設置爲您的警報對話

dialog.setContentView(R.layout.yourlayout); 

並獲得

選定項的值
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 } 
      } 
    }); 

希望它有幫助!

+0

很好的建議,但這是一個問答網站。請用相反的方式回答問題,然後您可以將其中的建議包括在內。最好的做法是將您的建議添加爲評論,以便其他人不會跳過此問題,因爲它已經有了答案。如果你想幫助代碼評論,那麼你可以在這裏頭:https://codereview.stackexchange.com/ – sorifiend

+0

我認爲這將有助於它,因爲我在我的一個項目中面臨同樣的問題。並感謝您的回覆,現在我必須刪除答案並將其添加爲評論? –

+0

這通常是做這件事的最好方式,但既然你的答案已經在這裏,可能會留下這個,但下次要留心留言。 – sorifiend