Android ArrayAdapter:ListView更改位置的getView参数值
我是在安卓4.4中开发的,而且我对安卓开发相对来说还是个新手 以下是要在listView中使用的自定义阵列适配器:Android ArrayAdapter:ListView更改位置的getView参数值,android,listview,android-listview,android-arrayadapter,Android,Listview,Android Listview,Android Arrayadapter,我是在安卓4.4中开发的,而且我对安卓开发相对来说还是个新手 以下是要在listView中使用的自定义阵列适配器: class teamsAdapter extends ArrayAdapter<String> { Context context; String[] teamName; String[] teamRecord; int teamLogo[]; teamsAdapter(Context c, String[] teamName, int[] teamLogo,
class teamsAdapter extends ArrayAdapter<String> {
Context context;
String[] teamName;
String[] teamRecord;
int teamLogo[];
teamsAdapter(Context c, String[] teamName, int[] teamLogo,
String[] teamRecord) {
super(c, R.layout.activity_teams, R.id.teamList, teamName);
this.context = c;
this.teamName = teamName;
this.teamLogo = teamLogo;
this.teamRecord = teamRecord;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int teamLeft = position; // 1
int teamRight = position + 1; // 2
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.elements_teams, parent, false);
TextView teamNameLeft = (TextView) row.findViewById(R.id.teamNameLeft);
ImageView teamLogoLeft = (ImageView) row
.findViewById(R.id.teamLogoLeft);
TextView teamRecordLeft = (TextView) row
.findViewById(R.id.teamRecordLeft);
TextView teamNameRight = (TextView) row
.findViewById(R.id.teamNameRight);
ImageView teamLogoRight = (ImageView) row
.findViewById(R.id.teamLogoRight);
TextView teamRecordRight = (TextView) row
.findViewById(R.id.teamRecordRight);
teamNameLeft.setText(teamName[teamLeft]);
teamLogoLeft.setImageResource(teamLogo[teamLeft]);
teamRecordLeft.setText(teamRecord[teamLeft]);
teamNameRight.setText(teamName[teamRight]);
teamLogoRight.setImageResource(teamLogo[teamRight]);
teamRecordRight.setText(teamRecord[teamRight]);
return row;
}
}
等等
是否有一种方法可以在每次迭代后重新分配position
的值?那么在第一次迭代之后,position=position+2
我希望这是有道理的。提前非常感谢您提供的任何帮助。另外,如果我需要澄清什么,请告诉我
-Patrick您不应该在适配器内部的getView中编写此逻辑。首先处理数据,准备好显示,然后像
String arrayToAdapter [] = new String[8] ;
for(int i = 0,j=0 ; i < arr1.length ; i++,j++){
arrayToAdapter[j] = arr1[i]+ " " + arr2[i] + " " +arr3[i] +" and "+arr1[i+1]+ " " + arr2[i+1] + " " +arr3[i+1]
i+=2 ;
}
String arrayToAdapter[]=新字符串[8];
对于(int i=0,j=0;i
然后将arrayToAdapter传递给列表适配器是否有一种方法可以在每次迭代后重新分配位置值?-不,没有,位置参数由适配器提供。无论如何,您不想这样做,只需将接收到的位置映射到实际索引:
int realPos=position*2代码>。然后,您可以直接在数组中使用realPos
和realPos+1
。我认为您必须重新考虑将数据传递到适配器的方式。我最初尝试过@Luksprog,但一旦“position>8”,我就会收到arrayOutOfBounds错误。如何处理这个问题?如果你在超级构造函数中传递teamName
,那么适配器会认为你有16行(但是你想要8行,判断你每行使用2个数组项,对吗?)。相反,还要重写getCount()
以返回teamName.length/2
(或直接返回8)。@Luksprog谢谢!我做了一些与你的建议相似的事情,结果奏效了。谢谢你的帮助!
String arrayToAdapter [] = new String[8] ;
for(int i = 0,j=0 ; i < arr1.length ; i++,j++){
arrayToAdapter[j] = arr1[i]+ " " + arr2[i] + " " +arr3[i] +" and "+arr1[i+1]+ " " + arr2[i+1] + " " +arr3[i+1]
i+=2 ;
}