ListView不适用于膨胀的视图

时间:2014-04-20 19:07:05

标签: android listview android-fragments android-arrayadapter

如果我使用片段,它不会显示任何条目,但如果我在布局中直接向ListView添加适配器,则效果很好。

有人可以解释一下吗?

The ListView

这是我的代码: MainActivity

package com.example.testandroid;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

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


        View view = getLayoutInflater().inflate(R.layout.list_fragment, null);
        ListView listv = (ListView) view.findViewById(R.id.ListViewInFragment);
        ListView listv2 = (ListView) findViewById(R.id.listViewInRoot);

        // ListView listv = (ListView) getFragmentManager().findFragmentById(R.id.fragment1).getView().findViewById(R.id.ListView1);

        ArrayList<String> alist = new ArrayList<String>();
        alist.add("text1");
        alist.add("text2");
        alist.add("text3");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_view, R.id.listRow, alist);


        listv.setAdapter(adapter);
        listv2.setAdapter(adapter);
    }

}

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.example.testandroid.MainActivity"
    tools:ignore="MergeRootFrame" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.testandroid.ListFragment"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:layout="@layout/list_fragment" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="10dp" >

    </LinearLayout>

    <ListView
        android:id="@+id/listViewInRoot"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

    </ListView>

</LinearLayout>

ListFragment

package com.example.testandroid;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list_fragment, container, false);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ListViewInFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

list_view.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="- " />

    <TextView
        android:id="@+id/listRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

0 个答案:

没有答案