Home > Blockchain >  ArrayList<String> dont charge data - JAVA
ArrayList<String> dont charge data - JAVA

Time:04-04

I doing a ArrayList with a Class "Company", this list a companys what get for firestore.

The code for class is:

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;
    }`

This class work fine, i used on spinners but when i use for charge a ArrayList on fragment don't work. The code for fragment is:

Company company = new Company();
ArrayList<String> arrayList = new ArrayList<>(company.companyList(root));
Log.i("Current User: ", arrayList.toString());

When executed code the log show this:

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]

The first log show a arrayList from fragment and second log show the class arrayList.

I can't charge the arrayList at fragment as a class show, what's wrong??

I try this methods for charge a ArrayList:

  1. ArrayList<String> arrayList = new ArrayList<>(company.companyList(root)

  2. ArrayList<String> arrayList = new ArrayList<>(); arrayList.addAll(company.companyList(root));

  3. ArrayList<String> arrayList; arrayList = (ArrayList<String>) company.companyList();

CodePudding user response:

I could suggest that you start by declaring a variable as an ArrayList such as :

ArrayList<String> resultArraylist = new ArrayList<>();

And then you add your first item :`

resultArrayList.add("Seleccionne un providor");

With ArrayList except if you always want to add in first position, the method add will put your item at the end of the list.

CodePudding user response:

Louis Wasserman, Cheshiremoe, Thomas Koenig, thanks for reply.

Thomas, i tried with this method but no result.

Louis and Cheshiremoe, i try implemented a AsyncTask. On doInBackground put the code what get arraylist from firestore, in onPostExecute i try compare a arrayList. The code is this:

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());
    }
}

and the call is:

new Task().execute();

but this don't work as expected, the log is:

I/Current User TASK:: [Seleccione un proveedor]

Is the first time i implement AsyncTask, is this way correct??

  • Related