简体   繁体   中英

Custom Listview does not display in a fragment of navigation drawer, with custom arrayadaptar class

I created a navigation drawer and for each menu and when clicked i created fragments. Now i am trying to add custom listview with a picture, text and description in each fragment but my app stops when i click the menu item "Parks and lakes " in the drawer. Here is some code:

    `public class ParksFragment extends Fragment {
    private ArrayList<Park> parks = new ArrayList<>();
    parkArrayAdapter adapter;

    public ParksFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        parks.add(new Park("Artificial Lake of Tirana", "The Grand Park of Tirana, also known as the Tirana Park on the Artificial Lake or even the Park of Saint Procopius, is a 230 hectare public park situated on the southern part of Tirana.",
                        "At the end of Rruga Sami Frasheri.", "artificiallake.jpg"));
        parks.add(new Park("Zoo park", "The only one of its kind in Albania, Tirana Zoo is concentrated in an area of \u200B\u200B7 hectares in the southern part of town, between the Grand Park and the Botanic Garden of Tirana . The zoo was established in 1966.",
                        "Near Rruga Liqeni i Thate", "botanicpark.jpg"));
        parks.add(new Park("Memorial park of the Cemetery of the Nation's Martyrs","The National Martyrs Cemetery of Albania is the largest cemetery in Albania, located on a hill overlooking Tirana. The \"Mother Albania\" statue is located at the Cemetery.",
                        "Near street Rruga Ligor Lubonja","memorialpark.jpg"));
        parks.add(new Park("Kashar park","The main core of Kashar’s Park, is the Reservoir of Purez- Kus. The reservoir and its surrounding territory are considered as one of the most picturesque and biologically unsoiled suburbs of Tirana.",
                        "Kashar","kasharpark.jpg"));
        parks.add(new Park("Vaqarr park","The second park in Vaqarr, is a recreational area of 97 ha, that is more than useful to inhabitants in Tirana.",
                        "Vaqarr","vaqarripark.jpg"));
        parks.add(new Park("Farka Lake park","To the East of the East of Tirana’s city center, Lake Farka is a local favorite for waterborne fun in Summer. Picnicking, jet and water skiing, swimming, boating, all the usual wet sports suspects.",
                        "At Lake of Farka, near Rruga Pjeter Budi","farkapark.jpg"));
        parks.add(new Park("Peza park","Peza, a village approximately 20 minutes from the center of Tirana, is a popular place for locals to go for a coffee or lunch on the weekends to escape the city.",
                        "Peze","pezapark.jpg"));
        parks.add(new Park("Dajti Recreative park","This park is one of the components of Dajti National Park, located 26 km east of Tirana and 50 km from \"Mother Teresa\" airport. This place is very frequented by tourists and is also known as the \"Natural Balcon of Tirana\" which offers recreation and accommodation facilities for tourists.",
                        "Dajti mountain","dajtirecreative.jpg"));
        parks.add(new Park("Dajti National park","Dajti National Park is very important on local, national and regional level, for its biodiversity, landscape, recreational and cultural values. Among others it is considered as a live museum of the natural vertical structure of vegetation.",
                        "Dajti mountain","dajtinational.jpg"));
        parks.add(new Park("Rinia park","The park, 500 metres (1,600 ft) from the central square, was built in 1950[5] as part of a major urban development program which developed after World War II. It was initially a pleasant family park where inhabitants of Tirana could take their children.",
                        "Near Bulevardi Deshmoret e Kombit and near Rruga Myslym Shyri","riniapark.jpg"));
        parks.add(new Park("Botanic garden","The Botanical Gardens of Tirana are scenic botanical gardens located in southern Tirana, Albania. It is the only botanical garden in Albania. Construction commenced in 1964, with the original site covering approximately 15 hectares.",
                        "Near Zoo park","botanicpark.jpg") );

        ArrayAdapter<Park> adapter = new parkArrayAdapter(getActivity(), 0, parks);


        ListView listView = (ListView) getActivity().findViewById(R.id.customListView);
        listView.setAdapter(adapter);



        // Inflate the layout for this fragment
        return inflater.inflate(layout.fragment_parks, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
    }
}

Here is my arrayadapter class:

`class parkArrayAdapter extends ArrayAdapter<Park> {

    private Context context;
    private List<Park> parks;

    //constructor, call on creation
    public parkArrayAdapter(Context context, int resource, ArrayList<Park> objects) {
        super(context, resource, objects);

        this.context = context;
        this.parks = objects;
    }

    //called when rendering the list
    public View getView(int position, View convertView, ViewGroup parent) {

        //get the park we are displaying
        Park par = parks.get(position);

        //get the inflater and inflate the XML layout for each item
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.park_layout, null);

        TextView park_title = (TextView) view.findViewById(R.id.p_title);
        TextView park_description = (TextView) view.findViewById(R.id.p_description);
        TextView park_streetname = (TextView) view.findViewById(R.id.address);
        ImageView park_image = (ImageView) view.findViewById(R.id.image);

        //set title and description


        String completetitle = Park.getPark_title();
        park_title.setText(completetitle );


        //display trimmed excerpt for description
        int descriptionLength = par.getPark_description().length();
        if(descriptionLength >= 100){
            String descriptionTrim = par.getPark_description().substring(0, 100) + "...";
            park_description.setText(descriptionTrim);
        }else{
            park_description.setText(par.getPark_description());
        }

        //set adress
        String completeadress =Park.getPark_streetname();
        park_streetname.setText(completeadress);

        //get the image associated with this park
        int imageID = context.getResources().getIdentifier(par.getPark_image(), "drawable", context.getPackageName());
        park_image.setImageResource(imageID);

        return view;
    }}

