2014-04-03 16 views
11

Ich beginne mit Android-Entwicklung und ich bekomme diesen Fehler auf den Titel.addView (View) wird in AdapterView nicht unterstützt

Hier ist meine 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; 
     } 
    } 

} 

Im Inneren des onCreateView() Methode meiner ContactListFragment, ich bin ein TextView mit "Hallo Welt" als Text zu schaffen.

Dann versuche ich, diese Ansicht zu meinem ViewGroup listContacts hinzuzufügen.

Hier ist meine 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> 

Was mache ich falsch?

Danke.

+0

* Was mache ich falsch? * - du bist Hinzufügen einer 'TextView' zu einer' ListView', die auf normale Weise nicht erlaubt ist. – Luksprog

+1

@Luksprog, wie füge ich ein Element zu meiner 'ListView' hinzu? –

Antwort

7

Sie versuchen, es auf Ihre ListView zu setzen. Das wird nicht funktionieren. Versuchen Sie, es auf das übergeordnete Element zu setzen. Mit so etwas wie

listContacts.getParent().addView(aux); 

bearbeiten nach Kommentare

Um einen Artikel zu Ihrem hinzufügen ListView Sie müssen es addieren, was Liste, die Sie verwenden, um Ihre ListView bevölkern und rufen setAdapter() auf sie oder notifyDataSetChanged() auf Ihrem Adapter .

Sie fügen keine Artikel zu Ihrem ListView hinzu. Sie fügen Elemente zu Ihrer Adapter hinzu und setzen die Adapter auf die ListView.

Ich schlage vor, going through this tutorial

und das Lesen durch die gründlich docs.

ListView

Adapter

die Dokumente

Ein Adapter-Objekt fungiert als eine Brücke zwischen einem AdapterView und die zugrundeliegenden Daten für diese Ansicht. Der Adapter bietet Zugriff auf die Datenelemente. Der Adapter ist auch dafür zuständig, für jedes Element im Datensatz eine Ansicht zu erstellen.

Ihre ListView ist die AdapterView hier (es könnte ein Spinner oder andere solche Dinge sein)

+0

Aber ich möchte ein Element zu meiner 'ListView' hinzufügen. Das kann ich mit deinem Beispiel nicht erreichen. –

+0

"Aber ich möchte ein Element zu meinem ListView hinzufügen" ... das ist nicht das, was Sie in Ihrem Post * überhaupt gesagt haben *. – codeMagic

+1

Wie könnte ich das erreichen? –

0

Verwendung Linear-Layout statt Listenansicht.

0

Setzen Sie Ihre Listenansicht in Linear-Layout und fügen Sie dann eine beliebige Ansicht in der linearen Liste seiner nicht gonna den Fehler geben

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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="wrap_content" 
android:layout_height="match_parent" 
android:id="@+id/llist" 
tools:context="com.android.parteek.contentprovider.Main2Activity"> 

    <android.support.v7.widget.SearchView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/serch" /> 


< ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.android. parteek.contentprovider.Main2Activity"> 

</ListView> 

Verwandte Themen