一个适配器多种数据类型 - android

时间:2017-09-18 14:23:05

标签: java android polymorphism adapter

我的应用程序需要能够更改gridview中显示的drawable,主要是颜色和显示的一些信息。不同的样式有不同的.java和XML文件。

不同的.java文件用于显示同一事物的不同类型,因此从共享父项继承,例如basicrunner extends runner和lollipoprunner extends runner。

适配器扩展BaseAdapter,并覆盖getItem等方法,但它们指定一种返回类型;

public class RunnersAdapter extends BaseAdapter {
private static final String TAG = "RunnersAdapter";

//used in switch statement to decide which XML to inflate
private static final int LayoutTYPE_1 = 1;  
private static final int LayoutTYPE_2 = 2;
private static final int LayoutTYPE_3 = 3;
int layout = 2; //hard coded just for testing

@Override
public Runner getItem(int position) { //runner is superclass
    return mRunners.get(mKeys[position]);
}

有没有办法为每种类型的跑步者使用这个RunnersAdapter,或者我最好为每种类型制作不同的适配器?

编辑:

这是从只需要显示一种数据类型的适配器升级,即Runner。现在我们想要显示多种不同类型的跑步者,但不一定要制作多种类型的适配器。

更多代码来说明我的意思:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(layout == LayoutTYPE_1){ //LollipopView

            Runner runner = getItem(position); //should be a lollipopRunner!!

            gridView = inflater.inflate(R.layout.lollipopRunner, null); //use lollipopRunner XML
}else if(layout == LayoutTYPE_2){

            Runner runner = getItem(position);  //should be basicRunner!!

            gridView = inflater.inflate(R.layout.basicrunner, null); //use basic runner XML

如上所示,重写的getItem方法返回类型" Runner",它是BasicRunner和LollipopRunner的父类。

是否有正确的多态性应用可以实现这一点?我是否需要每个Runner的新适配器?

2 个答案:

答案 0 :(得分:1)

BaseAdapter有一些方法可以覆盖,以便根据位置返回不同类型的视图。

那些是:

        @Override
        public int getItemViewType(int position) {
            if (...) return 1;
            else if (...) return 2;
            else if (...) return 3;
            else return 4;
        }

        @Override
        public int getViewTypeCount() {
            return 4; //total number of different type of views 
        }


        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {


            switch (getItemViewType(position)) {
                case 0:
                    if (view == null) {
                        view = mActivity.getLayoutInflater().inflate(R.layout.message_item_1, viewGroup, false);
                    }
                    break;
                case 1:
                    if (view == null) {
                        view = mActivity.getLayoutInflater().inflate(R.layout.message_item_1_media, viewGroup, false);
                    }
                    break;
                case 2:
                    if (view == null) {
                        view = mActivity.getLayoutInflater().inflate(R.layout.message_item_2, viewGroup, false);
                    }
                    break;
                case 3:
                    if (view == null) {
                        view = mActivity.getLayoutInflater().inflate(R.layout.message_item_2_media, viewGroup, false);
                    }
                    break;
            }
    }

编辑:

在将数据提供给适配器的同时,您可以将其作为List of Object传递,稍后您可以根据类型向下传播。@Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(layout == LayoutTYPE_1){ //LollipopView // getItem(postion) will give you Object - downcast it as per the position. Runner runner = (LollipopRunner)(getItem(position)); //downcast to lollipopRunner gridView = inflater.inflate(R.layout.lollipopRunner, null); //use lollipopRunner XML }else if(layout == LayoutTYPE_2){ Runner runner = (BasicRunner)(getItem(position)); //downcasted to basic runner gridView = inflater.inflate(R.layout.basicrunner, null); //use basic runner XML

export const find = (query: string):string => `${apiUrl}find?q=${query}&APPID=${apiKey}`

答案 1 :(得分:0)

声明适配器时,可以尝试在适配器的构造函数部分中添加runners作为参数。然后getItem将返回跑步者的位置。