Question

I found a lot of same problem, but I don't really know the solution for my problem. I have a simple Costumer parse.com class, and want to list the names. (all of them.)

Logcat says:

05-02 19:31:46.534: E/ArrayAdapter(12816): You must supply a resource ID for a TextView
05-02 19:31:46.534: D/AndroidRuntime(12816): Shutting down VM
05-02 19:31:46.534: W/dalvikvm(12816): threadid=1: thread exiting with uncaught exception (group=0x41cd1700)
05-02 19:31:46.574: E/AndroidRuntime(12816): FATAL EXCEPTION: main
05-02 19:31:46.574: E/AndroidRuntime(12816): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

I think there is a problem with my IDs. I never used Adapter before.

// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("Wait! Listing customers!");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "Customers");
        // query.orderByDescending("_created_at");
        try {
            ob = query.find();
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        listview = (ListView) findViewById(R.id.listview);
        adapter = new ArrayAdapter<String>(MainActivity.this,
                R.layout.activity_main);

        for (ParseObject customer : ob) {
            adapter.add((String) customer.get("name"));
        }
        listview.setAdapter(adapter);
        mProgressDialog.dismiss();
    }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.csaba.noteapp.MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
    android:id="@+id/tvname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/etname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<TextView
    android:id="@+id/tvphone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Phone:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/etphone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/tvcity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="City:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/etcity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<Button
    android:id="@+id/buttonlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="List of customers" />

<Button
    android:id="@+id/buttonadd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add costumer" />

  <ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Was it helpful?

Solution

You are passing a custom layout in your ArrayAdapter. Here you should pass the resource id of the textview on which you want to show the text. Thats why it is giving this error.

Just call the adapter this way.

listview = (ListView) findViewById(R.id.listview);
        adapter = new ArrayAdapter<String>(MainActivity.this,
                R.layout.activity_main,R.id.tvname );

here R.id.tvname is the id of one of the textview in your activity_main.xml. Or you can create the custom adapter.

OTHER TIPS

Look at the constructor of ArrayAdapter

public ArrayAdapter (Context context, int resource)

Added in API level 1 Constructor

Parameters context The current context. resource The resource ID for a layout file containing a TextView to use when instantiating views.

Check the source

http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/ArrayAdapter.java

Look at line 387 which is exactly what the stacktrace says. You can check createViewFromResource and know what happens

What you need is a xml with TextView only

row.xml

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

/>

And then

adapter = new ArrayAdapter<String>(MainActivity.this,
            R.layout.row);

You could also have your custom adapter and customize the textview

Also check this

"ArrayAdapter requires the resource ID to be a TextView" xml problems

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top