addView(查看)中不支持适配器视图 [英] addView(View) is not supported in AdapterView

查看:143
本文介绍了addView(查看)中不支持适配器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Android的发展,我得到这个错误的标题。

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

下面是我的Contacts.java

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;
        }
    }

}

在我的 ContactListFragment ,我创建的 onCreateView()方法的TextView 与Hello World的文本。

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

然后,我想这个观点添加到我的的ViewGroup listContacts。

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

下面是我的 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?

感谢。

推荐答案

您正试图将其设置为你的的ListView 。这是行不通的。尝试将它设置为它的父。随着像

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);

的意见后修改

要将项目添加到您的的ListView 你需要把它添加任何列表,你用它来填充你的的ListView 并调用 setAdapter()上,或 notifyDataSetChanged()适配器

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.

您没有将项目添加到您的的ListView 。将项目添加到您的适配器,并设置适配器的ListView

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

建议<一href="https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=8&cad=rja&uact=8&ved=0CHgQFjAH&url=http%3A%2F%2Fwww.vogella.com%2Ftutorials%2FAndroidListView%2Farticle.html&ei=3so9U6nVNaj22AX9nYDwCg&usg=AFQjCNEmKxSOQWTEdnIteMT1duTBuc2Xsg&sig2=LE9YKIoMt0RtSrGUUKgdWQ&bvm=bv.64125504,d.b2I"相对=nofollow>通过这个教程

和读通过的文档彻底。

的ListView

适配器

从文档

这是适配器对象充当一个AdapterView之间的桥梁,该视图的基础数据。适配器提供访问的数据项。适配器还负责制造视图在数据组中的每个项。

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.

的ListView 适配器视图这里(它可能是一个微调或其他类似的东西)

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

这篇关于addView(查看)中不支持适配器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