setlistAdapter不工作

时间:2015-11-29 10:08:30

标签: android

我的适配器

public class FlowerAdapter extends ArrayAdapter<Flower> 
{

    private Context context;

    private List<Flower> flowerlist;

    public FlowerAdapter(Context context, int resource, List<Flower> objects){
        super(context,resource,objects);
        this.context=context;
        this.flowerlist=objects;


    }

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

       LayoutInflater inflater = (LayoutInflater)   

       context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


       View view= inflater.inflate(R.layout.items ,parent,false);

       Flower flower = flowerlist.get(position);

       TextView tv = (TextView) view.findViewById(R.id.text2);

       tv.setText(flower.getName());

        return view;
    }
}

在列表视图中显示

FlowerAdapter flowerAdapter = new FlowerAdapter(Getdata.this ,   R.layout.items,flowers);

//setListAdapter(flowerAdapter);

1 个答案:

答案 0 :(得分:0)

我认为您的活动类不是列表活动,可能是您的问题

确保您继承自ListActivity而不是Activity

public class YourActivity extends ListActivity {
 setListAdapter(flowerAdapter);
}

同时,如果您不想使用/ ListActivity扩展,则可以在活动中轻松添加ListView,并在不更改活动类继承的情况下将其设置如下。

listView.setAdapter(flowerAdapter);