更新生活在AlertDialog中的listview项目

时间:2013-06-16 15:28:58

标签: android listview adapter edit alertdialog

我在扩展DialogFragment的类中构建一个AlertDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    Intent locationTypesIntent = new Intent(getActivity(), LocationTypesRetrieverService.class);
    getActivity().getApplicationContext().startService(locationTypesIntent);

    itemsOnList = new CharSequence[noOfItems];
    itemsChecked = new boolean[noOfItems];
    for(int i=0;i<noOfItems;i++)
    {
        itemsOnList[i] = "Location " + i;
        itemsChecked[i] = true;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Filter locations")
           .setMultiChoiceItems(itemsOnList, itemsChecked, 
                   new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        System.out.println("clicked choice");                           
                    }
                }
     );
    createdAlertDialog = builder.create();
    return createdAlertDialog;
}

然后我尝试检索AlertDialog中的列表,并更改适配器,以便更新显示的项目:

public void updateOptions(String[] optionsArray)
{
    ListView list = createdAlertDialog.getListView();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),list.getId(),optionsArray);
    list.setAdapter(adapter);
    //list.invalidate();
}

我收到错误:

E / AndroidRuntime(9401):android.content.res.Resources $ NotFoundException:资源ID#0x1020403类型#0x12无效

发生了什么事?

1 个答案:

答案 0 :(得分:0)

移动此行:

list.setAdapter(adapter);

在您片段的onViewCreated方法下:

public void onViewCreated(View view,Bundle savedInstanceState){
    //...

    list.setAdapter(mAdapter);
}