I have a problem displaying a ListView in a Fragment

1

I have a problem trying to show listview in fragment . The listview appears in white, but when I put a break point everything goes perfect.
This is the code of my fragment :

public class ListRequestFragment extends Fragment implements AdapterView.OnItemClickListener {

    private View rootView;
    private ListView listView;
    private RequestAdapter adapterListView;
    private ArrayList<Solicitudes> _solicitudes;
    private int counter = 0;
    private RequestTask mAuthTask = null;
    private ProgressBar request_progress;
    private String _tipoServicio = null;
    private Button btnActualizar;

    public ListRequestFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_list_request, container, false);

        Bundle bundle = new Bundle();
        _tipoServicio = getArguments() != null ? getArguments().getString("TipoServicio"): "";

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        attemptRequest();
        this.listView = (ListView) rootView.findViewById(R.id.listView);
        listView.setOnItemClickListener(this);
        adapterListView = new RequestAdapter(getActivity(), R.layout.item_list_request, _solicitudes);
        listView.setAdapter(adapterListView);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    private void attemptRequest() {
        if (mAuthTask != null) {
            return;
        }

        boolean cancel = false;
        View focusView = null;

        if (_tipoServicio == null) {
            cancel = true;
        }

        if (!cancel) {
            mAuthTask = new RequestTask(_tipoServicio);
            mAuthTask.execute((Void) null);
        }
    }

    public class RequestTask extends AsyncTask<Void, Void, Boolean> {

        private final String tipoServicio;

        RequestTask(String servicio) {
            tipoServicio = servicio;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.
            try {
                Service gestor = new Service();
                _solicitudes = gestor.GetSolicitudesByTipoServicio(tipoServicio);
            } catch (Exception e) {
                return false;
            }

            return _solicitudes != null;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;

            if (!success) {
                Toast.makeText(getContext(), "No se pudo cargar los datos", Toast.LENGTH_LONG).show();

            }
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
        }
    }
 }

And this is my adapter:

public class RequestAdapter extends BaseAdapter {

    private Context context;
    private int layout;
    private ArrayList<Solicitudes> list = null;

    public RequestAdapter(Context context, int layout, ArrayList<Solicitudes> list) {
        this.context = context;
        this.layout = layout;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Solicitudes getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layout, null);
            holder = new ViewHolder();
            holder.profile_name = (TextView) convertView.findViewById(R.id.lblProfileName);
            holder.profile_address = (TextView) convertView.findViewById(R.id.lblProfileAddress);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Solicitudes currentSolicitud = getItem(position);
        holder.profile_name.setText(currentSolicitud.getNombresCliente() + " "  + currentSolicitud.getApellidosCliente());
        holder.profile_address.setText(currentSolicitud.getCallePrincipal() + " y " + currentSolicitud.getCalleSecundaria());

        return convertView;
    }

    static class ViewHolder {
        private TextView profile_name;
        private TextView profile_address;
    }
}
    
asked by Luis Garzon 09.07.2017 в 08:05
source

1 answer

0

For what you say, it probably happens because you have an asynchronous process which fills in the data, and you have an AsyncTask, in this case what is done is to update the data of adapter and% ListView called the onPostExecute() method, where data acquisition and termination of the asynchronous process is ensured.

@Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;

            if (!success) {
                Toast.makeText(getContext(), "No se pudo cargar los datos", Toast.LENGTH_LONG).show();

            }else {
              //*** Actualiza datos.
            }


        }
    
answered by 10.07.2017 / 10:46
source