为一个customAdapter

时间:2017-03-06 03:14:51

标签: android custom-adapter

如何为customAdapter提供不同的样式?我的自定义适配器从它的getView方法获取它的布局视图。我想要做的是在从不同的活动调用适配器时给出不同的样式布局。我试图使用界面,但它显示错误

Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

这是我的getView(int position,View convertView,ViewGroup parent)方法中的代码:

if(convertView!=null){
    convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item1,parent,false);
}

实现界面后:

CustomAdapter

StylingView stylingView;
public interface StylingView{
    void styleView(View convertView,ViewGroup parent);
}

public View getView(int position, View convertView, ViewGroup parent){
    stylingView.styleView(convertView,parent);
}

FragmentSecondPage.class

public class FragmentSecondPage extends ListFragment implements CustomAdapter.StylingView{
    CustomAdapter customAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        customAdapter.styleView = this;
    }
    @Override
    public void styleView(View convertView,ViewGroup parent){
        if(convertView!=null){
             convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item1,parent,false);
        }
    }
}

有没有办法解决这个问题或其他任何方法?

1 个答案:

答案 0 :(得分:0)

您必须实施getViewTypeCount()& getItemViewType(int position)。看看下面的例子。

public class MultipleItemsList extends ListActivity {

private MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = new MyCustomAdapter();
    for (int i = 1; i < 50; i++) {
        mAdapter.addItem("item " + i);
        if (i % 4 == 0) {
            mAdapter.addSeparatorItem("separator " + i);
        }
    }
    setListAdapter(mAdapter);
}

private class MyCustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList mData = new ArrayList();
    private LayoutInflater mInflater;

    private TreeSet mSeparatorsSet = new TreeSet();

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item1, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item2, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }

}

public static class ViewHolder {
    public TextView textView;
}
}

这里,`getItemViewType()'是你自己的逻辑。您可以通过适配器的构造函数参数设置一个标志,以识别活动,并根据该标记,您可以编写逻辑来扩充相应的布局。