我用一个类做一个ArrayList"Company";,这份清单列出了一家公司的消防用品商店。
类的代码是:
public ArrayList<String> companyList (View view) {
CompanyList.add(0, "Seleccione un proveedor");
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").document(currentFirebaseUser.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
User = (HashMap<String, Object>) documentSnapshot.getData();
if(User.get("List") != "") {
List = (HashMap<String, Object>) User.get("List");
CompanyList.addAll(List.keySet());
Log.i("Current User: ", CompanyList.toString());
}
}
});
return CompanyList;
}`
这个类工作得很好,我在微调器上使用过,但当我在片段上使用ArrayList进行充电时,它不起作用。碎片的代码是:
Company company = new Company();
ArrayList<String> arrayList = new ArrayList<>(company.companyList(root));
Log.i("Current User: ", arrayList.toString());
当执行代码时,日志显示:
2022-04-02 13:34:12.370 17434-17434/com.martin.preventapp I/Current User:: [Seleccione un proveedor] 2022-04-02 13:34:12.615 17434-17434/com.martin.preventapp I/Current User:: [Seleccione un proveedor, Nutrifresca, Ideas Gastronomicas, Pollo]
第一个日志显示片段中的arrayList,第二个日志显示类arrayList。
我不能把fragment上的arrayList当作类显示来收费,怎么了??
我尝试这种方法来收费ArrayList:
ArrayList<字符串>arrayList=新的arrayList<gt;(company.companyList(root(
数组列表<字符串>arrayList=新的arrayList<gt;((;arrayList.addAll(company.companyList(root((;
数组列表<字符串>arrayList;arrayList=(arrayList<String>(company.companyList((;
我建议您首先将变量声明为ArrayList,例如:
ArrayList<String> resultArraylist = new ArrayList<>();
然后添加您的第一个项目:`
resultArrayList.add("Seleccionne un providor");
对于ArrayList,除非您总是想在第一个位置添加,否则add方法会将您的项目放在列表的末尾。
托马斯,我试过这种方法,但没有结果。
Louis和Cheshiremoe,我尝试实现一个AsyncTask。在doInBackground上放入从firestore获取arraylist的代码,在onPostExecute中,我尝试比较arraylist。代码是这样的:
private class Task extends AsyncTask<String, String, ArrayList<String>> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected ArrayList<String> doInBackground(String... params) {
Company company = new Company();
// Call this to update your progress
//publishProgress(companyList);
return company.companyList(getView());
}
// This is called from background thread but runs in UI
protected void onProgressUpdate(ArrayList<String> arr1) {
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
ArrayList<String> companyList = new ArrayList<String>(result);
TextView tvt = getView().findViewById(R.id.textView7);
if(companyList.size() > 1){
addFragmentListAdd();
tvt.setText(companyList.toString());
}
Log.i("Current User TASK: ", companyList.toString());
}
}
呼叫是:
new Task().execute();
但这并没有像预期的那样工作,日志是:
I/Current User TASK:: [Seleccione un proveedor]
这是我第一次实现AsyncTask吗??