从listview到我的自定义适配器的setAdapter编译器错误

时间:2017-03-11 03:12:50

标签: java android listview

我不确定这段代码有什么问题:

    mylistview = (ListView) findViewById(R.id.list);
    CustomAdapter adapter = new CustomAdapter(this, rowItems);
    mylistview.setAdapter(adapter);

    mylistview.setOnItemClickListener(this);

导致错误的行是mylistview.setAdapter(适配器)。 CustomAdapter是一个java类,其构造函数定义如下:

Context context;
List<RowItem> rowItems;

CustomAdapter(Context context, List<RowItem> rowItems){
    this.context = context;
    this.rowItems = rowItems;
}

我得到的错误是: &#39; setAdapter(android.widget.ListAdapter)&#39;在&#39; android.widget.ListView&#39;不能应用于&#39;(com.name.app.CustomAdapter)&#39;

我不知道导致此错误的是什么,并且非常感谢我为什么无法将自定义适配器的适配器设置为listview。谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个

public class SpinnerAdapter extends BaseAdapter{

Context context;

ArrayList<String> list;

LayoutInflater layoutInflater;

SpinnerAdapter(Context context, ArrayList<String> list)
{
    layoutInflater = LayoutInflater.from(context);

    this.list = list;

    this.context = context;
}


@Override
public int getCount()
{
    return list.size();
}

@Override
public Object getItem(int position)
{
    return list.get(position);
}

@Override
public long getItemId(int position)
{
    return list.indexOf(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    convertView = layoutInflater.inflate(R.layout.spinner_s,null);

    TextView client = (TextView) convertView.findViewById(R.id.client);

    client.setText(list.get(position).toString());

    return convertView;
}

答案 1 :(得分:0)

setAdapter需要android.widget.ListAdapter。看起来你的CustomAdapter类需要实现ListAdapter接口