Skip
Skip

Reputation: 35

Custom ListView confusion with Fragments

I am having a problem understanding how to create a custom listView inside a fragment. Most tutorials I've found have many different ways of doing this that either confuses me or can't be used in a fragment. As I am able to create a simple listView using ...simple_list_item_1, when I try to customize my own xml to used three textViews inside ea list, my app crashes. At this point all I want to do is create a simple listview consisting of three hard-coded string arrays (textViews). Can someone point me in the right direction?

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"
         />

Upvotes: 1

Views: 1381

Answers (1)

Ali Imran
Ali Imran

Reputation: 9217

Here you can find very easy tutorial for this Example and more on this at here

Upvotes: 2

Related Questions