我为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;
}
}
答案 0 :(得分:0)
length
为9.而pos_word.length
为5。
更改
if(counter < length)
到
if(counter < pos_word.length-1)