弹出带有动态列表视图的窗口

时间:2014-03-28 08:07:51

标签: android popupwindow

我有一个像这样创建的xml文件的数组列表(xmlList):

private static ArrayList<File> xmlList = new ArrayList<File>();
public static ArrayList<File> XMLContact(File directory, File contactDirectory,
ArrayList<Contact> myContactList) {
if (!(directory.exists())) {
directory.mkdirs();}
if (!(contactDirectory.exists())) {
contactDirectory.mkdirs();
}
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh-mm-ss");
String FileName = df.format(c.getTime());
File newxmlfile = new File(Environment.getExternalStorageDirectory()+ "/newfile/contactfile/"+FileName+"xml");  
xmlList.add(newxmlfile);

然后想在弹出窗口中显示此列表的元素(点击按钮后:按钮联系人)。所以我写了这段代码

private void onClickButtonContact(View view) {
Button myButton = (Button) view.findViewById(R.id.buttonContact);
myButton.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
     xmlList = CreateContactXML.getXmlList();
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     for (int i =1 ; i< xmlList.size(); i++)  
     {Log.e (  null, xmlList.get(i).getAbsolutePath());
     final String path ;
     path  = xmlList.get(i).getName();
     builder.setTitle("Backup Date"); 
     builder.setItems(i, new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int item) {
     Toast.makeText(getActivity(), "Restore done for ", Toast.LENGTH_SHORT).show();                                
     }
     });
     AlertDialog alert = builder.create();
     alert.show();
}
}
}
});
}

列表已创建,我可以登录其元素。但问题是弹出窗口只包含标题。

1 个答案:

答案 0 :(得分:1)

在警告中显示列表:

  ArrayList<String> arrfile_path=new ArrayList<String>(); 
    for (int i =1 ; i< xmlList.size(); i++)
    arrfile_path.add(xmlList.get(i).getAbsolutePath());
    builder.setTitle("Backup Date");
    builder.setItems(arrfile_path, new DialogInterface.OnClickListener() {

      // your code here
    });

因为目前您只将索引(i)传递给builder.setItems