Czunio February 2016

Android Studio ListView from SQLite

I'm working on Android Studio.I have a problem with displaying data from SQLite database on my ListView. Here is a method to take data from SQLite(it works well,checked)

 public List<Terminy> getAllItems(){
    List<Terminy> itemList = new ArrayList<Terminy>();

    String selectQuery = "SELECT id,data FROM Terminy WHERE Wolny = 1";

    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);


    if(cursor.moveToFirst()){
        do{
            Terminy termin = new Terminy();
            termin.setId2(Integer.parseInt(cursor.getString(0)));
            termin.setData(cursor.getString(1));
            itemList.add(termin);
        }while(cursor.moveToNext());
    }
    cursor.close();
    return itemList;
}

And there is method to put data into ListView

public void ButtonLista(View v)
{
    ArrayAdapter<Terminy> adapter;
    ArrayList<Terminy> lista;
    DatabaseHelper helper = new DatabaseHelper(this);
    ListView listView = (ListView) findViewById(R.id.listView);
    lista = new ArrayList<>(helper.getAllItems());
    adapter = new ArrayAdapter<Terminy>(this,R.layout.activity_panel_uzytkownik, lista);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

LogCat Error

com.example.czou.proojekt E/ArrayAdapter: You must supply a resource ID for a TextView com.example.czou.proojekt D/AndroidRuntime: Shutting down VM com.example.czou.proojekt W/art: Suspending all threads took: 02-08 17:47:59.352 1779-1779/com.example.czou.proojekt E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.czou.proojekt, PID: 1779 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 

Could some one help me ? I have read many tutorials so far and still don't know how to solve my problem :(

Answers


Souf R February 2016

Take a look at this Answer

Or try using a separate Adapter (extends ArrayAdapter) to have a full control over your layout and each piece of it.

Here is a tutorial about using ArrayAdapter link

Another tutorial on how to use SQLIte with ListView link


Czunio February 2016

<RelativeLayout 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: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="com.example.czou.proojekt.PanelUzytkownik">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:onClick="ButtonLista" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="80dp" />

Post Status

Asked in February 2016
Viewed 1,531 times
Voted 9
Answered 2 times

Search




Leave an answer