gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:14:35 25 4
gpt4 key购买 nike

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

不同的 .java 文件用于显示同一事物的不同类型,因此从共享父级继承,例如 basicrunner 扩展 runner 和 lollipoprunner 扩展 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 的父类。

是否有正确的多态性应用可以使其发挥作用?每个运行者都需要新的适配器吗?

最佳答案

BaseAdapter 有几个可以重写的方法,以便根据位置返回不同类型的 View 。

这些是:

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

编辑:

将数据提供给适配器时,您可以将其作为对象列表传递,稍后您可以根据您的类型进行向下转换。

@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

关于java - 一个适配器多种数据类型 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46281719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com