【发布时间】:2014-11-25 10:07:25
【问题描述】:
搜索了最常见的类似主题,但没有任何帮助。
public class NewCustomAdapter extends BaseAdapter {
Context activityContext;
LayoutInflater myInflater;
Drawable[] myDrawableArray;
String[] myLabelArray;
String[] myABCArray;
int position = 0;
ImageView myImg;
TextView myLabelTextView;
TextView myABCTextView;
public NewCustomAdapter(Context context) {
// TODO Auto-generated constructor stub
this.activityContext = context;
myInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.e("getcount","true " + position);
return position;
}
@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;
}
public class Holder {
ImageView img;
TextView tv;
TextView abc;
public Holder(ImageView img, TextView tv, TextView abc) {
this.img = img;
this.tv = tv;
this.abc = abc;
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.e("getView","true");
if(convertView == null) {
convertView = myInflater.inflate(R.layout.customadapter_view_layout_medium, null);
myImg = (ImageView) convertView.findViewById(R.id.imageview_layout_normal_portrait_sizemedium);
myLabelTextView = (TextView) convertView.findViewById(R.id.textview_label_layout_normal_portrait_sizemedium);
myABCTextView = (TextView) convertView.findViewById(R.id.textview_abc_layout_normal_portrait_sizemedium);
convertView.setTag(new Holder(myImg,myLabelTextView, myABCTextView));
} else {
Holder holder = (Holder) convertView.getTag();
myImg = holder.img;
myLabelTextView = holder.tv;
myABCTextView = holder.abc;
}
myImg.setImageDrawable(myDrawableArray[position]);
myLabelTextView.setText(myLabelArray[position]);
myABCTextView.setText(myABCArray[position]);
return convertView;
}
public void updateGridView(Drawable[] myDrawableArray_, String[] myLabelArray_, String[] myABCArray_, int position_) {
this.myDrawableArray = myDrawableArray_;
this.myLabelArray = myLabelArray_;
this.myABCArray = myABCArray_;
this.position = position_;
this.notifyDataSetChanged();
}
}
这是一个普通的 BaseAdapter,它从我的 backgroundThread 获取更新。我的 BackgroundThread 调用
updateGridView(...)
方法。方法被按需调用。每次我调用 updateGridView 时都会调用 getCount .. 这样也可以。唯一的问题是。 GridView 不会膨胀。永远不会调用 getView。
在我的 AsyncTask 中,我在 UIThread(onPreExecute()) 上正确设置:
gv = (GridView) myActivityView.findViewById(R.id.gv1);
myCustomAdapter = new NewCustomAdapter(myActivityContext);
gv.setAdapter(myCustomAdapter);
没有错误,只是永远不会调用 getView,这就是为什么 gridview 永远不会被夸大的原因......感谢任何帮助。
【问题讨论】:
-
您的代码中有很多问题,我认为必须通过基本适配器的正确教程。当这里有任何列表项时调用 getView 没有列表传递给您的 CustomAdapter 的构造函数
-
@DanialHussain 听起来是好是坏。所以我的问题似乎是我传递了一个数组而不是一个列表?这就是为什么他从不调用 getView..
-
myCustomAdapter = new NewCustomAdapter(myActivityContext);有问题
-
查看给定的链接它们如何覆盖构造函数。 vogella.com/tutorials/AndroidListView/article.html
-
@DanialHussain 我知道这个教程,但他不使用简单的 BaseAdapter,他使用 ArrayAdapter。如果我有多个不同类型的数组,使用 ArrayAdapter 不是更好吗?
标签: android gridview baseadapter