简体   繁体   English

调用片段时,我需要片段中的listView

[英]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.

相关问题 片段中的自定义ListView,我需要哪些文件? - Custom ListView in a fragment, what files do I need? 当我在片段2时,需要加载或运行片段1中的函数,更新表,然后转到片段1 - Need to load or run a function in fragment 1 when I'm at fragment 2, update a table, then go to fragment 1 我需要在同一片段内引用一个片段 - I need to reference a fragment inside the same fragment 未聚焦时更新ListView片段 - Updating ListView fragment when not focused 片段和列表视图 - Fragment and Listview 片段的自定义ListView只是在旋转Android Phone时出现 - Custom ListView in Fragment just appear when I rotate the Android Phone 实现LoaderCallbacks的片段(片段内ViewPager的片段)的ListView <Cursor> 当我回到它是空的 - ListView of a Fragment (of a ViewPager inside a Fragment) that implements LoaderCallbacks<Cursor> is empty when I return back to it 当我转到另一个片段时片段重复 - Fragment duplicates when I go to another Fragment 当 SQLite 数据库在另一个 Fragment 中更新时更新 Fragment 中的 ListView - Update ListView in Fragment when SQLite database is updated in another Fragment 加载新片段时调用上一个片段 onCreateOptionMenu - Previous fragment onCreateOptionMenu is called when loading a new fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM