Listview in the Asynctask

Asked

Viewed 66 times

0

My code does not load the data to list view when ta inside the AsyncTask, but if it is placed in the onCreateView works perfectly.

public class Tab_os_Fechadas extends Fragment {

    private String url ="http://redeml.no-ip.info/controleos/mobile/list_os_fechadas.php";
    private String parametros ="";
    private  ListView listOSFechadas;
    private int i;

    private LayoutInflater inflater;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


       View x =  inflater.inflate(R.layout.tab_os_fechada,null);

        new SolicitarDados().execute(url);
        return x;

    }

    private  class SolicitarDados extends AsyncTask<String, Void,String > {


        View view;

        @Override
        protected String doInBackground(String... urls) {
            return ConexaoDB.postDados(urls[0], parametros);
        }

        @Override
        protected void onPostExecute(String result) {

            try {
                JSONArray jsonarray = new JSONArray(result);
                for (int i = 0; i < jsonarray.length(); i++) {
                    JSONObject jsonobject = jsonarray.getJSONObject(i);

                }


                ArrayList<ItemOSFechadas> listaFechada = new ArrayList<ItemOSFechadas>();

                ItemOSFechadas a = new ItemOSFechadas("g1","ffff");
                ItemOSFechadas b = new ItemOSFechadas("g1","ffff");
                ItemOSFechadas c = new ItemOSFechadas("g1","ffff");
                ItemOSFechadas d = new ItemOSFechadas("g1","ffff");
                listaFechada.add(a);
                listaFechada.add(b);
                listaFechada.add(c);
                listaFechada.add(d);

                ListAdapterOSFechadas adapterOSFechadas = new ListAdapterOSFechadas(getContext(),listaFechada);
                listOSFechadas = (ListView)view.findViewById(listFechadasOS);
                listOSFechadas.setAdapter(adapterOSFechadas);
            }catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}

My intention is to make my JSON return and assemble my entire list view.

1 answer

0


I can’t run your code so I’m not sure, but I think your problem is related to your Listview id.

listOSFechadas = (ListView)view.findViewById(listFechadasOS);

I don’t understand why listechadasOs id, since there is no integer(int) with this value.

There are also some mistakes with the View. Try to edit your code as follows:

//Criando variável view
private View view;
//Remova sua LitView da AsyncTask e defina em onCreateView.
@Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (view == null) {
        view = inflater.inflate(R.layout.tab_os_fechada,null);
       listOSFechadas = (ListView)view.findViewById(R.id.id_da_listview);
       new SolicitarDados().execute(url);
       }
       return view; 
 }
  • I tried that way, and it didn’t work. my listview does not appear the data I create, my Requested function is usually executed, but not the data from listview

  • can hit, and working ok , but I went to test on mobile and listview did not load, but in emulator android studio uploaded, how can I identify the problem ?

  • It’s pretty hard to tell from just looking at this piece of code. Try sending your project to github and provide the link so I can try to identify your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.