C
C
chelovek_rediska2017-02-26 02:33:28
Java
chelovek_rediska, 2017-02-26 02:33:28

Application crashes when populated dynamically via ArrayAdapter?

Through ArrayAdapter I display the names of objects through the Foods array from the Food class . Where is the mistake?
FoodCategoryActivity _

package 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 layout
<?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>

Food class
package 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;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2017-02-26
@Rou1997

Where is the mistake?

All errors - usually in LogCat!

T
tapokshot, 2017-02-26
@tapokshot

for simple_list_item_1, it seems that only an array of strings is suitable. For your case, you need to draw your own view and make your own adapter. And instead of ListView use recyclerview.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question