Home > Software engineering >  ListView seems to be working fine but in the View nothing appears
ListView seems to be working fine but in the View nothing appears

Time:12-02

I've been doing a ListView, I've checked many times with the debugger, and everythings seems to be working correctly, but I dont know why, it doesnt show up and I cant see the content of the ListView. Here is my MainActivity (it uses some other classes but they are all alright) Please, dont worry about the variables names, cuz they are in Spanish, and dont worry about the commented lines, I've commented them just for testing the error that I've talked about

public class MainActivity extends AppCompatActivity {
ListView lV;
private List<Object> empresas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lV = findViewById(R.id.lista);

    empresas.add(new EmpresaTecnologica("Company S.A.", R.drawable.emptec1, "https://company.com", "Aquí", "mail123"));
    empresas.add(new EmpresaTecnologica("Adidas", R.drawable.empnotec1, "https://company.com", "Aquí", "mail123"));
    empresas.add(new EmpresaTecnologica("Amazon", R.drawable.emptec2, "https://company.com", "Aquí", "mail123"));
    empresas.add(new EmpresaTecnologica("CocaCola", R.drawable.empnotec2, "https://company.com", "Aquí", "mail123"));

    ListaDinamica a = new ListaDinamica(this);
    lV.setAdapter(null);
    //TODO listener del click
}

class ListaDinamica extends BaseAdapter {
    Context c;

    public ListaDinamica(Context c) {
        super();
        this.c = c;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof EmpresaTecnologica) {
            return 0; //es empresa tecnologica
        } else {
            return 1; //es empresa no tecnologica
        }
    }

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

    @Override
    public Object getItem(int position) {
        return empresas.get(position);
    }

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

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


        View row = convertView;
        ViewHolder holder;
        ImageView img = null;
        TextView txtNombreEmpresa = null;
        TextView txtLocalizacion = null;
        TextView txtMail = null;
        TextView txtCodigoCnae;

        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            if (getItemViewType(position) == 0) { // es empresa tecnologica
                row = inflater.inflate(R.layout.emp_tecnologicas, parent, false);
                img = row.findViewById(R.id.logo);
                txtLocalizacion = row.findViewById(R.id.localizacion);
                txtMail = row.findViewById(R.id.mail);
                txtNombreEmpresa = row.findViewById(R.id.nombreEmpresa); //TODO mejorar esto para no repetir codigo

            }/*else{ // es empresa no tecnologica
                row=inflater.inflate(R.layout.emp_no_tecnologicas, parent, false);
                txtCodigoCnae = row.findViewById(R.id.codigoCnae);
                txtNombreEmpresa = row.findViewById(R.id.nombreEmpresa); //TODO mejorar esto para no repetir codigo
                holder = new ViewHolder(txtNombreEmpresa, txtCodigoCnae);
            }*/
                holder = new ViewHolder(img, txtNombreEmpresa, txtLocalizacion, txtMail);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }

            if (getItemViewType(position) == 0) {
                holder.getTxtNombreEmpresa().setText(((EmpresaTecnologica) empresas.get(position)).getNombreEmpresa());
                holder.getImg().setImageResource(((EmpresaTecnologica) empresas.get(position)).getIdLogo());
                holder.getTxtLocalizacion().setText(((EmpresaTecnologica) empresas.get(position)).getLocalizacion());
                holder.getTxtMail().setText(((EmpresaTecnologica) empresas.get(position)).getMail());
            } /*else {
                holder.getTxtNombreEmpresa().setText(((EmpresaNoTecnologica) empresas.get(position)).getNombreEmpresa());
                holder.getTxtCodigoCnae().setText(((EmpresaNoTecnologica) empresas.get(position)).getCodigoCnae());
            }*/

            return row;
        }
    }

    class ViewHolder {
        ImageView img;
        TextView txtNombreEmpresa;
        TextView txtLocalizacion;
        TextView txtMail;
        TextView txtCodigoCnae;

        public ViewHolder(ImageView img, TextView txtNombreEmpresa, TextView txtLocalizacion, TextView txtMail) {
            this.img = img;
            this.txtNombreEmpresa = txtNombreEmpresa;
            this.txtLocalizacion = txtLocalizacion;
            this.txtMail = txtMail;
        }

        public ViewHolder(TextView txtNombreEmpresa, TextView txtCodigoCnae) {
            this.txtNombreEmpresa = txtNombreEmpresa;
            this.txtCodigoCnae = txtCodigoCnae;
        }

        public ImageView getImg() {
            return img;
        }

        public TextView getTxtNombreEmpresa() {
            return txtNombreEmpresa;
        }

        public TextView getTxtLocalizacion() {
            return txtLocalizacion;
        }

        public TextView getTxtMail() {
            return txtMail;
        }

        public TextView getTxtCodigoCnae() {
            return txtCodigoCnae;
        }
    }
}

This is my layout: activity_main.xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:layout_weight="2"
    android:id="@ id/lista"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is my other layout: emp_tecnologicas.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@ id/logo"
    app:layout_constraintLeft_toRightOf="parent" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@ id/nombreEmpresa"
    app:layout_constraintLeft_toRightOf="@id/logo" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@ id/localizacion"
    app:layout_constraintLeft_toRightOf="@id/nombreEmpresa" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@ id/mail"
    app:layout_constraintLeft_toRightOf="@id/localizacion" />

</androidx.constraintlayout.widget.ConstraintLayout>

And here is my last laout: emp_no_tecnologicas.xml:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@ id/nombreEmpresa"
    app:layout_constraintLeft_toRightOf="parent" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@ id/codigoCnae"
    app:layout_constraintLeft_toRightOf="@id/nombreEmpresa" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is how the App looks like: App View

You can see those grey horizontal line, which means that the ListView shows up, but without content

Thx so much for ur time

CodePudding user response:

This is what your layout emp_no_tecnologicas.xml looks like, as you can see, your TextView are not on the layout.

You can always see the preview in Android Studio, in Design (on left panel)

enter image description here

Do something like:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@ id/nombreEmpresa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="nombreEmpresa"
        app:layout_constraintEnd_toStartOf="@id/codigoCnae"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/codigoCnae"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="codigoCnae"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toRightOf="@id/nombreEmpresa"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This will give you something like that (adapt it, if it's not want you want)

enter image description here

Do the same correction for the emp_tecnologicas.xml (ask me if you have trouble doing this^^)

CodePudding user response:

I think that you have not added the arraylist while setting the adapter of the listview.

So, in the class ListaDinamica :

class ListaDinamica extends BaseAdapter {
    Context c;
    List<Object> empresa;

    public ListaDinamica(Context c, List<Object> empresa) {
        super();
        this.c = c;
        this.empresa = empresa;
    }
}

And in the .MainActivity code :

ListaDinamica a = new ListaDinamica(this, empresa);
lV.setAdapter(a);

Please inform me if I am understanding your question incorrectly or my answer has a mistake. Thank you.

  • Related