ListView displays only one line, why?

1

I have a ListView in a layout with more items, and it only displays one row. If I make the same schema in a code that the layout has only the ListView it works fine, if anyone has any idea why I would appreciate it.

Here is my xml

<?xml version="1.0" encoding="utf-8"?>

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#fff">

        <LinearLayout
            android:padding="5dp"
            android:background="#E3F2FD"
            android:elevation="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:orientation="vertical">

            <ImageView
                android:layout_marginTop="3dp"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/piscina"
                android:layout_width="match_parent"
                android:layout_height="150dp" />

            <TextView
                android:layout_marginTop="5dp"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:text="Nome do cliente"
                android:id="@+id/visual_nome"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_gravity="center"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="[email protected]"
                android:id="@+id/visual_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

                <TextView
                    android:layout_gravity="center"
                    android:textSize="15sp"
                    android:layout_marginTop="5dp"
                    android:text="(47)33823449"
                    android:id="@+id/visual_telefone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_gravity="center"
                    android:textSize="15sp"
                    android:layout_marginTop="5dp"
                    android:text="(47)88565576"
                    android:id="@+id/visual_celular"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_gravity="center"
                    android:textSize="15sp"
                    android:layout_marginTop="5dp"
                    android:text="Rua tatata 178"
                    android:id="@+id/visual_rua"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_gravity="center"
                    android:textSize="15sp"
                    android:layout_marginTop="5dp"
                    android:text="Centro"
                    android:id="@+id/visual_bairro"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:layout_gravity="center"
                    android:textSize="15sp"
                    android:layout_margin="5dp"
                    android:text="Terça e Sexta"
                    android:id="@+id/visual_dia"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout
            android:background="#2196F3"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginLeft="3dp"
            android:elevation="6dp"
            android:padding="1dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:textColor="#fff"
                android:padding="3dp"
                android:textSize="18sp"
                android:layout_gravity="center"
                android:text="Produtos do Mês"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

            <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >

            <ListView
                android:verticalScrollbarPosition="right"
                android:id="@+id/lista_produtos"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </ListView>
            </ScrollView>

    </LinearLayout>

</ScrollView>

Here is the code for my adapter

public class VisualClienteAdapter extends BaseAdapter {

    private ClientesDAO pDao;

    private TextView valor;
    private TextView qtd;
    private TextView nome;

    private LinkedList<Produto> data = new LinkedList<Produto>();
    private Context context;

    public VisualClienteAdapter(Context context){
        this.context = context;

        data.add(new Produto(R.mipmap.ic_launcher,"Algicida de Choque","20,00",1));
        data.add(new Produto(R.mipmap.ic_launcher,"Cloro Granulado 10Kg","220,00",1));
        data.add(new Produto(R.mipmap.ic_launcher,"Barrilha Leve 2Kg","20,00",1));
        data.add(new Produto(R.mipmap.ic_launcher,"PH Estavel 1,5Kg","20,00",1));
        data.add(new Produto(R.mipmap.ic_launcher,"Clarificante","20,00",1));
        data.add(new Produto(R.mipmap.ic_launcher,"Reagente Cloro","7,50",1));
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Produto getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_produtos,null,false);

        ImageView image = (ImageView) view.findViewById(R.id.imagemLista);
        TextView nome = (TextView) view.findViewById(R.id.id_produto_nome);
        TextView qtd = (TextView) view.findViewById(R.id.id_qtd);
        TextView valor = (TextView) view.findViewById(R.id.produto_valor);

        Produto p = getItem(position);

        image.setImageResource(p.getId());
        nome.setText(p.getNome());
        qtd.setText(String.valueOf(p.getQdt()));
        valor.setText(p.getValor());

        return view;
    }
}

Here is my Activity.class for .xml

package br.com.piscinas.piscinas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import br.com.piscinas.piscinas.adapter.VisualClienteAdapter;
import br.com.piscinas.piscinas.dao.ClientesDAO;

    public class VisualizarCliente extends AppCompatActivity implements view.OnClickListener{

    private TextView nome;
    private TextView email;
    private TextView rua;
    private TextView tel;
    private TextView cel;
    private TextView bairro;
    private TextView diaSemana;
    private ClientesDAO hp;
    private VisualClienteAdapter adapter;
    private ListView list;

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

        adapter = new VisualClienteAdapter(this);
        list = (ListView) findViewById(R.id.lista_produtos);
        list.setAdapter(adapter);


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setElevation(6f);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2196F3")));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (item.getItemId() == android.R.id.home) {

            finish();
            return true;

        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        }
}

Here the items that are "inflated" in each line of the list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="20dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#fff">


    <ImageView
        android:id="@+id/imagemLista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:minWidth="30dp"
        android:id="@+id/id_produto_nome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_marginLeft="10dp"
        android:text="R$: "
        android:id="@+id/id_valor_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView

        android:id="@+id/produto_valor"
        android:minWidth="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/id_qtd_text"
        android:layout_marginLeft="10dp"
        android:text="Qtd: "
        android:minWidth="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView

        android:id="@+id/id_qtd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
    
asked by anonymous 30.07.2015 / 22:58

1 answer

1

It's because you're using a listview inside a scrollview. The listview already has a scrollview internally, so in practice it has a lot of scroll inside the other. I recommend redoing the layout, but if you really need to keep it up, there is a workaround to recalculate the size of the list.

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}

Font

    
23.02.2016 / 21:40