Listview does not present first item on android

Asked

Viewed 40 times

0

Hi, how are you? i have a problem using android listview, I realize that it shows only from the second item, and in case of only one item in the list it shows nothing.

I’ll show you my codes

Oncreate from Mainactivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gridView = (ListView) findViewById(R.id.gridview_listaCategoria);

    DataBaseHelper myDbHelper;
    myDbHelper = new DataBaseHelper(this);
    DataBaseManager.initializeInsyance(myDbHelper);

    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        myDbHelper.openDataBase();

    }catch(SQLException sqle){

        throw sqle;

    }
    CategoriaSQL categoriaSQL = new CategoriaSQL();
    arrayAdapter = categoriaSQL.getAlllitas(this);
    gridView.setAdapter(arrayAdapter);
    gridView.setOnItemClickListener(this);
}

to display this I have 2 layouts: 1) activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="br.com.dorgamnetwork.calculadora.MainActivity">
    <include layout="@layout/lista_categoria" />
</RelativeLayout>

and the.xml category listing

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gridview_listaCategoria"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</LinearLayout>

Categoriasql

public class CategoriaSQL {
    private String [] AllColumns ={DataBase.TABLE_CATEGORIA_ID, DataBase.TABLE_CATEGORIA_NOME,DataBase.TABLE_CATEGORIA_DESCRICAO,DataBase.TABLE_CATEGORIA_IMAGEM};

public ArrayAdapter<Categoria> getAlllitas(Context context){
    SQLiteDatabase database = DataBaseManager.getInstance().openDatabase();
    ArrayAdapter<Categoria> categorias = new ArrayAdapter<Categoria>(context,android.R.layout.simple_list_item_1);
    Cursor cursor = database.query(DataBase.TABLE_CATEGORIA,AllColumns,null,null,null,null,null,null);
    cursor.moveToFirst();
    while (cursor.moveToNext()){
        Categoria categoria = cursorToLista(cursor);
        categorias.add(categoria);

    }
    cursor.close();
    DataBaseManager.getInstance().closeDatabase();
    return categorias;
}

private Categoria cursorToLista(Cursor cursor) {
    Categoria lista = new Categoria();
    lista.setId(cursor.getLong(0));
    lista.setNome(cursor.getString(1));
    lista.setDescricao(cursor.getString(2));
    lista.setImagem(cursor.getString(3));
    return lista;
}

}

  • I’ve had a similar problem, try to check if the item is not under Toolbar, you can make a test by taking Toolbar from the application.

  • @Danilo D if you can, include the code of the class Categoriasql?

  • @Marcogiovanni I did the removal but she still does not show item 1

  • @Rodrigopaixão made the addition of class

  • 1

    The problem is reading the cursor. You point to the first position and when you enter the while you ta calling the next command that takes the second element of the cursor.

  • 1

    Try to use the while. That should solve.

  • Yes, I saw that mistake, very grateful for the tip, I used a while.

Show 2 more comments
No answers

Browser other questions tagged

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