Search code examples
androidlistviewandroid-arrayadapter

My custom ListView row make app crash?


Quite new to android development I was just messing around with ListViews using the standard layout (android.R.layout.simple_list_item_1) no problem with that.

But when i'm trying a custom layout, my app crash at launch.

First my files, so you can see what I'm doing (simple stuff just for getting used to it)

Java file:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] testArray = {"test1", "test2", "test3", "test4", "test5", "test6", "test7"};

    ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, testArray);
    ListView theListView = (ListView) findViewById(R.id.theListView);
    theListView.setAdapter(theAdapter);

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String testArrayPicked = "You selected " +
                    String.valueOf(parent.getItemAtPosition(position));

            Toast.makeText(MainActivity.this, testArrayPicked, Toast.LENGTH_SHORT).show();
        }
    });
 }
}

View (activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">

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

</LinearLayout>

My layout file (row_layout_new.xml):

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:id="@+id/TextViewNew"/>

</LinearLayout>

On my android monitor I have the following:

E/ArrayAdapter: You must supply a resource ID for a TextView

But I gave an id to the TextView in my row layout.

I watched some tutorials (like in Derek Banas youtube channel if some of you knows), he is doing it the same way but it crash on my side.

Am I missing something obvious or I have to make my own ArrayAdapter function ?


Solution

  • As the error says you need to pass the TextView id to the ArrayAdapter to make it function.

    Change

    ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, testArray);
    

    to

    ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, R.id.TextViewNew, testArray);