在启动应用程序时,我想用来自数据库的数据在片段中填充微调器。我从数据库中拉出一个字符串列表,并希望将其显示在微调器中。我已经得到了列表来填充,我相信ArrayExapter在PostExecute中被更改,但是什么时候什么都没有显示。使用来自Async任务的数据更新微调器
片段代码
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
});
}
请澄清你的问题有点多。 – Yuriy
所以你正在'doInBackground'中更新'instituteList'? –
耶instituteList在doInBackground – bobeyuno