Android Adaptor Row Hiding
Solution 1:
AFAIK you can't return a null view from getView
, but you could just make the view invisible and height 1. Although manipulating using the getCount
is probably the preferred way.
view.setVisibility(View.INVISIBLE);
view.getLayoutParams().height = 1;
Solution 2:
You'll need to have the adapter return the total number of non-null items with getCount
and then keep a mapping of position to your internal data structure.
For example. You have a list
1 - John
2 - null3 - Bill
4 - Susan
5 - null
When getCount is called it returns 3.
Then when getView
is called on position 1 you return the item at list[1]
.
getView
on position 2 returns list[3]
(as it's the 2nd non-null),
and so forth.
This is the only way I've found to do this.
Solution 3:
You can use a View that has no height for the "hidden" items so that you don't have to do all the model housekeeping and mapping. For example, suppose you had a "filter" EditText field that when data is entered it only keeps matching items:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflaterinflater= (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayoutview= (RelativeLayout) inflater.inflate(R.id.myListLayout, null, false);
...
// if we didn't match filter be GONE and leaveif (filterText.length() > 0 && myModelValueAtPosition.toLowerCase().indexOf(filterText) < 0){
view = (RelativeLayout) inflater.inflate(R.layout.myListLayoutWithZeroHeight, null, false);
view.setVisibility(View.GONE); // this doesn't really do anything useful; I'd hoped it would work by itself, but turns out the zero height layout is the keyreturn view;
}
view.setVisibility(View.VISIBLE);
...
}
Solution 4:
Here you need to write the logic in your getCount(),getItemId() and getItem(), It will create the no of rows what the getCount return
//How many items are in the data set represented by this AdapterpublicintgetCount() {
return//Should return the count of rows you need to display (here the count excluding null values)
}
And
//This need to return data item associated with the specified position in the data set.publicObjectgetItem(int position) {
return//Return the object need to display at position, need the logic to skip null value
}
Edit:So in your getview
public View getView(int position, View convertView, ViewGroup parent) {
----
getItem(position);//Object corresponding to position ,In your case it will not be null since you need to write the logic to skip null object at getItem
----
}
Solution 5:
This is the solution I implemented, here is a code example for everyone that is looking it:
classShipment_AdapterextendsArrayAdapter<Shipment>{
....
ArrayList<Integer> emptyPositions = newArrayList<>();
publicShipment_Adapter(Context context, int shipment_row, Shipment[] myShipments){
super(context, R.layout.shipment_row,myShipments);
//populate emptyPositions listfor(inti=0; i< myShipments.length; i++){
if(myShipments[i]==null){
emptyPositions.add(i);
}
}
this.mShipment = myShipments;
this.mContext = context;
}
//corrects size of List@OverridepublicintgetCount() {
return (mShipment.length - emptyPositions.size());
}
//recursive function that checks if position is not empty until it isn'tpublicintisEmpty(int positiontocheck){
int newposition;
if(emptyPositions.contains(positiontocheck)){
//true? check that next one is freereturn isEmpty(positiontocheck+1);
}else{
newposition = positiontocheck;
}
return newposition;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
//now just need to use isEmpty to get the next not empty position in //case our real position is empty
position= isEmpty(position);
Shipmentshipment= mShipment[position];
...//and so on
}
hope this helps!
Post a Comment for "Android Adaptor Row Hiding"