这是我的java代码
public class FragmentAssignTask extends Fragment {
ProgressDialog progressDialog;
DocumentReference dr;
CollectionReference cr = FirebaseFirestore.getInstance().collection("tasks");
SharedPreferences sp;
final List<String> tasks = new ArrayList<String>();
final List<String> users = new ArrayList<String>();
View myview;
Spinner sp1;
Spinner sp2;
Button btn;
ArrayAdapter<String> adp1,adp2;
public String jsonResponse;
public FragmentAssignTask() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
myview = inflater.inflate(R.layout.fragment_assign_task, container, false);
sp1 = (Spinner) myview.findViewById(R.id.spinner1);
sp2 = (Spinner) myview.findViewById(R.id.spinner2);
btn = myview.findViewById(R.id.btn_assignTask);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Syncing Tasks");
progressDialog.show();
initiaizeValues();
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView arg0, View arg1, int position, long id) {
sp1.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
sp2.setSelection(position);
} // to close the onItemSelected
public void onNothingSelected(AdapterView parent)
{
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String tn;
if(sp1.isSelected())
tn = sp1.getSelectedItem().toString();
String un ;
if(sp2.isSelected())
un= sp2.getSelectedItem().toString();
progressDialog.setMessage("Assigning Task");
progressDialog.setCancelable(false);
progressDialog.show();
tn = "Task 2";
un = "umar@gmail.com";
String documentString = "clienttask/" + un + "/tasks/"+tn;
dr = FirebaseFirestore.getInstance().document(documentString);
Map<String, Object> data = new HashMap<String, Object>();
data.put("status", "i");
dr.set(data).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getActivity(),"Task Assigned Successfully",Toast.LENGTH_SHORT).show();
Log.d("assignTask", "task assigned to user");
progressDialog.dismiss();;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getActivity(),"Failed!!!",Toast.LENGTH_SHORT).show();
Log.d("assignTask", "task not assigned");
progressDialog.dismiss();;
}
});
}catch (Exception ex)
{
progressDialog.dismiss();
ex.toString();
}
}
});
return myview;
}
@Override
public void onStart()
{
super.onStart();
}
public void initiaizeValues()
{
try{
cr.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if(!documentSnapshots.isEmpty())
{
List<DocumentSnapshot> d = documentSnapshots.getDocuments();
DocumentSnapshot t;
String tname,description;
for(int i=0;i<d.size();i++)
{
t = d.get(i);
tname = t.getString("tname");
tasks.add(tname);
}
}
else
{
Toast.makeText(getActivity(),"No Task !!!",Toast.LENGTH_LONG).show();
}
}
});
sp = getActivity().getSharedPreferences("trainer",Context.MODE_PRIVATE);
String tid = sp.getString("trainerid","aun@gmail.com");
String collectionstring = "clienttrainer/"+ tid+"/clients";
cr = FirebaseFirestore.getInstance().collection(collectionstring);
cr.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if(!documentSnapshots.isEmpty())
{
List<DocumentSnapshot> d = documentSnapshots.getDocuments();
DocumentSnapshot t;
String cid;
for(int i=0;i<d.size();i++)
{
t = d.get(i);
cid = t.getString("name");
users.add(cid);
}
Toast.makeText(getActivity(),"Syncing Clients Sucessfull!!",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
else
{
Toast.makeText(getActivity(),"No user assigned!!!",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}catch(Exception ex)
{
int j = 0;
}
adp1 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,tasks);
adp2 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,users);
sp1.setAdapter(adp1);
sp2.setAdapter(adp2);
}
}
答案 0 :(得分:0)
您从Firebase
Asynchronously
获取数据并使用空列表创建Adapter
。
在创建包含数据的List后,您必须创建Adapter
,然后将Adapter设置为Spinner
。或者在获取数据并构建列表后,使用Adapter
方法通知notifyDataSetChanged()
数据集更改。
首先Spinner
:
cr.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if(!documentSnapshots.isEmpty())
{
List<DocumentSnapshot> d = documentSnapshots.getDocuments();
DocumentSnapshot t;
String tname,description;
for(int i=0;i<d.size();i++)
{
t = d.get(i);
tname = t.getString("tname");
tasks.add(tname);
}
}
else
{
Toast.makeText(getActivity(),"No Task !!!",Toast.LENGTH_LONG).show();
}
adp1 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,tasks);
sp1.setAdapter(adp1);
}
});
对第二个Spinner
使用相同的方法。