-2

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>
miatech
  • 2,150
  • 8
  • 41
  • 78
  • Alright, I just got it working. I gave it the first textView from my list_item.xml. the odd thing is that it works with the second textview too... that seems wrong. I want to be able to parse the information and assign to two different textview. maybe I have even more textview which requires more info... – miatech Aug 09 '17 at 21:05

2 Answers2

1

You can use customize R.layout.list_item without mentioning the textview id as next parameter because without this the adapter internal code won't able to find the textview to bind data

 // another overloaded constructor 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                                    R.layout.list_item,R.id.YourTextViewID hosts);
 //                                                    ^^^^^^^^^^^^^^^^^

or

you make sure that your TextView has id as android:id="@android:id/text1"

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • I've always declare arrayadpaters with three paratemeters(context, resource, arrays).. problably something wrong in xml file – miatech Aug 09 '17 at 19:27
  • i don't think ,i guess you might be using simple_list_item_1 or something but just post the complete code so that i can double check it though you can read more about adapter [constructors here](https://developer.android.com/reference/android/widget/ArrayAdapter.html) – Pavneet_Singh Aug 09 '17 at 19:30
  • can I post more code in this thread or I need another one? – miatech Aug 09 '17 at 19:36
  • yes you can , use the `edit` option – Pavneet_Singh Aug 09 '17 at 19:36
  • all textView in my custom xml list adapter has an id... I just updated the code – miatech Aug 09 '17 at 20:52
  • also, which textView do I have to use when my custome xml file contains more than one textView? – miatech Aug 09 '17 at 20:53
  • @miatech you need [customize adapter](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – Pavneet_Singh Aug 10 '17 at 06:16
0

You haven't posted your layout file for me to know if you're using a custom TextView or if you want to use the default Android provided TextView. You should have a TextView in the list_item_layout and reference it's ID in the ArrayAdapter adapter.

This Constructor takes the ID of the custom textview which you have defined

ArrayAdapter<String>(Context context, int resource, int textViewResourceId, T[] objects)

This constructor is for the ListView to use a default TextView with white background

ArrayAdapter<String>(this, int resource, T[] values)

The int resource value is given by android.R.layout.list_view and int textViewResourceID is given by android.R.id.list_item for a custom TextView

Ankit Sharma
  • 663
  • 1
  • 5
  • 17
  • I"m sure I"ve come across this before, just don't remember now. And I know I have always declare arrayadapters with three parameters(context, int, arraylist); so probably I'm doing something wrong somewhere else just don't remember now. like someone pointed out. I shouldve post the xml layout file – miatech Aug 09 '17 at 19:25
  • Do know that for the three parameter constructor, the App will use a default TextView. As I answered, if you're trying to use a custom TextView (which I advise you do), you'll need to use the first constructor I posted. – Ankit Sharma Aug 09 '17 at 19:27
  • could you guys check my xml files and suggest thanks – miatech Aug 09 '17 at 20:59
  • how do I know which textview to provide, I could have more than one. I it still works. how does the adapter knows where to put the data – miatech Aug 09 '17 at 21:14