私はRetriever.getCourses(username、password)を使ってダウンロードしましたが、後でValues.Coursesがnullに設定されているというエラーが表示されます。私はAsyncTaskを間違って書いたと思います。ありがとう!グレード断片中データをダウンロードして変数に保存するAsynctask?
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
private final String muser;
private final String mPassword;
private boolean checked;
private ArrayList<Course> crs;
private String name;
private ArrayList<Teacher> tc;
UserLoginTask(String user, String password) {
muser = user;
mPassword = password;
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
try {
return Retriever.logInTest(muser, mPassword);
} catch (Exception e) {
return false;
}
// TODO: register the new account here.
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
if (success) {
SharedPreferences sharedPref = Login.this.getSharedPreferences("Login",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Username", muser);
editor.putString("Password", mPassword);
crs = Retriever.getCourses(muser, mPassword);
Values.courses = crs;
editor.commit();
Intent intent = new Intent(Login.this,MainActivity.class);
startActivity(intent);
finish();
} else {
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}
public class UserCourseTask extends AsyncTask<Void, Void, ArrayList>{
private String username;
private String password;
private ArrayList<Course> courses;
public UserCourseTask(String user, String pass)
{
username = user;
password = pass;
}
protected ArrayList doInBackground(Void... params)
{
try{
courses = Retriever.getCourses(username,password);
return courses;
}
catch (Exception e){
Log.e("MYAPP", "exception", e);
}
return courses;
}
protected void onPostExecute(ArrayList<Course> result)
{
setCourses(result);
}
}
。 courses.size()
は、あなたの代わりにコースを返し、nullを返したdoInBackground内のエラー
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_grades, container, false);
ListView yourListView = (ListView) rootview.findViewById(R.id.listview);
ArrayList<Course> courses = Values.courses;
for(int i = 0; i<courses.size();i++)
{
if(courses.get(i).getSubject().equals("Lunch"))
{
courses.remove(i);
break;
}
}
yourListView.setAdapter(new CourseAdapter(rootview.getContext(),courses));
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Values.assignments = Values.courses.get(position).getAssignments();
Values.assingmentsclass = Values.courses.get(position).getSubject();
Intent intent = new Intent(getActivity(), Scrnassign.class);
startActivity(intent);
}
});
return rootview;
}
のようなコードを使用することができます私はValues.courses'は、いくつかの静的変数である 'と仮定していますか?あなたのAsyncTaskが、私は静的変数の代わりに使用すべきか 'ArrayListの' –
内のクラスとの間でデータを渡すために静的変数を使うべきではないのですか? –
を返すように定義されていないため、これはコンパイルべきではない、またAndroidの – Engima