My app that has ListView with ArrayAdapter is closing when creating the list

1

I want to make a product listing, where each line contains more than one information per line.

The problem is that the app throws an exception (I believe) and closes when I click the button that would populate the list.

I made a custom View for each ListView item that is produtos_item.xml

My IDE is Android Studio . Activity is Inicial.java and View where the ListView is principal.xml .

main.xml (Activity)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Formar lista"
        android:onClick="FormarLista" />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Principal__ListaProdutos"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

products_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/produtos_item__Nome"
        android:textColor="@android:color/background_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nome do produto"
        android:textStyle="bold"
        android:textSize="17dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false">
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/clientes_itens_Quantidade"
                android:textSize="15dp" />
            <TextView
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" " />
            <TextView
                android:id="@+id/produtos_item__Quantidade"
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="150"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/clientes_itens_Preco"
                android:textSize="15dp" />
            <TextView
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" R$ "
                android:textSize="15dp" />
            <TextView
                android:id="@+id/produtos_item__Valor"
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="5.50"
                android:textSize="15dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/clientes_itens_Estoque"
                android:textSize="15dp" />
            <TextView
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" R$ "
                android:textSize="15dp" />
            <TextView
                android:id="@+id/produtos_item__Total"
                android:textColor="@android:color/background_dark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="825.00"
                android:textSize="15dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

ListAdapterList.java

public class ListAdapterList extends ArrayAdapter<Item> {

    private Context context;
    private ArrayList<Item> itens;
    private int layoutResourceId;

    public ListAdapterList(Context context, int layoutResourceId, ArrayList<Item> itens){
        super(context, layoutResourceId, itens);
        this.context = context;
        this.itens = itens;
        this.layoutResourceId = layoutResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Item item = this.itens.get(position);

        convertView = LayoutInflater.from(this.context).inflate(layoutResourceId, parent, false);
        ((TextView) convertView.findViewById(R.id.produtos_item__Nome)).setText(item.getNome());
        ((TextView) convertView.findViewById(R.id.produtos_item__Quantidade)).setText(item.getQuantidade());
        ((TextView) convertView.findViewById(R.id.produtos_item__Valor)).setText(String.valueOf(item.getValor()));
        ((TextView) convertView.findViewById(R.id.produtos_item__Total)).setText(String.valueOf(item.getValorEstoque()));

        return convertView;
    }
}

Item.java

public class Item {
    private String Nome;
    private double Valor;
    private int Quantidade;

    public Item(String nome, double valor, int quantidade) {
        Nome = nome;
        Valor = valor;
        Quantidade = quantidade;
    }
    public String getNome() { return Nome; }
    public double getValor() { return Valor; }
    public int getQuantidade() { return Quantidade; }
    public double getValorEstoque() { return Quantidade * Valor; }
}

Initial.java

public class Inicial extends Activity{

    private ListView Lista;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.principal);
        Lista = (ListView)findViewById(R.id.Principal__ListaProdutos);
    }
    public void FormarLista(View v){
        ArrayList<Item> objects = new ArrayList<>();
        objects.add(new Item("Produto 1", 5.6, 18));
        objects.add(new Item("Produto 2", 16.9, 12));
        objects.add(new Item("Produto 3", 1.2, 200));

        ListAdapterList customAdapter = new ListAdapterList(this, R.layout.produtos_item, objects);
        Lista.setAdapter(customAdapter);
    }
}

Error Console

E/ArrayAdapter: You must supply a resource ID for a TextView
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
    
asked by anonymous 07.07.2016 / 05:05

1 answer

1

You are using an ArrayAdapter, it by default expects a TextView as layout.

  • You can change your call from super to super(context, layoutResourceId); should resolve

  • You can extend from a BaseAdapter

  • You can use RecyclerView (more recommended) Here's a example
07.07.2016 / 15:50