0

I have a problem that I've tried to resolve for some time with the tips I've found from googling on this issue.

My SimpleAdapter looks like this:

@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);

It displays items in a listview that is gathered from pushing the search query button in my searchview which fetches data from a JSON api.

My issue is that some of the items are empty which results in an empty space. I want this empty space to disappear.

I've tried with the code below but never figured out how to get it to work

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

any tips?

3
  • remove that notifyDataSetChanged(); and try. Commented Oct 31, 2018 at 10:06
  • 1
    While creating contactList check whether any field is empty, then do not add that empty field into the list, this is how it will form the list of data which is not empty, then you can call notifyDataSetChanged with updated list. Commented Oct 31, 2018 at 10:08
  • Thanks for the tip, how would I go on about checking contactList if its empty? Any tips?
    – midw
    Commented Oct 31, 2018 at 10:16

1 Answer 1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.