I'm working on an android project which involves an array adapter and listview. However, when initizializing the arrayadpater I'm encountering some errors during runtime. error: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
I'm suspecting error on line 32 when initializing arrayadpater. Or maybe packagename and / or activity declaration in manifest file.... Any help appreciated
package org.pctechtips.menulistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList hosts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hosts = = new ArrayList<String>();
hosts.add("192.168.10.100 | 33:00:11:aa:cc:bb");
hosts.add("192.168.10.101 | 33:00:11:aa:cc:bb");
hosts.add("192.168.10.102 | 33:00:11:aa:cc:bb");
hosts.add("192.168.10.103 | 33:00:11:aa:cc:bb");
hosts.add("192.168.10.104 | 33:00:11:aa:cc:bb");
hosts.add("192.168.10.105 | 33:00:11:aa:cc:bb");
hosts.add("192.168.10.106 | 33:00:11:aa:cc:bb");
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, hosts);
list.setAdapter(adapter);
}
/*
* Infating the menu for toolbar
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
/*
* actions for menu options in toolbar
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.change_icon) {
Toast.makeText(getApplicationContext(), "Change Icon", Toast.LENGTH_SHORT).show();
return true;
}
if (id == R.id.set_hostname) {
Toast.makeText(getApplicationContext(), "Set Hostname", Toast.LENGTH_SHORT).show();
return true;
}
if (id == R.id.notifications) {
Toast.makeText(getApplicationContext(), "Notifications", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
list_menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="7dp"
android:src="@drawable/computer_48_dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:orientation="vertical"
android:paddingLeft="0dp"
>
<TextView
android:id="@+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
<TextView
android:id="@+id/mac_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
tools:text="aa:bb:cc:00:11:22"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
main_activity.xml file
<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"
tools:context=".MainActivity" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>