android中的baseAdapter和膨胀不同的布局

时间:2014-12-24 10:18:06

标签: android

我为GridView编写了一个简单的程序,我对Counter变量有疑问。

当我使用pos_word[counter]时它不起作用,但当我在其中使用数字时,它的作用。像pos_word[1] 我想写一个适配器来扩充gridView中的不同布局。

你能帮忙搞清楚吗

package com.example.crossword;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

public class CrossWord_Adapter extends BaseAdapter implements ListAdapter {

    LayoutInflater inflater;
    public int length = 9;
    public int[] pos_word = {0,3,4,5,6};
    public int counter = 0;

    public CrossWord_Adapter(Context context) //difination -------|
    {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        holder layoutHolder;

        if(position == pos_word[counter])
            {
                convertView = inflater.inflate(R.layout.layout_word, null);
                layoutHolder = new holder();
                layoutHolder.word = (TextView) convertView.findViewById(R.id.text_word);
                convertView.setTag(layoutHolder);
                if(counter < pos_word.length)
                {
                    counter++;
                }
            }
        else
            {
                convertView = inflater.inflate(R.layout.layout_hashur, null);
            }   

        return convertView;
    }

    class holder
    {
        TextView word;
    }

}

1 个答案:

答案 0 :(得分:0)

length为9.而pos_word.length为5。

更改

if(counter < length)

if(counter < pos_word.length-1)