【问题标题】:I need a listView in a fragment when the fragment is called调用片段时,我需要片段中的 listView
【发布时间】:2017-10-18 00:05:19
【问题描述】:

我需要帮助。我有一个包含 2 个片段的活动,第一个工作正常,但第二个片段有一个列表视图,当我启动该片段时不显示项目,仅在我单击 crearCommentario 按钮并返回片段时显示项目,只有当我这样做是列表视图加载项目时。这是我的课,如果你看到,我用从 StringRequest 和 ResponseListener 获得的数据填充列表视图,它工作正常,但唯一不起作用的部分是当我从第一个片段更改为第二个片段(我有listView的片段)。请有人知道当我导航到片段 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();

}

}

【问题讨论】:

    标签: java android listview mobile


    【解决方案1】:

    mapFill添加项目后,您需要为ListView调用adapter.notifyDataSetChanged();

    【讨论】:

    • 调用 adapter.notifyDataSetChanged();这里 ? SimpleAdapter 适配器 = new SimpleAdapter(getContext(), mapFill, R.layout.diseno_negocio_cmets, negInfo, views); listView.setAdapter(适配器); adapter.notifyDataSetChanged();
    • @Andressualu 是的,你需要它!
    猜你喜欢
    • 2019-10-29
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多