Only print the first element of Arraylist

Asked

Viewed 223 times

0

My problem right now is to print the various elements that are inside an Arraylist I created. How my app works is as follows::

  • I use a Cursor, and I will fetch the table information from the database.
  • Then step inside an Arraylist

So far so good, keep the information well inside the Arraylist, the problem here is when the part where I print the elements arrives, just print me the first element.

I have used several solutions of the various questions of the same generic problem, but nothing solves.

I leave down 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 used a layout like this, but only print 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

1 answer

2


The problem is in the method getView().

Note that Textview is only filled in when the convertView/v is null. This situation only happens once, when it is of 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;
}
  • Thank you too much for the reply, but it still does not print the information, I think it now has to do with listfragment...

  • I edited the answer. The way I was just "printing" from the second item.

  • The way you edited the answer, you continue to print only the first element. In Fragment "toApresentar", it will be the best approach to use onCreateView() and assign a layout to Fragment or as I have now, with onActivityCreated()?

  • I don’t know if I understand, but the layout of Fragment(the one that has the list) should be "inflated" in the onCreateView()

  • Thanks for the answer! It helped me a lot to realize! : ) The problem was at the bottom of the xml layout.

Browser other questions tagged

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