Reputation: 1859
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.
Upvotes: 1
Views: 3899
Reputation: 7486
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.
Upvotes: 2
Reputation: 697
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.
Upvotes: 1