Here is my custom row i want to display for each park:

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginRight="10dp"
        android:contentDescription="Park Image" />

    <LinearLayout
        android:id="@+id/infoSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"
        android:orientation="vertical">

        <TextView
            android:id="@+id/p_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="Park Title"
            android:textSize="18sp" />
        <TextView
            android:id="@+id/p_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="Park Description"
            android:textSize="15sp" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/addressSection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/infoSection"
        android:orientation="vertical">

        <TextView
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:text="Address:" />

    </RelativeLayout>

</RelativeLayout>

Here is my fragment layourt that contains my custom listview:

`<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.appsightseeing.ParksFragment">

    <!-- TODO: Update blank fragent layout -->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/customListView"></ListView>

</FrameLayout>

Firstly create the view then call the listView

public class ParksFragment extends Fragment {

private ArrayList<Park> parks = new ArrayList<>();
parkArrayAdapter adapter;

public ParksFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

View rootView = inflater.inflate(layout.fragment_parks, container, false);

...

 ListView listView = (ListView)rootView.findViewById(R.id.customListView);
    listView.setAdapter(adapter);
 return rootView;

on the line where you have set a text, remove

Park.getXXX()

change it and use the object you retrieved on the first line of your getView() method. do

par.getXXX();

then set the corresponding value.

sindi-b try changing the class to this

class parkArrayAdapter extends ArrayAdapter<Park> {

private Context context;
private Parks parks;

//constructor, call on creation
public parkArrayAdapter(Context context, int resource, ArrayList<Park> objects) {
    super(context, resource, objects);

    this.context = context;
    this.parks = objects;
}

//called when rendering the list
public View getView(int position, View convertView, ViewGroup parent) {

    //get the park we are displaying
    parks = (Parks) getItem(position);

    //get the inflater and inflate the XML layout for each item
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.park_layout, null);

    TextView park_title = (TextView) view.findViewById(R.id.p_title);
    TextView park_description = (TextView) view.findViewById(R.id.p_description);
    TextView park_streetname = (TextView) view.findViewById(R.id.address);
    ImageView park_image = (ImageView) view.findViewById(R.id.image);

    //set title and description


    String completetitle = park.getPark_title();
    park_title.setText(completetitle );


    //display trimmed excerpt for description
    int descriptionLength = park.getPark_description().length();
    if(descriptionLength >= 100){
        String descriptionTrim = park.getPark_description().substring(0, 100) + "...";
        park_description.setText(descriptionTrim);
    }else{
        park_description.setText(park.getPark_description());
    }

    //set adress
    String completeadress =park.getPark_streetname();
    park_streetname.setText(completeadress);

    //get the image associated with this park
    int imageID = context.getResources().getIdentifier(park.getPark_image(), "drawable", context.getPackageName());
    park_image.setImageResource(imageID);

    return view;
}}

It should work better now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM