Reputation: 3
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 ?
Upvotes: 0
Views: 302
Reputation: 1
You should give to the adapter the name of the textView
ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, R.id.TextViewNew, testArray);
ListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Upvotes: 0
Reputation: 68
I could be wildly wrong on this but I think that if you want to use a LinearLayout as the root element in the resource passed to your adapter you have to use a custom adapter. If you want to stick to the built-in ArrayAdapter you need to change row_layout_new.xml so that it only contains a TextView.
EDIT: Or, as stated in another answer in this thread, provide the TextView contained in the LinearLayout as another argument to the ArrayAdapter constructor.
Upvotes: 0
Reputation: 5951
ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, R.id.TextViewNew, testArray);
you also need to provide the proper textview id with your constructor.
Upvotes: 2
Reputation: 18112
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);
Upvotes: 1