[英]I need a listView in a fragment when the fragment is called
i need help. 我需要帮助。 I have an activity with 2 fragments, the first one works fine but the second fragment that has a listview, does not show the items when i start that fragment, only show the items if i click on crearComentario button and go back to the fragment, only if i do this is when the listview load the items.
我有一个包含2个片段的活动,第一个片段工作正常,但是第二个片段具有列表视图,启动片段时不显示项目,仅当我单击crearComentario按钮并返回片段时才显示项目,只有当我这样做时,listview加载项目。 this is my class, if you see, i fill the list view with data obtained from a StringRequest and ResponseListener and it works fine but the only part that doesnt work is when i want to show the items when i change from the first fragment to the second fragment(fragment where i have the listView).
这是我的课程,如果您看到的话,我用从StringRequest和ResponseListener获得的数据填充列表视图,它工作正常,但是唯一不起作用的部分是当我想显示从第一个片段更改为第二个片段(我有listView的片段)。 Please someone know how to show the list view when i navigate to fragment a to fragment b
当我导航到片段a到片段b时,请有人知道如何显示列表视图
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();
}
} }
You need to call adapter.notifyDataSetChanged();
您需要调用
adapter.notifyDataSetChanged();
for ListView
after adding items to mapFill
. 将项目添加到
mapFill
后,用于ListView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.