Listview displays only one line, why?

Asked

Viewed 652 times

1

I have a Listview in a layout with more items, and it only displays one line. If I do the same scheme in a code that the layout has only Listview it works perfectly, if anyone has any idea why I thank you.

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’s the code to 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 row 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>
  • Please put the list items XML

  • This is the . xml of the items that are inflated in the individual list field

  • The only thing I see wrong is the way inflate of view in the method getView() change the line View view = LayoutInflater.from(context).inflate(R.layout.item_produtos,null,false); for View view = LayoutInflater.from(context).inflate(R.layout.item_produtos,parent,false);. Instead of nullpass parent to the method inflate()

  • OK I’ll make the change

  • Nothing, buddy, stays the same. I have tried a different method, I searched the items in the list of my database I ordered asc and go down and it displayed the first and last information in each case, I’m beginner really have no idea what to do ,I researched but nobody posted anything similar so far , at least not that I’ve seen.

  • 1

    When you say it displays only one line it means that the list has only one item or it means it is only the height of one item?

  • It’s not just a line, I put a Scrollview ,I do not know if this serves as a test for this case but even with it working continued displaying only one line (an item)

  • I couldn’t tell if the list had all the items or not. If you have all the items but her height is one line, one possible cause would be that you are actually using the Listview within a Scrollview.

  • Another thing I notice is that either did not fully post their xml or missing include them within some kind of Layout(Linearlayout, Relativelayout, etc)

  • Yes I noticed that at the time of posting they disappeared, but at last, that’s what you said, I removed the Scrollview and the list was displayed. Only then how should I do to be able to view the full list, it is at the end of my precise Scrollview layout, but with it is only one line, as I should do?

  • I ran the app without scrollview, it adds a scrollview automatically in the layout that is my list, but as it is close to the screen limit is too small for the user to handle, you have idea what I should do?

  • Group the information by context and present each group on, for example, tabs.

  • Well, that’s beyond my knowledge, I’ll start researching. Thanks for your help.

  • I’m a little lost on what to look for, could you point me to a topic?

  • @Cesarrobertomartins, the ramaral refers to these tabs http://answall.com/a/76497/10315

  • I went through the same problem. When I searched google in English for the answer here: Change the layout of the item android:layout_height="match_parent" for wrap_content https://stackoverflow.com/questions/36819329/recyclerview-shows-just-a-single-row-in-android/36819884#36819884

Show 11 more comments

1 answer

1

It’s because you’re using a listview inside a scrollview. 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 that way, there’s a workaround to recalculate the list size.

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));
    }
}

Source

Browser other questions tagged

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