尝试设置自定义BaseAdapter时出错

时间:2014-07-22 23:18:47

标签: java android adt baseadapter

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

构造函数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列表的对象

公共类SingleRow {

String title;
String description;
int image;

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

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

}

}

aaand这是来自将要使用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的所有内容,并希望获得任何有用的建议。

谢谢,

詹姆斯

1 个答案:

答案 0 :(得分:1)

如果您放置适配器的部分继承自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;
 }
相关问题