How to remove the space from a list corresponding to an adapter, when I apply the function setVisible (View.gone) to the adapter?

StackOverflow https://stackoverflow.com/questions/21862771

Question

I'm trying to hide a adapter when my object "TuMesaModel" be a administrator, this is the code

public class TuMesaAdapter extends ArrayAdapter<TuMesaModel>{
    private String _id;

    public TuMesaAdapter(Context context, List<TuMesaModel> objects, String id) {
        super(context, 0, objects);
        _id = id;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        TuMesaModel entry = getItem(position);
        String numero = "+"+entry.getUsername();
        //Toast.makeText(getContext(), Settings.PREFERENCES.getString(Settings.PHONE_PREF, null), Toast.LENGTH_LONG).show();

        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.tumesa_user, parent, false);
            holder = new ViewHolder();
            holder.numero_personal_tu_mesa = (TextView) convertView.findViewById(R.id.numero_personal_tu_mesa);
            holder.aceptar_y_rechazar_tu_mesa = (LinearLayout) convertView.findViewById(R.id.aceptar_y_rechazar_tu_mesa);
            holder.foto_perfil_tu_mesa = (ImageView) convertView.findViewById(R.id.foto_perfil_tu_mesa);
            holder.mRelative = convertView.findViewById(R.id.layout_tu_mesa);
            Utils.setFontAllView((ViewGroup) holder.mRelative);
            convertView.setTag(holder);

        }else{
            holder =  (ViewHolder) convertView.getTag();
        }

        holder.numero_personal_tu_mesa.setText(entry.getUsername());

        if(!entry.isAdmin()){
            holder.mRelative.setVisibility(View.GONE);
            //Toast.makeText(getContext(), Settings.PREFERENCES.getString(Settings.PHONE_PREF, null), Toast.LENGTH_LONG).show();
            //Toast.makeText(getContext(), "Luego:"+numero, Toast.LENGTH_LONG).show();
        }
        return convertView;

    }

    private static class ViewHolder {
        public View mRelative;
        public TextView numero_personal_tu_mesa;
        public LinearLayout aceptar_y_rechazar_tu_mesa;
        public ImageView foto_perfil_tu_mesa;

    }

}

In the mainActivity

        Type collectionMesa = new TypeToken<Collection<TuMesaModel>>() {}.getType();
        _response = new Gson().fromJson(reader, collectionMesa);
        Log.i("Tamaño response TuMesaActivity", _response.size()+"");
        return null;
    }

    @Override
    protected void onPostExecute(Void Void) {
        super.onPostExecute(Void);
        setSupportProgressBarIndeterminateVisibility(false);
        _mesa_adapter = new TuMesaAdapter(TuMesaActivity.this, _response, _bundle.getString("_id"));
        _list_view.setAdapter(_mesa_adapter);
        _mesa_adapter.notifyDataSetChanged();


    }

But when I run the function setVisible (View.Gone) is a space in the list corresponding to the adapter. What can I do to remove that space??

Was it helpful?

Solution 3

If you want to remove all the data from the list then pass an empty list and make the following calls as you did earlier.

_mesa_adapter = new TuMesaAdapter(TuMesaActivity.this, _response, _bundle.getString("_id"));
_list_view.setAdapter(_mesa_adapter);
_mesa_adapter.notifyDataSetChanged();

or you can also try setting list adapter to null as,

_list_view.setAdapter(null);    

Lemme me know if this helps. :)

OTHER TIPS

You can't do it in a clean way. You must remove the non admin entries from your list, or create a list without those entries, and call notifyDataSetChanged.

Just filter the list in the constructor, something like this:

public TuMesaAdapter(Context context, List<TuMesaModel> objects, String id) {
    super(context, 0);
    final List<TuMesaModel> filtered = new ArrayList<TuMesaModel>();
    for(final TuMesaModel object : objects) {
        if(!object.isAdmin()){
            filtered.add(object);
        }
    }
    addAll(filtered);
    _id = id;
}

see ArrayAdapter#addAll (since API level 11)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top