当我尝试实例化时,为什么我的自定义适配器无法正常工作?

时间:2017-01-03 06:33:14

标签: java android instantiation

我一直试图让这个自定义适配器工作,但由于某种原因它没有。我尝试创建名为ItemAdapter的新适配器,但它表示当鼠标悬停在“ItemAdapter”上时无法解析该符号。它表示永远不会使用ItemAdapter构造函数,即使我尝试调用构造函数。有什么想法吗?

FoodDrinks

    public class FoodDrinks extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewitem);

        //create new array list to hold food and drink items
        ArrayList<NewItem> obj = new ArrayList<>();

        //add new items to array
        obj.add(new NewItem("Name", "Description", R.mipmap.ic_launcher));
        obj.add(new NewItem("Name", "Description", R.mipmap.ic_launcher));
        obj.add(new NewItem("Name", "Description", R.mipmap.ic_launcher));
        obj.add(new NewItem("Name", "Description", R.mipmap.ic_launcher));
        obj.add(new NewItem("Name", "Description", R.mipmap.ic_launcher));
        ItemAdapter adapter = new ItemAdapter(this, obj, R.mipmap.ic_launcher);
        ListView listview = (ListView) findViewById(R.id.l1);
        listview.setAdapter(adapter);
    }
}

ItemAdapter

public class ItemAdapter extends ArrayAdapter<NewItem> {
private int mImageResource;
private ArrayList<NewItem> obj;

    public ItemAdapter(Context context, ArrayList<NewItem> obj, int imageResource) {
        super(context, 0, obj);
        mImageResource = imageResource;
        this.obj = obj;

    }

0 个答案:

没有答案