How to ignore the first item in array adapter

别来无恙 提交于 2019-12-02 12:01:30

Return first element as usual but also set visibility to GONE or set Height to 0 in LayoutParams.

if(position==0)
convertView.setVisibility(View.GONE);
else
convertView.setVisibility(View.VISIBLE);

or

RelativeLayout.LayoutParams lp=null;
if(position==0)
lp=new RelativeLayout.LayoutParams(-1,0);
else
lp=new RelativeLayout.LayoutParams(-1,-1);

convertView.setLayoutParams(lp);

I think there is a better option ...

Add a field

private List<YOUR_TYPE> mDatas;
 // OR
private ArrayList<YOUR_TYPE> mDatas;

Then, in your constructor

public ListViewImgAdapter(Context c) {                                     
        mContext = c;                  
        mDatas = Broker.model.issueFilter.issueShowingList();
        if (mDatas > 0)
            mDatas.remove(0);
    }      

The simplest solution, and far the most proper one, is not to break adapter's logic. It's much better to control data that you pass to the adapter.

So in the code line where you pass data to your adapter, simple remove all rows that the adapter will not need.

List<T> myList = fetchMyList();
myList.remove(0);
myList.remove(11);
myList.remove(N);
MyAdapter myAdapter = new MyAdapter(this, R.layout.myLayout, myList);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!