2014-03-27 28 views
0

我在这里创建的文件列表,我称之为的XMLList不能使用ArrayList在其他类

public class CreateContactXML { 
public ArrayList <Contact> myContactList= new ArrayList <Contact>(); 
static ArrayList<File> xmlList = new ArrayList<File>(); 
@SuppressLint("SimpleDateFormat") 
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); 
for (int i =1 ; i < xmlList.size(); i++) 
{Log.e(null , xmlList.get(i).getName());} 
return xmlList; 
} 

}

创建列表,我可以看到它的元素

27月2014 09-00-00.xml

27月2014 09-11-00.xml

然后我想用个是在其他类中的列表

public class RestoreFragment extends Fragment { 
ArrayList <File> xmlList = new ArrayList<File>(); 
static RestoreFragment fragment; 
public RestoreFragment(){} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) 
{ 
    View rootView = inflater.inflate(R.layout.fragment_restore, container, false); 
    onClickButtonContact(rootView); 
    return rootView ; 
} 
private void onClickButtonContact(View view) { 
    Button myButton = (Button) view.findViewById(R.id.buttonContact); 
    myButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    {if (xmlList.size() == 0) {Log.e(null, "empty list");}        final CharSequence[] charSequenceItems = xmlList.toArray(new CharSequence[xmlList.size()]); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setTitle("Backup Date"); 
     builder.setItems(charSequenceItems, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) {   
      Toast.makeText(getActivity(), "Restore done for "+ charSequenceItems[item], Toast.LENGTH_SHORT).show(); 
      } 
     }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
     } 
     } 
      }); 
    } 

但是这里的列表是空的!

+0

我看不到您称之为XMLContact()方法的位置 –

回答

0

,因为你不填充它这是空的(RestoreFragment.xmlList是不一样的CreateContactXML.xmlList)。将以下内容添加到RestoreFragment

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    if (CreateContactXML.xmlList.size() != 0) { 
     xmlList = CreateContactXML.xmlList; 
    } else { 
     xmlList = CreateContactXML.XMLContact(...); 
    } 
}