我正在創建一個對話框警報,它將顯示一個廣播組,取決於所選擇的選項,它將用一個數組或另一個數組的內容填充列表。這些數組在主要活動中填充,因此它們不爲空。我的問題是嘗試填充對話框中的列表,數組變成空的,我不知道如何在那裏傳遞填充值。從活動傳遞到對話框時,我的數組爲空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;
}
}
我檢查了所有類似的問題,沒有一個似乎適合我的情況,因爲。 –
您確定'all_times_array'和'all_runtimes_array'例如不是空的嗎?你可以在創建適配器之前打印它們的長度嗎? – Denny
你已經在你的活動中初始化了這些數組 –