I'm trying to display the contents of an array in a ListView on Android. I initially had the error
ArrayAdapter requires the resource ID to be a TextView
so I changed the ArrayAdapter from
ArrayAdapter<String> adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.activity_list_item, weatherData);
to
ArrayAdapter<String> adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.activity_list_item, R.id.list_item, weatherData);
However I now get the error
java.lang.RuntimeException: Failed to find view with ID com.example.weatherapp:id/list_item in item layout
I was hoping that someone could help me fix this. The whole code is below
Java File
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayListView();
}
public void displayListView () {
String[] weatherData = {"Today - Storm 8 / 12", "Tomorrow - Foggy 9 / 13", "Thurs - Rainy 8 / 13", "Fri - foggy 8 / 12", "Sat - Sunny 9 / 14", "Sun - Sunny 10 / 15", "Mon - Sunny 11 / 15" };
ArrayAdapter<String> adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.activity_list_item, R.id.list_item, weatherData);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
public void toastFunction (View view) {
Context context = getApplicationContext();
CharSequence text = "Refreshing Weather!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<TextView
android:id="@+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Welcome to weather dummy app :-)" />
<Button
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="110dp"
android:layout_marginEnd="16dp"
android:onClick="toastFunction"
android:text="Refresh" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/enterLocationName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Location Name" />
</com.google.android.material.textfield.TextInputLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="0dp"
android:layout_marginTop="205dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp" />
</RelativeLayout>
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textSize="25sp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>