0

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 :(

John Joe
  • 12,412
  • 16
  • 70
  • 135
Czunio
  • 11
  • 1
  • 1
    what's the problem ? – Blackbelt Feb 08 '16 at 15:44
  • my method not working. When i click on button to show data on ListView my application stops and quit. – Czunio Feb 08 '16 at 15:46
  • 1
    do you mind sharing the stacktrace of the exception/crash (aka *the logcat*) – Blackbelt Feb 08 '16 at 15:47
  • yee exactly.Unfortunately,app has stopped error message – Czunio Feb 08 '16 at 15:49
  • @Czunio post your logcat please – John Joe Feb 08 '16 at 17:01
  • @John Joe 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 – Czunio Feb 08 '16 at 17:54
  • It seems like there are something wrong with your xml layout. – John Joe Feb 08 '16 at 17:57
  • @John Joe yee i know it,but i dont know how to solve my problem. It's my first app with Android Studio – Czunio Feb 08 '16 at 17:59
  • Can you show `activity_panel_uzytkownik` layout ? – John Joe Feb 08 '16 at 18:01
  • @John Joe u got it on Answer now – Czunio Feb 08 '16 at 18:18

2 Answers2

0

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

Community
  • 1
  • 1
Soufiane ROCHDI
  • 1,543
  • 17
  • 24
0
<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" />

Czunio
  • 11
  • 1
  • where are your `textView` ? – John Joe Feb 08 '16 at 18:20
  • @John Joe i don't have it... thought that i need only ListView and nothing else – Czunio Feb 08 '16 at 18:22
  • You need to have two layout in this case. Please see [this](http://stackoverflow.com/questions/8673829/arrayadapter-requires-id-to-be-a-textview-error) and [this](http://stackoverflow.com/questions/5916459/you-must-supply-a-resource-id-for-a-textview-android-error) – John Joe Feb 08 '16 at 18:28