0

I'm new in android programmin so,please pardon if i did silly mistake

I want the list of users from Users collection and display in the spinner, so i can select i've tried almost everything but didn't resolve the issue, where is the problem? problem in my java code or in xml file? #Here is my XMl:#

           <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2">


                <TextView
                    android:layout_width="match_parent"
                    android:text="Assigned User"
                    android:textStyle="bold"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:layout_margin="15dp"
                    android:layout_height="wrap_content"/>
            </RelativeLayout>
            <RelativeLayout
                android:layout_weight="4"
                android:layout_height="wrap_content"
                android:layout_width="0dp">

                <Spinner
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:textColor="@color/black"
                    android:spinnerMode="dropdown"
                    android:id="@+id/edt_assigned_user_task"
                    />
            </RelativeLayout>

        </LinearLayout>

#Here is my code ---># when i select the item from the list the selected item dissappeared

spinnerUser = myview.findViewById(R.id.edt_assigned_user_task);
                spinnerProject =myview.findViewById(R.id.edt_project_task);
                populateSpinnerUser();



private void populateSpinnerUser() {
        final List<String> mUserList = new ArrayList<>();
        db.collection("Users").get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful()){
                            for(DocumentSnapshot documentSnapshot: task.getResult()){
                                Log.d("TAG", documentSnapshot.getId() + " => " + documentSnapshot.getData());
                                String user = documentSnapshot.getString("uID");
                                mUserList.add(user);
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }

                });

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
            android.R.layout.simple_spinner_item, mUserList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerUser.setAdapter(adapter);

}




spinnerUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        String userName = parent.getItemAtPosition(position).toString();
                        spinnerUser.setSelection(position);
                        Toast.makeText(parent.getContext(),userName,Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
anand patel
  • 33
  • 1
  • 5
  • Don't use `getApplicationContext()` in the `ArrayAdapter` instantiation. Use the current `Activity` instead. – Mike M. Jun 25 '20 at 07:42
  • Firebase API is asynchronous. Any code that needs data from Firestore, needs to be inside the onComplete method, or be called from there. So please check the duplicate to how can you solve this using a custom callback. – Alex Mamo Jun 25 '20 at 07:53

0 Answers0