Only prints the first element of the ArrayList

0

My problem right now is in printing the various elements that are inside an ArrayList that I created. The workings of my app are as follows:

  • I use a Cursor, and I fetch the information from the table to the database.
  • Then I pass in an ArrayList

So far so good, well save the information inside the ArrayList, the problem here is when it comes to the part where I print the elements, it just prints the first element to me.

I've used several solutions from the various questions of the same problem genre, but nothing solves it.

I leave below the parts of the code:

Fragment

    package cakeparty.cakeparty.Fragments;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;

import cakeparty.cakeparty.DB.MyDBHelper;
import cakeparty.cakeparty.Entidades.Bolo;
import cakeparty.cakeparty.Entidades.MyListAdapter;
import cakeparty.cakeparty.R;


public class to_Apresentar extends ListFragment {


    private Context context;
    private ArrayList<Bolo> arraylistBolo = new ArrayList<Bolo>();
    private MyListAdapter myAdapter = null;
    private String userLogado;
    private Cursor cursor_bolos;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = getActivity();
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        myAdapter = new MyListAdapter(context, arraylistBolo);
        setListAdapter(myAdapter);
    }


    public void guardarInformacao(String user) {
        this.userLogado = user;
    }


    @Override
    public void onResume() {
        super.onResume();

        MyDBHelper dbHelper = new MyDBHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();


        cursor_bolos = db.rawQuery("SELECT * FROM Bolo", null);


        arraylistBolo.clear();
        if (cursor_bolos != null && cursor_bolos.moveToFirst()) {


            do {
                      arraylistBolo.add(new Bolo(cursor_bolos.getString(1), cursor_bolos.getString(5), cursor_bolos.getString(3), cursor_bolos.getString(2), cursor_bolos.getInt(4), cursor_bolos.getInt(7), cursor_bolos.getString(6), cursor_bolos.getString(8)));
            } while (cursor_bolos.moveToNext());


        }


        myAdapter.notifyDataSetChanged();
        cursor_bolos.close();
        db.close();


    }

MyListAdapter

package cakeparty.cakeparty.Entidades;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import cakeparty.cakeparty.R;

/**
 * Created by JoseDias on 22/02/2017.
 */

public class MyListAdapter extends ArrayAdapter<Bolo> {

    private ArrayList<Bolo> resource;
    private Context context;

        public MyListAdapter(Context context, ArrayList<Bolo> resource) {
            super(context, R.layout.linhaof_owncakes, resource);

            this.resource = resource;
            this.context = context;
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.linhaof_owncakes,parent, false);

                TextView txtData = (TextView) v.findViewById(R.id.txtviewData);
                TextView txtEstado = (TextView) v.findViewById(R.id.txtviewestado);
                txtData.setText(resource.get(position).getDataEntrega());
                int ex = resource.get(position).getEstado();



                if(ex == 0) {
                    txtEstado.setText("Pendente");
                }else {
                    if(ex == 1) {
                        txtEstado.setText("Em preparação");
                    }
                }

            }

            return v;
        }
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_toLine"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="horizontal"

    >

        <TextView
            android:id="@+id/txtviewData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"/>

        <TextView
            android:id="@+id/txtviewestado"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="40dp"

            />



</LinearLayout>

IMPORTANT NOTE:

I've already used a layout like this, but it just prints the first element.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical">


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="lista vazia"

        />

</LinearLayout>

Thank you for your understanding

    
asked by anonymous 26.02.2017 / 04:12

1 answer

2

The problem is in the getView() method.

Notice that TextView is only filled in when convertView/v is null. This situation only happens once, when it's the first item.

Change to:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.linhaof_owncakes,parent, false);
    }

    TextView txtData = (TextView) v.findViewById(R.id.txtviewData);
    TextView txtEstado = (TextView) v.findViewById(R.id.txtviewestado);
    txtData.setText(resource.get(position).getDataEntrega());
    int ex = resource.get(position).getEstado();

    if(ex == 0) {
        txtEstado.setText("Pendente");
    }else {
        if(ex == 1) {
            txtEstado.setText("Em preparação");
        }
    }

    return v;
}
    
26.02.2017 / 11:25