动态定义项目时如何创建自定义数组适配器
How to create custom array adapter when items defined dynamically
我有一个自定义适配器,它应该包含图像按钮。但是,我对 getView() 方法的重写的实现有点困惑。由于我的图像按钮是动态定义的,因此我可以使用代码
恢复图像按钮
@Override
public View getView(int i, View view, ViewGroup viewGroup){
ImageButton ibutton = (ImageButton) getItem(i);
我如何return查看它?我没有专门为它创建一个 xml 文件,因为它只是一个 ImageButton(没有与其他任何东西结合),但是是否有必要为它创建一个 xml?或者有没有一种方法可以轻松地从图像按钮本身获取视图。
当我为 getView() 尝试此操作时,图像按钮由于某种原因不可点击。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton = getItem(position);
return imageButton ;
}
尝试像这样构建适配器:
public class ImageButtonAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageButtonAdapter(Context c) {
mContext = c;
}
public int getCount() {
return listCount;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageButton for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton ;
if (convertView == null) {
imageButton = new ImageButton (mContext);
imageButton.setLayoutParams(lp);
}
else
{
imageButton = (ImageButton ) convertView;
}
imageButton.setBackgroundColor(Color.TRANSPARENT)
return imageButton ;
}
}
我有一个自定义适配器,它应该包含图像按钮。但是,我对 getView() 方法的重写的实现有点困惑。由于我的图像按钮是动态定义的,因此我可以使用代码
恢复图像按钮@Override
public View getView(int i, View view, ViewGroup viewGroup){
ImageButton ibutton = (ImageButton) getItem(i);
我如何return查看它?我没有专门为它创建一个 xml 文件,因为它只是一个 ImageButton(没有与其他任何东西结合),但是是否有必要为它创建一个 xml?或者有没有一种方法可以轻松地从图像按钮本身获取视图。
当我为 getView() 尝试此操作时,图像按钮由于某种原因不可点击。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton = getItem(position);
return imageButton ;
}
尝试像这样构建适配器:
public class ImageButtonAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageButtonAdapter(Context c) {
mContext = c;
}
public int getCount() {
return listCount;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageButton for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton ;
if (convertView == null) {
imageButton = new ImageButton (mContext);
imageButton.setLayoutParams(lp);
}
else
{
imageButton = (ImageButton ) convertView;
}
imageButton.setBackgroundColor(Color.TRANSPARENT)
return imageButton ;
}
}