Через
ArrayAdapter вывожу названия объектов через массив
Foods из класса
Food. В чём ошибка?
Активность FoodCategoryActivitypackage dumpfiles.ru.coffeebuzz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FoodCategoryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listFood = (ListView) findViewById(R.id.list_foods);
ArrayAdapter<Food> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Food.foods);
listFood.setAdapter(listAdapter);
}
}
Макет activity_food_category<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_food_category"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FoodCategoryActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtFood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="TextView"
android:textColor="#00FF00"
android:textSize="24sp">
</TextView>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_foods" />
</LinearLayout>
Класс Foodpackage dumpfiles.ru.coffeebuzz;
public class Food {
private String name;
private String description;
private int imageResourced;
public static final Food[] foods = {
new Food("Pizza","Pizza is very tasty!", R.drawable.pizza),
new Food("Burger","Burger from Usa with love",R.drawable.burger),
new Food("Pasta","Italian bomb against sport",R.drawable.pasta)
};
private Food(String name, String description, int imageResourced) {
this.name = name;
this.description = description;
this.imageResourced = imageResourced;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public int getImageResourced() {
return imageResourced;
}
@Override
public String toString() {
return this.name;
}
}