Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addView(View) is not supported in AdapterView

Tags:

android

I'm starting in android development and I'm getting that error on the title.

Here is my Contacts.java

package us.inevent.toot;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Contacts extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new ContactListFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contacts, menu);
        return true;
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class ContactListFragment extends Fragment {

        private ViewGroup listContacts;

        public ContactListFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState)
        {

            View rootView = inflater.inflate(R.layout.fragment_contacts,
                    container, false);

            listContacts = (ViewGroup) rootView.findViewById(R.id.listContacts);

            TextView aux = new TextView(getActivity());

            aux.setText("Hello World!");

            listContacts.addView(aux);

            return rootView;
        }
    }

}

Inside the onCreateView() method of my ContactListFragment, I'm creating a TextView with "Hello World" as text.

Then, I'm trying to add that view to my ViewGroup listContacts.

Here is my fragment_contacts.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="us.inevent.toot.Contacts$PlaceholderFragment" >

    <TextView
        android:id="@+id/textContact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:text="@string/textContact"
        android:textSize="@dimen/abc_action_bar_title_text_size" />

    <ListView
        android:id="@+id/listContacts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textContact"
        android:layout_marginTop="10dp" />

</RelativeLayout>

What am I doing wrong?

Thanks.

like image 882
Maurício Giordano Avatar asked Apr 03 '14 20:04

Maurício Giordano


1 Answers

You are trying to set it to your ListView. That's not going to work. Try setting it to it's parent. With something like

listContacts.getParent().addView(aux);

Edit after comments

To add an item to your ListView you need to add it to whatever list you use to populate your ListView and call setAdapter() on it or notifyDataSetChanged() on your Adapter.

You don't add items to your ListView. You add items to your Adapter and set the Adapter on the ListView.

I suggest going through this tutorial

and reading through the docs thoroughly.

ListView

Adapter

From the docs

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

Your ListView is the AdapterView here (it could be a Spinner or other such things)

like image 144
codeMagic Avatar answered Oct 13 '22 17:10

codeMagic