我在理解如何在片段中创建自定义 listView 时遇到问题。我发现的大多数教程都有很多不同的方法来做到这一点,要么让我感到困惑,要么不能在片段中使用。因为我能够使用 ...simple_list_item_1 创建一个简单的 listView,所以当我尝试自定义我自己的 xml 以在 ea 列表中使用三个 textView 时,我的应用程序崩溃了。此时我要做的就是创建一个简单的列表视图,其中包含三个硬编码的字符串数组(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"
/>