我有一个自定义Gridview来显示运营商列表及其图标和provider.my问题的名称是Gridview只显示有限提供商(仅限12个)。但我有超过16个值要显示。剩余价值在滚动GridView的同时在Gridview中重复。
我的源代码在这里
public class ImageAdapter extends BaseAdapter {
public int getCount() {
return providerlist1.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;
ImageView imageView;
if (convertView == null) {
LayoutInflater layoutInflater = getLayoutInflater();
v = layoutInflater.inflate(R.layout.icon_with_text, null);
TextView txt = (TextView) v.findViewById(R.id.icon_text);
txt.invalidate();
txt.setTypeface(localTypeface1);
Collections.sort(providerlist1);
Log.v("COLLECTIONS", ""+providerlist1);
txt.setText("" + providerlist1.toArray()[position]);
String string = (String) txt.getText();
icon = string.toLowerCase();
System.out.println(icon);
int resid = getResources().getIdentifier("" + icon, "drawable",
"com.digient.yeldi.app");
imageView = (ImageView) v.findViewById(R.id.icon_image);
imageView.setImageResource(resid);
} else {
v = convertView;
}
return v;
}
}
providerlist1是一个arraylist,其中有16个以上的运算符值。
答案 0 :(得分:0)
使用以下代码,可能对您有帮助。
public class ImageAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
Context ctx;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
layoutInflater=LayoutInflater.from(context);
this.ctx=context;
}
public int getCount() {
return providerlist1.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;
ImageView imageView;
if (convertView == null) {
v = layoutInflater.inflate(R.layout.icon_with_text, null);
} else {
v = convertView;
}
TextView txt = (TextView) v.findViewById(R.id.icon_text);
txt.invalidate();
txt.setTypeface(localTypeface1);
Collections.sort(providerlist1);
Log.v("COLLECTIONS", ""+providerlist1);
txt.setText("" + providerlist1.toArray()[position]);
String string = (String) txt.getText();
icon = string.toLowerCase();
System.out.println(icon);
int resid = getResources().getIdentifier("" + icon, "drawable", "com.digient.yeldi.app");
imageView = (ImageView) v.findViewById(R.id.icon_image);
imageView.setImageResource(resid);
return v;
}
}