You probably need the first two lines only, because gridView.setAdapter(adapter)
doesn't make sense if the adapter is already set.
I don't think you need an AsynkTask for refreshing your collection, just modify your adapter to be aware of item types: when the filter is set (an integer that represent one of the 3 states: all, closed, opened), your adapter should report the correct count and return the proper item (you could use 3 different item collections, although this means duplicating your data).
gridView.notifyDataChanged()
triggers a new getCount()
call in your adapter, followed by multiple calls to getView(..)
. The gridView.invalidateViews()
says that child views need to be redrawn. You don't need to recreate the gridView.
If your items should look similar to what you drawn, I think you could use a ListView (with the same adapter).
Update:
It's OK if getCount()
is getting called multiple times, but you should avoid performing heavy tasks in this method (not the case at the moment).
In getView(..)
you should deliver a view which is fully synchronized with the corresponding position, no matter if you reuse a previously created one or not. So the problem is that you are updating the view only if it's freshly created (convertView==null)
, although you should update the title's text &CO. right before the view is returned.
Related to your "ghost item", I don't know how big are your layout items, if they are visible all at once etc., but your adapter should create at least as many views as the visible ones.
Update2:
Modify your adapter as below, call prepareData()
in your adapter's constructor, and see if it's working:
private ArrayList<Integer> closedIndexToRealIndex;
private ArrayList<Integer> openedIndexToRealIndex;
private void prepareData(){
nOpen = nClosed = 0;
closedIndexToRealIndex = new ArrayList<Integer>();
openedIndexToRealIndex = new ArrayList<Integer>();
for(int i = 0; i < values.count; i++){
if(vStatus[i] == 0){
openedInexToRealIndex.add(Integer.valueOf(i));
nOpen++;
}
else{
closedIndexToRealIndex.add(Integer.valueOf(i));
nClosed++;
}
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = ((MainActivity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.item_main, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.text);
viewHolder.imageview = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
int actualIndex = 0;
switch(visibleFlag){
case 0://all
actualIndex = position;
break;
case 1://opened
actualIndex = openedIndexToRealIndex.get(position).intValue();
break;
case 2://closed
actualIndex = closedIndexToRealIndex.get(position).intValue();
break;
default:
break;
}
viewHolder.text.setText(values[acutalIndex]);
if(vStatus[position] == 1)
viewHolder.imageview.setImageResource(R.drawable.ic_open);
else
viewHolder.imageview.setImageResource(R.drawable.ic_closed);
return convertView;
}