1

I have a listview that for displaying detail data. I'm storing my data in an ArrayList of Strings. However, some of the fields may not have any data to display, but I need to keep the array length the same to match a static titles array. I can trap the empty data in my getView method in my custom base adaptor here:

        public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.drug_detail_cell, parent, false);
        }

        // check array bounds
        if (position < getCount()) {

            // check for null items
            String item = items.get(position);
            if (item != null) {

                // get the text views
                TextView title = (TextView) convertView.findViewById(R.id.item_title);
                TextView sub = (TextView) convertView.findViewById(R.id.item_subtitle);

                title.setText(titles.get(position));
                sub.setText(item.toString());                   
            } else {
                // delete row
            }
        }

        return convertView;
    }

My problem is that while the data does not display, I still have an empty row in my listview. My question is how do I delete that row? Any help would be greatly appreciated. Thanks in advanced.

2
  • delete the data that populates your listview call notifyDatasetChanged on your adapter to refresh or update listview Commented Aug 17, 2013 at 17:05
  • find textViews id in if condition not out of it while you are inflating the view. Commented Aug 17, 2013 at 17:34

2 Answers 2

2

For removing a row from the CustomListAdapter:

Remove the item from the ArrayAdapter from the specified index, after that call notifyDatasetChanged . It will update your listView.

In CustomAdapterClass:

@Override
public void remove(String object) {
    super.remove(object);

         // your other code
}

In ListActivity class:

CustomAdapterClass adap = new CustomAdapterClass();

adap.remove("hello world");

adap.notifyDatasetChanged(); // this will update your listView

My code is a bare bone example to depict how to achieve your goal.

Sign up to request clarification or add additional context in comments.

Comments

1

I have a tip: in else clause you return a empty view

else{
    View v = new View(context);
    v.setLayoutParams(new AbsListView.LayoutParams(0, 0));
    return v;
}

But if your list have divider, the divider below the empty view will be double.

In a different: I think you should handle all null data before getView call. I mean: - In getCount(){ loop and create a new map from position and not null data
loop and count all data!=null; return count; }

use new map in getView function.

Hope this help.

1 Comment

Thanks for the reply. I took your suggestion to handle null data before sending it to the array adapter. Since my data set is small, I created a loop to remove null data from my list and the associated header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.