Hiding Android adapter string

In arrayAdptor we use the following code:

 final LayoutInflater inflater = activity.getLayoutInflater();
    row = (LinearLayoutCustom) inflater.inflate(R.layout.row, null);
    final TextView label = (TextView) row.findViewById(R.id.title);
    label.setText(position + "" + items[position]); 
    return row;

      

Now, suppose some value is null (for example, at position 2, items [2] = null), so I don't want to show it on the line. I want to hide it. if i use

               row.setVisibility(View.GONE) 

      

it leaves a blank space on that line that I don't want. so what should i do?

+1


source to share


5 answers


You need the adapter to return the total number of non-zero elements with getCount

, and then store the position mapping in your internal data structure.

For example. Do you have a list

1 - John
2 - null
3 - Bill
4 - Susan
5 - null

      



When getCount is called, it returns 3.

Then, when getView

called at position 1, you return the item at list[1]

. getView

at position 2 returns list[3]

(as it is the second not zero), etc.

This is the only way I have found it.

+2


source


AFAIK you cannot return a null view from getView

, but you can just make the view invisible and the height 1. Though manipulating with getCount

is probably the preferred way.



view.setVisibility(View.INVISIBLE);
view.getLayoutParams().height = 1;

      

+8


source


You can use a View that has no height for "hidden" elements, so you don't have to do all of the modeling and mapping of the model. For example, suppose you have a filter "EditText" field that, when you enter data, contains only matching elements:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater   inflater = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout view = (RelativeLayout) inflater.inflate(R.id.myListLayout, null, false);

    ...

    // if we didn't match filter be GONE and leave
    if (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 key
        return view;
    }
    view.setVisibility(View.VISIBLE);

    ...
}

      

+1


source


Here you need to write logic in getCount () , getItemId () and getItem () .
This will create no of rows, which is what getCount returns

//How many items are in the data set represented by this Adapter
    public int getCount() {
            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.
public Object getItem(int position) {
        return //Return the object need to display at position, need the logic to skip null value  
    }

      

Edit: So, in your browser

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

      

0


source


This is the solution I have implemented, here is some sample code for anyone looking for it:

class Shipment_Adapter extends ArrayAdapter<Shipment>{
     ....
     ArrayList<Integer> emptyPositions = new ArrayList<>();

     public Shipment_Adapter(Context context, int shipment_row, Shipment[] myShipments){
        super(context, R.layout.shipment_row,myShipments);

        //populate emptyPositions list
        for(int i = 0; i< myShipments.length; i++){
            if(myShipments[i]==null){
                emptyPositions.add(i);
            }
        }

        this.mShipment = myShipments;
        this.mContext = context;
    }

    //corrects size of List
    @Override
    public int getCount() {
        return (mShipment.length - emptyPositions.size());
    }

    //recursive function that checks if position is not empty until it isn't
    public int isEmpty(int positiontocheck){
         int newposition;
         if(emptyPositions.contains(positiontocheck)){
             //true? check that next one is free
            return 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);

    Shipment shipment = mShipment[position];

    ...//and so on

 }

      

hope this helps!

0


source







All Articles