我需要在片段中调用片段时的listView
问题描述:
我需要帮助。我有一个活动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();
}
}
答
你需要将项目添加到mapFill
后打电话到adapter.notifyDataSetChanged();
为ListView
。
call adapter.notifyDataSetChanged();这里 ? SimpleAdapter adapter = new SimpleAdapter(getContext(),mapFill,R.layout.diseno_negocio_comments,negInfo,views); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); – Andressualu
@Andressualu你需要它! – kimkevin