2017-10-18 55 views
0

我需要幫助。我有一個活動2片段,第一個工作正常,但第二個片段有一個列表視圖,沒有顯示項目,當我開始片段,只顯示項目,如果我點擊crearComentario按鈕,並返回到片段,只有當我這樣做是當列表視圖加載項目。這是我的課,如果你看到,我填充列表視圖與從StringRequest和ResponseListener獲得的數據,它工作正常,但唯一不工作的部分是當我想顯示項目時,我從第一個片段更改爲第二個片段(片段,我有listView)。請人知道如何顯示在列表視圖,當我瀏覽到一個片段到片段B我需要在片段中調用片段時的listView

public class NegocioCommentFragment extends Fragment { 
private ListView listView; 
private FloatingActionButton crearComentario; 
private String neg_Nombre; 
final List<HashMap<String, String>> mapFill = new ArrayList<HashMap<String, String>>(); 
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(); 

} 

}

回答

1

你需要將項目添加到mapFill後打電話到adapter.notifyDataSetChanged();ListView

+0

call adapter.notifyDataSetChanged();這裏 ? SimpleAdapter adapter = new SimpleAdapter(getContext(),mapFill,R.layout.diseno_negocio_comments,negInfo,views); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); – Andressualu

+0

@Andressualu你需要它! – kimkevin