ListView in AsyncTask

0

My code does not load the data for list view when it is inside AsyncTask , but if it is placed in onCreateView it 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 mount all my list view.

    
asked by anonymous 08.06.2018 / 23:29

1 answer

0

I can not execute your code so I'm not sure, but I think your problem is related to your Listview id.

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

I do not understand why ClosedOs as id, since there is no integer (int) with this value.

There are also some errors with View . Try editing 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; 
 }
    
09.06.2018 / 01:06