Java 根据所选对话框选项填充自定义列表
填充自定义列表时遇到问题。当应用程序处于“创建”状态时,它会使用默认元素正确填充列表。但是,当我转到“设置”对话框时,我有一个无线电组,允许我选择将使用哪个数组填充列表,但我不能从对话框中调用populatelistview自定义类,因为它不允许我使用“this”作为输入,因为它在对话框中,所以它不接受它作为上下文。这是我的populatelistview类:Java 根据所选对话框选项填充自定义列表,java,android,listview,Java,Android,Listview,填充自定义列表时遇到问题。当应用程序处于“创建”状态时,它会使用默认元素正确填充列表。但是,当我转到“设置”对话框时,我有一个无线电组,允许我选择将使用哪个数组填充列表,但我不能从对话框中调用populatelistview自定义类,因为它不允许我使用“this”作为输入,因为它在对话框中,所以它不接受它作为上下文。这是我的populatelistview类: class populateListView extends ArrayAdapter <String> { Co
class populateListView extends ArrayAdapter <String>
{
Context context;
String [] times;
String [] runtimes;
populateListView(Context c,String [] tms, String [] rts)
{
super(c, R.layout.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(R.layout.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;
}
}
最后,这是我与广播组的对话:
`公共无效对话框_过滤器(){
`ArrayAdapter要求第一个输入是
上下文的实例。由于对话框
不是从上下文
扩展而来,因此不能使用此
实例化适配器。但是,您可以调用getContext()
,并将其传递给您。将用于在onclick方法中创建适配器的代码行更改为如下所示
adapter = adapter = new populateListView(YourActivityName.this, all_times_array, all_runtimes_array);
此方法出现nullpointer异常。在OnCreate期间,我使用此方法时完全没有任何问题。当我调用此方法时:populateListView(YourActivityName.this,all_times_数组,all_runtimes_数组);您是否已将活动名称替换为“YourActivityName”在代码中?是的,我已经意识到问题来自数组,因为它们是空的。在主活动中它们不是空的。我如何才能将值传递到对话框中?在开关情况下为特定选择将数组分配给适配器我找不到此方法,只有getAppliCationContext()或getBaseContext()
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(this, all_times_array, all_runtimes_array);
bannertext = "Total seizures:" + " " + total_seizures;
banner.setText(bannertext);
list.setAdapter(adapter);
break;
case 0:
// adapter = new populateListView(this, today_times_array, today_runtimes_array);
bannertext = "Today seizures:" + " " + today_seizures;
banner.setText(bannertext);
list.setAdapter(adapter);
break;
case 1:
// adapter = new populateListView(this, month_times_array, month_runtimes_array);
bannertext = "Month seizures:" + " " + month_seizures;
banner.setText(bannertext);
list.setAdapter(adapter);
break;
case 2:
// adapter = new populateListView(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();
}
adapter = adapter = new populateListView(YourActivityName.this, all_times_array, all_runtimes_array);