ArrayAdapter:ListView改变getView争论的位置值

时间:2014-10-09 16:58:12

标签: android listview android-listview android-arrayadapter

我在Android 4.4中开发,我对android开发相对较新。

这是我在listView中使用的自定义数组适配器:

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;

}

}

以下是我要做的事情:

我有3个静态数组(2个字符串和1个int),它们都包含16个元素。

对于listView中的每一行,我想显示前两个元素。例如:

ROW 1: teamName[1] teamLogo[1] teamRecord[1] and teamName[2] teamLogo[2] teamRecord[2]
ROW 2: teamName[3] teamLogo[3] teamRecord[3] and teamName[4] teamLogo[4] teamRecord[4]

依旧......

有没有办法可以在每次迭代后重新分配position的值?所以在第一次迭代后,position = position + 2

我希望这是有道理的。非常感谢您提供任何帮助。另外,如果我需要澄清任何内容,请告诉我。

-Patrick

1 个答案:

答案 0 :(得分:0)

您不应该在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 ;


    }

然后将arrayToAdapter传递给列表适配器