在启动应用程序时,我想用数据库中的数据填充片段中的微调器。我从数据库中提取字符串列表,并希望在微调器中显示它。我已经有了填充列表,我相信在PostExecute中更改了ArrayAdapter,但是什么都没有显示。
片段代码
public static class RegisterFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<String> instituteList = new ArrayList<String>();
View rootView = inflater.inflate(R.layout.fragment_register, container, false);
if (instituteList.isEmpty()){ //list is null on app start-up
Log.d("test","null list");
}else{
Spinner autoInstitute = (Spinner)getActivity(). findViewById(R.id.institute);
// Get the string array
String[] institutes = (String[]) instituteList.toArray(new String[instituteList.size()]);;
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> instituteApapdter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, institutes);
instituteApapdter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
autoInstitute.setAdapter(instituteApapdter);
}
return rootView;
}
异步类
class getInstitute extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Loading Institute List");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
//Get data and store in List
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
institutes_array = (String[]) instituteList.toArray(new String[instituteList.size()]);;
final ArrayAdapter<String> instituteApapdter =
new ArrayAdapter<String>(Login.this, android.R.layout.simple_list_item_1, institutes_array);
instituteApapdter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
runOnUiThread(new Runnable() {
public void run() {
instituteApapdter.notifyDataSetChanged();
}
//autoInstitute.setAdapter(instituteApapdter); // causes nullpointerexception
});
}
答案 0 :(得分:1)
写下这个:
Spinner autoInstitute = (Spinner)rootView. findViewById(R.id.institute);
取代这个:
Spinner autoInstitute = (Spinner)getActivity(). findViewById(R.id.institute);
此外,在AsyncTask类中,您已将Login.this
写为ArrayAdapter()
构造函数的第一个参数。将其更改为getActivity()
这应该是您完整的RegisterFragment类:
package com.bhavit.stackoverflow;
public static class RegisterFragment extends Fragment{
Spinner autoInstitute;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<String> instituteList = new ArrayList<String>();
View rootView = inflater.inflate(R.layout.fragment_register, container, false);
if (instituteList.isEmpty()){ //list is null on app start-up
Log.d("test","null list");
}else{
autoInstitute = (Spinner)getActivity(). findViewById(R.id.institute);
GetInstitute obj = new GetInstitute();
obj.execute();
}
return rootView;
}
class GetInstitute extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading Institute List");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
//Get data and store in List
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
institutes_array = (String[]) instituteList.toArray(new String[instituteList.size()]);;
final ArrayAdapter<String> instituteApapdter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, institutes_array);
instituteApapdter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
instituteApapdter.notifyDataSetChanged();
autoInstitute.setAdapter(instituteApapdter); // causes nullpointerexception
}
}
}