Listview error with Arrayadapter and Custom View

Asked

Viewed 89 times

1

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

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

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

My IDE is the Android Studio. A Activity is the Inicial.java and View where 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;
    }
}

Java item.

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.

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

1 answer

1


On the line

convertView = LayoutInflater.from(this.context).inflate(R.layout.principal, parent, false);

You are inflating the XML for the layout called "main", which seems to correspond to an Activity. This is incorrect. Your Adapter has to inflate the views corresponding to the list items, that is, you have to inflate the layout defined in products_item.xml .

  • Thank you, really this passed me. But there is still another error. I fixed this and the app is still closing.

  • @Marcelonascimento I’m glad I helped a little. Open a new question with the new bug and the new situation to keep the organization and make it easier to search for other users.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.