无法在其他类中使用ArrayList

时间:2014-03-27 11:32:09

标签: android arraylist

我在这里创建了一个名为xmlList

的文件列表
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;
}

}

列表已创建,我可以看到其元素

2014年3月27日09-00-00.xml

2014年3月27日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();
        }
        }
            });
    }

但这里的清单是空的!!

1 个答案:

答案 0 :(得分:0)

它是空的,因为你没有填充它(RestoreFragment.xmlListCreateContactXML.xmlList不同)。将以下内容添加到RestoreFragment

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