如果列表项为空,则将可见性设置为不可见

时间:2018-10-31 10:02:37

标签: android listview visibility

我有一个问题已经尝试解决了一段时间,这是我通过谷歌搜索此问题找到的提示。

我的SimpleAdapter看起来像这样:

@Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
                R.layout.list_item, new String[]{"namn", "avdelning", "epost", "mobil", "telnr"},
                new int[]{R.id.email, R.id.mobile, R.id.epost, R.id.mobil, R.id.telnr});
        lv.setAdapter(adapter);

它在列表视图中显示项目,该列表视图是通过按下我的searchview中的搜索查询按钮收集的,该按钮从JSON API获取数据。

我的问题是某些项目是空的,这导致空白。我希望这个空白消失。

我尝试使用下面的代码,但从未想出如何使其工作

ListAdapter adapter2 = new SimpleAdapter(this, contactList, R.layout.list_item, new String[]{"mobil"
}, new int[]{R.id.mobil})
{

    @Override

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            holder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
            holder.textView = (TextView) v.findViewById(R.id.mobil);
            //other stuff
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        HashMap<String, String> contact = contactList.get(position);

        if (TextUtils.isEmpty(contact.get("mobil"))) {
            holder.textView.setVisibility(View.GONE);
            holder.textView.setVisibility(View.INVISIBLE);
            notifyDataSetChanged();
        } else {
            holder.textView.setVisibility(View.VISIBLE);
        }
        //do the same thing for other possible views.
        return v;
    }

    class ViewHolder {
        TextView textView;
        //your other views
    }
};

任何提示?

1 个答案:

答案 0 :(得分:0)

public void removeUnwantedCharacter() {
    List<String> list = Arrays.asList("", "Hi", "", "How", "are", "you");
    List<String> result = new ArrayList<String>();
    if (!list.isEmpty()) {
        for (String str : list) {
            if (str != null && !str.isEmpty()) {
                result.add(str);
           }
        }
    }
    Log.d("++++++", result.toString());
}