Listview返回空

时间:2014-12-28 23:08:30

标签: android android-layout listview

美好的一天,我尝试在列表视图中构建一个带有水平列表的书架但是当应用程序运行时,列表视图为空白。 我错过了什么? 这是我的代码(以及我用作指南的教程中的一些代码)

ListBook.java

public class ListBooks extends ActionBarActivity {

    ListView listView;
    private VerticalAdapter verListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_books);

        listView = (ListView)findViewById(R.id.listbooks);
        BookItem book1 = new BookItem("Fist of Fury");
        BookItem book2 = new BookItem("Fist of the fists");
        BookItem book3 = new BookItem("Enders game");
        BookItem book4 = new BookItem("Lovers");

        ArrayList<BookItem> arrayList = new ArrayList<BookItem>();
        arrayList.add(book1);
        arrayList.add(book2);
        ArrayList<BookItem> list = new ArrayList<BookItem>();
        list.add(book3);
        list.add(book4);
        ArrayList<ArrayList<BookItem>> group = new ArrayList<ArrayList<BookItem>>();
        group.add(arrayList);
        group.add(list);
        verListAdapter = new VerticalAdapter(this, R.layout.activity_book_list, group);
        listView.setAdapter(verListAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_list_books, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private class VerticalAdapter extends ArrayAdapter<ArrayList<BookItem>> {
        private int resource;

        public VerticalAdapter(Context _context, int _ResourceId,
                               ArrayList<ArrayList<BookItem>> _items) {
            super(_context, _ResourceId, _items);
            this.resource = _ResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView;

            if (convertView == null) {
                rowView = LayoutInflater.from(getContext()).inflate(resource,
                        null);
            } else {
                rowView = convertView;
            }

            HorizontalListView hListView = (HorizontalListView) rowView
                    .findViewById(R.id.sublistview);
            HorizontalAdapter horListAdapter = new HorizontalAdapter(
                    getContext(), R.layout.listitem, getItem(position));
            hListView.setAdapter(horListAdapter);

            return rowView;
        }
    }

   private class HorizontalAdapter extends ArrayAdapter<BookItem> {

        private int resource;

        public HorizontalAdapter(Context _context, int _textViewResourceId,
                                 ArrayList<BookItem> _items) {
            super(_context, _textViewResourceId, _items);
            this.resource = _textViewResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View retval = LayoutInflater.from(getContext()).inflate(
                    this.resource, null);

            TextView topText = (TextView) retval.findViewById(R.id.title);


            topText.setText(getItem(position).getTitle());


            return retval;
        }
    }

}

布局文件 Listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff">


    <ImageView
        android:id="@+id/image"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:scaleType="centerCrop"
        android:src="@drawable/wood"
        />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:gravity="center_horizontal"
        />

</LinearLayout>

activity_book_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="com.azed">


    <com.HorizontalListView
        android:id="@+id/sublistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ddd"/>

</LinearLayout>

activity_list_books.xml

<LinearLayout 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:orientation="vertical"
    tools:context="com.azed">

    <ListView
        android:id="@+id/listbooks"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

</LinearLayout>

0 个答案:

没有答案