gpt4 book ai didi

java - 尝试设置自定义 BaseAdapter 时出现错误

转载 作者:行者123 更新时间:2023-12-01 12:43:11 26 4
gpt4 key购买 nike

我刚刚构建了一个扩展 BaseAdapter 的新类。我相信我已经正确设置了所有内容,但是,当我尝试在 fragment 内设置适配器时,我想使用它,但出现以下错误:

构造函数 HomeBase(Home) 未定义

HomeBase 是我构建的类的名称。

这是 BaseAdapter 的构造函数(我会添加整个类,但它相当长):

// constructor call
HomeBase(Context c){

context=c;

list = new ArrayList<SingleRow>();

Resources res = c.getResources();
String [] title; // temporary storage array for titles
title = res.getStringArray(R.id.title); // gets string array title and temporarily stores it in title[]

String [] invites; // temporary storage array for titles
invites = res.getStringArray(R.id.description); // gets string array invites and temporarily stores it in invites[]

int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img8,
R.drawable.img9, R.drawable.img10, R.drawable.img11}; // get images and store them into an array


// Loop to insert data from the different arrays into their respective single row objects, and those objects into the ArrayList list
for(int i=0; i<10; i++)
{

list.add(new SingleRow (title[i], invites[i], images[i] ));

}

}

这是用于填充 ArrayList 列表的对象

公共(public)类 SingleRow {

String title;
String description;
int image;

SingleRow(String title, String description, int image){

this.title=title;
this.description=description;
this.image=image;

}

}

aa以下是 fragment 中将使用 BaseAdapter 的 onCreate() 方法:

@Override
public void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
list=(ListView)getView().findViewById(R.id.list_view);
list.setAdapter(new HomeBase(this));
Log.v(HOME, "onCreate() was called");

}

我现在正在学习 Android 的所有知识,如果有任何有用的建议,我将不胜感激。

谢谢

詹姆斯

最佳答案

如果您放置适配器的部分继承自 ListFragment

@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// Initially there is no data
setEmptyText("No Data Here");

// Create an empty adapter we will use to display the loaded data.
mAdapter = new CustomArrayAdapter(getActivity());
setListAdapter(mAdapter);
}

或者,如果在 Fragment 中,请按如下方式移动代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.list_view, container, false);

list=(ListView)view.findViewById(R.id.list_view);
list.setAdapter(new HomeBase(getActivity()));
Log.v(HOME, "onCreate() was called");

return view;
}

关于java - 尝试设置自定义 BaseAdapter 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24899420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com