我有一個問題,瞭解如何創建一個片段內的自定義listView。我發現的大多數教程都有很多不同的方法來做到這一點,或者讓我感到困惑,或者不能在片段中使用。因爲我能夠使用... simple_list_item_1, 創建一個簡單的listView,當我嘗試自定義我自己的xml以在ea列表中使用三個textView時,我的應用程序崩潰。在這一點上,我想要做的就是創建一個由三個硬編碼字符串數組(textViews)組成的簡單列表視圖。有人能指引我朝着正確的方向嗎?自定義ListView混亂與片段
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"
/>
我以前見過本教程,但在使用片段時遇到問題。我的錯誤之一是我不能在代碼中擁有setContentView參數。如果這是一個主要活動,我不會有任何問題。 – Skip
從這個討論我明白你想創建一個片段的列表視圖,如果是的話這不是一個問題,上面的例子應該已經解決了你的問題。 –
上述問題並不能解決問題,因爲通過研究,我發現setContentView參數在片段內無效。該錯誤讀取,「方法setContentView(int)未定義類型ListFragment」。 – Skip