Help ... ListView does not show Items in fragment until after entering an activity and returning

1

I have an activity with 2 fragments,

  • The first fragment that is the first one that opens opens the information correctly.

  • The second fragment has a listView that contains a format as if they were comments, this listview fills it with data that I extract from a json of a php, besides this fragment contains a button that sends you to an activity to write a comment and send it.

The problem is that when I enter that second fragment, the list does not load, the pure add comment button appears and I realized that if I press the send comment button and return to the fragment, that's where the list. Apparently, that fragment does not run until I enter an activity and return directly to that fragment.

NOTE: the json data is received with a StringRequest and a ResponseListener. I leave the code of the class of the fragment to check to see if I need to put something.

public class BusinessCommentFragment extends Fragment {     private ListView listView;     private FloatingActionButton createComment;     private String neg_Name;     Final List > mapFill = new ArrayList> ();     private String neg_id;

public NegocioCommentFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_negocio_comments, container, false);


}


public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    crearComentario = (FloatingActionButton) view.findViewById(R.id.agregarComentario);
    listView = (ListView) view.findViewById(R.id.commentsList);

    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("userData", MODE_PRIVATE);
    final String usrapp_id = sharedPreferences.getString("usrapp_id", null);


    //Accedemos a los extras para ectraer nombre e id del negocio
    Intent negocioInfo = getActivity().getIntent();
    final Bundle paqueteInfo = negocioInfo.getExtras();
    neg_Nombre = paqueteInfo.getString("neg_Nombre");
    neg_id = paqueteInfo.getString("neg_id");


    crearComentario.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent enviarComentarioActivity = new Intent(getContext(), EnviarComentario.class);
            enviarComentarioActivity.putExtra("neg_Nombre", neg_Nombre);
            enviarComentarioActivity.putExtra("usrapp_id", usrapp_id);
            enviarComentarioActivity.putExtra("neg_id", neg_id);
            startActivity(enviarComentarioActivity);


        }
    });

    //Creamos arreglos para el adaptador de los comentarios
    String[] negInfo = new String[]{"nombre", "nc_comentario",};
    int[] views = new int[]{R.id.userNameCommentTextView, R.id.userCommentTextView};

    //LLenamos los componentes de la lista de comentarios con los arreglos en donde se guardaron
    SimpleAdapter adapter = new SimpleAdapter(getContext(), mapFill, R.layout.diseno_negocio_comments, negInfo, views);
    listView.setAdapter(adapter);


    //Traemos todos los comentarios del que se han hecho últimamente al negocio
    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray comentariosArray = new JSONArray(response);

                for (int i = 0; i < comentariosArray.length(); i++) {

                    JSONObject comentarioJson = comentariosArray.getJSONObject(i);

                    String nombre = comentarioJson.getString("nombre");
                    String comentario = comentarioJson.getString("nc_comentario");
                    String fechaComentario = comentarioJson.getString("nc_fecha");
                    String calificacion = comentarioJson.getString("calificacion");

                    HashMap<String, String> commentsInfo = new HashMap<String, String>();

                    commentsInfo.put("nombre", nombre);
                    commentsInfo.put("nc_comentario", comentario);
                    commentsInfo.put("fechaComentario", fechaComentario);
                    commentsInfo.put("calificacion", calificacion);
                    mapFill.add(commentsInfo);


                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    };

    TraeComentariosRequest traeComentariosRequest = new TraeComentariosRequest(neg_id, responseListener);
    RequestQueue queue = Volley.newRequestQueue(getActivity());
    queue.add(traeComentariosRequest);





}

@Override
public void onStart() {
    super.onStart();

}

}

    
asked by Andressualu 19.10.2017 в 21:18
source

1 answer

1

When you pass the variable mapFill to the adapter it is empty, since you assign it to this adapter after loading the adapter. That's why it shows you the data after going to and from the activity, since there is the variable mapFill if it contains data. Keep in mind that the data to the variable mapFile is assigned after loading the adapter.

// Cargas el adaptador al cual le estas pasando la variable mapFill vacia
SimpleAdapter adapter = new SimpleAdapter(getContext(), mapFill, R.layout.diseno_negocio_comments, negInfo, views);
listView.setAdapter(adapter);

// Rellenas la lista que le pasas al adaptador despues de cargarlo
Response.Listener<String> responseListener = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
                ...
                mapFill.add(commentsInfo);
            }
        ...
    }
};

Solution

To solve your problem simply load your adapter after storing the data in the variable mapFile , after the response.

//Traemos todos los comentarios del que se han hecho últimamente al negocio
Response.Listener<String> responseListener = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
            JSONArray comentariosArray = new JSONArray(response);

            for (int i = 0; i < comentariosArray.length(); i++) {

                JSONObject comentarioJson = comentariosArray.getJSONObject(i);

                String nombre = comentarioJson.getString("nombre");
                String comentario = comentarioJson.getString("nc_comentario");
                String fechaComentario = comentarioJson.getString("nc_fecha");
                String calificacion = comentarioJson.getString("calificacion");

                HashMap<String, String> commentsInfo = new HashMap<String, String>();

                commentsInfo.put("nombre", nombre);
                commentsInfo.put("nc_comentario", comentario);
                commentsInfo.put("fechaComentario", fechaComentario);
                commentsInfo.put("calificacion", calificacion);
                mapFill.add(commentsInfo);


            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
};

//LLenamos los componentes de la lista de comentarios con los arreglos en donde se guardaron
SimpleAdapter adapter = new SimpleAdapter(getContext(), mapFill, R.layout.diseno_negocio_comments, negInfo, views);
listView.setAdapter(adapter);
    
answered by 19.10.2017 / 23:46
source