I am using Fragment to create a app which i can slide left and right to view questions. When i submit a question from another fragement it will update the 2nd listFragment/listView. However it always crash. Furthermore i tried storing stuff into the variable '"storeText"', it will be null after onActivityCreated.
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
public class UserQuestion extends ListFragment {
ArrayAdapter<String> adapter;
private UserQuestionStorage storage = new UserQuestionStorage();
private String storeText;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Set the list that was needed
ArrayList<String> getQuestionList = storage.getQuestionList();
this.adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getQuestionList);
//Test Storage
this.storeQuestion(123, 1111, "Question 1", "true", "false");
this.storeQuestion(123, 1111, "Question 2", "true", "false");
storeText = "Storing this to variable.";
setListAdapter(this.adapter);
}
public void storeQuestion(int id, int ownerID, String questionText, String optionOne, String optionTwo) {
Question userQuestion = new Question();
userQuestion.createQuestion(id, ownerID, questionText, optionOne, optionTwo);
this.storage.storeQuestion(questionText);
//TODO: This is not working
Log.d("QUESTION", "" + this.storage.getQuestionList()); //Storage is always wipe.
Log.d("ADAPTER", "" + adapter);
Log.d("TESTSTRING", "" + storeText);
//refreshList();
}
public void refreshList(){
this.adapter.notifyDataSetChanged();
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire