1
Hello, I have an Android application of an electronic questionnaire. Should be presented the same questionnaire in tabs, are 7 tabs. The application downloads web issues from an XML.
I’m using the Volley library to leak access to the questionnaire. When I lower the questionnaire and do the XML Parsing write to the database and then display in a Listview.
The problem is that the questions do not show up in the first two tabs. They appear from the third. And when accessing the third the questions appear in the first and when accessing the fourth they appear in the second. The others work normally.
The code of the Fragments are these:
public class MainActivity extends FragmentActivity {
//declarações e onCreate...
private void requestQuestionario(){
String tag_string_req = "req_questionario"; //tag para cancelar o request
pDialog.setMessage("Carregando Questionário...");
showDialog();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
RequestContract.XMLQuestionarioContract.XML_QUESITIONARIO_URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.d(TAG, "Response = " + s);
//TODO -- Tratar Response
InputStream xml = new ByteArrayInputStream(s.getBytes());
try {
HashMap questionario = XmlParser.parse(xml);
List<Questao> questoes = (List) questionario.get("questoes");
//insere questoes no banco....
QuestionarioCache cache = QuestionarioCache.newInstance();
cache.setQuestoes(questoes);
cache.getAdapter(getApplication()).notifyDataSetChanged();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Carrega as questões existentes no banco....
}
});
requestQueue = RequestManager.getInstance(this).getRequestQueue();
stringRequest.setTag(tag_string_req);
requestQueue.add(stringRequest);
}
}
Fragment with a Listview
public class ListaQuestoesActivity extends Fragment {
private static final String TAG = ListaQuestoesActivity.class.getSimpleName();
private View v;
private TextView textView;
private ListView listView;
private QuestionarioCache cache;
private int dia;
// onCreate....
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_lista_questoes, container, false);
textView = (TextView) v.findViewById(R.id.txt_dia_qionario);
textView.setText("Dia " + dia);
carregarListaQuestoes();
return v;
}
private void carregarListaQuestoes(){
listView = (ListView) v.findViewById(R.id.list_questoes);
cache = QuestionarioCache.newInstance();
listView.setAdapter(cache.getAdapter(getActivity()));
}
}
When I use a static list it appears in all tabs normally:
String[] list = {"Ola", "Mundo", "Foo", "Bar"};
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
Someone knows how to fix?
check that your Questionariocache is the same in all Fragments. Maybe each of your tabs is creating their own cache and the load time for each of them may be different, since IO is asynchronous. According to Main’s code, its cache seems to be a local instance.
– Marco Aurelio
Questionariocache is a Singleton, that may be the problem?
– André Minoro Fusioka
This article by Jon Skeet is an excellent reference for writing legal questions: http://tinyurl.com/stack-hints
– brasofilo