android - 自定义 ListView 与 Fragments 的混淆

标签 android listview actionbarsherlock fragment

我在理解如何在 fragment 内创建自定义 listView 时遇到问题。我发现的大多数教程都有许多不同的方法来执行此操作,这些方法要么让我感到困惑,要么无法在 fragment 中使用。因为我能够使用 ...simple_list_item_1 创建一个简单的 listView, 当我尝试自定义我自己的 xml 以在 ea 列表中使用三个 textView 时,我的应用程序崩溃了。在这一点上,我想要做的就是创建一个简单的 ListView ,其中包含三个硬编码的字符串数组 (textViews)。有人能指出我正确的方向吗?

Fragment Code:

 public class BuyFragTab extends SherlockListFragment {

ArrayList<Bookinfo> bookRec = new ArrayList<Bookinfo>();

//defines the Arraylist Bookinfo record layout
//public class Bookinfo {
    public String titles[] = new String[]{"Bk 1", "Bk 2", "Bk 3", "Bk 4", "Bk 5", "Bk 6", "Bk 7", "Bk 8"};
    public String authors[] = new String[]{"Tom", "Dick", "Harry", "Jack", "James", "Skip", "Jim", "June"};
    public String status[] = new String[]{"Open", "Open", "Open", "Open", "Closed", "Closed", "Closed", "Closed"};

//}

//returns the view of the fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
            android.R.layout.simple_list_item_1, titles);
            //R.layout.buy_tbfrag, titles);

    /** Setting the array adapter to the listview */
    setListAdapter(adapter);


    return super.onCreateView(inflater, container, savedInstanceState);  
}
}


Fragment xml layout:

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

<!-- Put a background to the fragment android:background="#FF0000" -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/titleSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Input Title Name" />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Search" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>  

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No Books Registered" />
</LinearLayout>

Fragment List xml:

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

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

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

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

最佳答案

在这里你可以找到关于这个的非常简单的教程Example更多信息请访问 here

关于android - 自定义 ListView 与 Fragments 的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718141/

相关文章:

Android:Sherlock 操作栏下拉

带有 ActionBar 的 Android 状态栏覆盖

android - sqlite 数据库中的两种语言 - Android

android - RxJava : How to wait for a value to be initialized by a previous subscription?

android requestFocusFromTouch 调出第一项突出显示的菜单

android - 如何在同一个 ListView 中获取来自不同 Urls 的提要

java - 带有 SearchView 的 Android abs,onQueryTextListener 不工作

Android 构建系统,NEON 和非 NEON 构建

android - AltBeacon 应用程序在几分钟后重新启动

Android convertView,用还是不用?