当我将函数setVisible(View.gone)应用于适配器时,如何从与适配器对应的列表中删除空格?

时间:2014-02-18 18:50:43

标签: java android android-adapter android-adapterview

当我的对象“TuMesaModel”是管理员时,我正在尝试隐藏适配器,这是代码

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;

    }

}

在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();


    }

但是当我运行函数时,setVisible(View.Gone)是一个与适配器对应的列表中的空格。我该怎么做才能删除那个空间?

3 个答案:

答案 0 :(得分:1)

你不能以干净的方式做到这一点。 您必须从列表中删除非管理员条目,或创建没有这些条目的列表,然后致电notifyDataSetChanged

答案 1 :(得分:1)

只需在构造函数中过滤列表,如下所示:

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;
}

请参阅ArrayAdapter#addAll(自API级别11以来)

答案 2 :(得分:0)

如果要从列表中删除所有数据,请传递一个空列表并按照之前的步骤进行以下调用。

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

或者您也可以尝试将列表适配器设置为null,

_list_view.setAdapter(null);    

告诉我这是否有帮助。 :)