根据所选的对话框选项填充自定义列表

时间:2017-05-25 14:16:48

标签: java android listview

我在填充自定义列表时遇到问题。当应用程序处于创建状态时,它会使用默认元素正确填充列表。但是,当我进入设置对话框时,我有一个无线电组,允许我选择将使用哪个数组来填充列表,但是我不能从对话框中调用populatelistview自定义类,因为没有'允许我使用"这个"作为输入,因为它在对话框中它并不接受它作为上下文。这是我的populatelistview类:

hPut

这是导致对话中出现问题的一行:

 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;


    }
}

最后,这是我与广播组的对话:

`public void dialog_filter(){

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

`

2 个答案:

答案 0 :(得分:0)

ArrayAdapter要求第一个输入是Context的实例。由于Dialog未从Context延伸,因此您无法使用this实例化适配器。但是,您可以从对话框中调用getContext(),然后传递它。

答案 1 :(得分:0)

更改用于在onclick方法中创建适配器的代码行,以便像这样

adapter = adapter = new populateListView(YourActivityName.this, all_times_array, all_runtimes_array);
相关问题