Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView项目不会加载到屏幕上_Android_Android Layout_Listview - Fatal编程技术网

Android ListView项目不会加载到屏幕上

Android ListView项目不会加载到屏幕上,android,android-layout,listview,Android,Android Layout,Listview,用例如下所示: 我想在ViewPager中加载一个ListView,同时在列表上设置一个ListView监听器 有人能告诉我为什么ListView没有加载,即屏幕一直是黑色的吗。连传呼机上的带子都不起作用 布局: 一些代码: public static class HeaderFragment extends ListFragment { List<Message> messages = new ArrayList<Message>(); String

用例如下所示: 我想在ViewPager中加载一个ListView,同时在列表上设置一个ListView监听器

有人能告诉我为什么ListView没有加载,即屏幕一直是黑色的吗。连传呼机上的带子都不起作用

布局:

一些代码:

public static class HeaderFragment extends ListFragment {

    List<Message> messages = new ArrayList<Message>();
    String topic;

    public HeaderFragment(List<Message> messages, String topic) {
        this.messages = messages;
        this.topic = topic;
    }

    public HeaderFragment() {
        // TODO Auto-generated constructor stub
    }

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

        View v = inflater.inflate(R.layout.submain, container, false);

        final ListView listView = (ListView) v.findViewById(android.R.id.list);

        final MessageItemAdapter adapter = new MessageItemAdapter(inflater,
                R.layout.headers, messages, 1);


        //the click listener.
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Message message = adapter.getItem(position);
                Intent intent = new Intent();
                intent.setClassName(
                        "main",
                        "NewsActivity");
                intent.putExtra("title", message.getTitle());
                intent.putExtra("description", message.getDescription());
                intent.putExtra("source", message.getLink().toString());
                intent.putExtra("language", location);
                startActivity(intent);
            }
        });

        listView.setAdapter(adapter);

        return v;
    }
提前感谢

编辑:

调用片段的代码:

public class HeadersActivityPagerAdapter extends FragmentStatePagerAdapter {

    public HeadersActivityPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {

        int k = 0;
        String topic = null;
        List<Message> messages = new ArrayList<Message>();

        for (Entry<String, List<Message>> elem : fragmentsData.entrySet()) {
            if (k==i)
            {
                topic = elem.getKey();
                messages = elem.getValue();
                break;
            }
            k++;
        }

        return new HeaderFragment(messages, topic);
    }

    @Override
    public int getCount() {
        return fragmentsData.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        int k = 0;
        String topic = "";
        for (Entry<String, List<Message>> elem : fragmentsData.entrySet()) {
            if (k==position)
            {
                topic = elem.getKey();
                break;
            }
            k++;
        }
        return topic;
    }
}
它是这样工作的,但我失去了ListView的侦听器:

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="1" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />
</android.support.v4.view.ViewPager>
submin.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="vertical" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />
</FrameLayout>
一些代码:

public static class HeaderFragment extends ListFragment {

    List<Message> messages = new ArrayList<Message>();
    String topic;

    public HeaderFragment(List<Message> messages, String topic) {
        this.messages = messages;
        this.topic = topic;
    }

    public HeaderFragment() {
        // TODO Auto-generated constructor stub
    }

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

        View v = inflater.inflate(R.layout.submain, container, false);

        final ListView listView = (ListView) v.findViewById(android.R.id.list);

        final MessageItemAdapter adapter = new MessageItemAdapter(inflater,
                R.layout.headers, messages, 1);


        //the click listener.
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Message message = adapter.getItem(position);
                Intent intent = new Intent();
                intent.setClassName(
                        "main",
                        "NewsActivity");
                intent.putExtra("title", message.getTitle());
                intent.putExtra("description", message.getDescription());
                intent.putExtra("source", message.getLink().toString());
                intent.putExtra("language", location);
                startActivity(intent);
            }
        });

        listView.setAdapter(adapter);

        return v;
    }
问题是我失去了我的倾听者,不会触发。 我找到的解决方案让我的应用程序慢了很多


我将侦听器放置在适配器内,用于每个项目。

您的ViewPager设置为高度0px。给它一个更大的高度,你应该能够看到它。也许:

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
编辑:

显示创建此片段并将其添加到屏幕的代码。还要将一些日志记录添加到此构造函数:

public HeaderFragment(List<Message> messages, String topic) {
    this.messages = messages;
    Log.i("TAG", "messages size: " + this.messages.size(); // <- add this
    this.topic = topic;
}

确保消息在传递到此处时包含某些项。此片段从不向消息中添加任何项,因此,如果在创建片段时它没有任何项,则不会在ListView中显示任何项。

问题仍然存在。显示条带,但ListView不显示任何数据…将代码发布到创建片段的位置,然后查看我的编辑。消息不是空的,这是肯定的。这是肯定的。你怎么能肯定?您没有将其记录到任何地方。因为此代码已重构,并且我在2分钟前已对此进行了调试。因为您在ViewPager中有它,所以我假设您在其中显示了其他内容。这些东西能正常显示吗?另外,请发布MessageItemAdapter类的代码。
public HeaderFragment(List<Message> messages, String topic) {
    this.messages = messages;
    Log.i("TAG", "messages size: " + this.messages.size(); // <- add this
    this.topic = topic;
}