我正在使用自定义ButtonAdapter
构建视图,该视图将加载StringArray
并显示带有按钮的GridView
。直到按钮不在屏幕上,一切都可以正常加载,但是当我向下滚动时只能看到的最后几个按钮是错误的。
public class ButtonAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public ButtonAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.button, null);
Button button = (Button) gridView
.findViewById(R.id.button);
button.setText(mobileValues[position]);
Log.d("tag1", "value: " + mobileValues[position] +" position: "+ position);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v.findViewById(R.id.button);
String mobile = b.getText().toString();
Log.d("tag1", "button: " + mobile);
if (mobile.equals("Bäm 1")){
play(R.raw.bam1);
} else if (mobile.equals("Buz")){
play(R.raw.buz);
} else if (mobile.equals("Bäm 2")){
play(R.raw.bam2);
} else if (mobile.equals("Wir nehmen das Geld")){
play(R.raw.gold2);
}
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return mobileValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
目前,我传入一个包含26个项目的字符串数组。当要绘制第20个按钮时,position
会降为0
,并且按钮显示错误的文本。我该如何解决?
您的代码的问题是您没有为屏幕外的视图设置测试。正确的代码应该是这样的]
public class ButtonAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private View.OnClickListener onClListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v.findViewById(R.id.button);
String mobile = b.getText().toString();
Log.d("tag1", "button: " + mobile);
if (mobile.equals("Bäm 1")){
play(R.raw.bam1);
} else if (mobile.equals("Buz")){
play(R.raw.buz);
} else if (mobile.equals("Bäm 2")){
play(R.raw.bam2);
} else if (mobile.equals("Wir nehmen das Geld")){
play(R.raw.gold2);
}
}
};
public ButtonAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.button, parent, false);
}
Button button = (Button) convertView
.findViewById(R.id.button);
button.setText(mobileValues[position]);
button.setOnClickListener(onClListener);
return convertView;
}
@Override
public int getCount() {
return mobileValues != null ? mobileValues.length : 0;
}
@Override
public Object getItem(int position) {
return mobileValues[position];
}
@Override
public long getItemId(int position) {
return position;
}
}