gpt4 book ai didi

android - 自定义 ListView 与 Fragments 的混淆

转载 作者:行者123 更新时间:2023-11-29 21:54:00 25 4
gpt4 key购买 nike

我在理解如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com