0

I'm new to android. I have created a a navigation drawer and made menus in it, dinamic by adding fragments. Now for each menu I want to create a custom list view with arrayadapter class. I implemented it, but the problem is that the title and address are the same in every item of the list. The list displays only the last item. Can anyone help me please!! I can't figure out the solution. Here is some code and a photo:

import static com.example.user.appsightseeing.R.layout;



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


    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);

        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.", R.drawable.artificiallake));
        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", R.drawable.zoopark));
        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", R.drawable.memorialpark));
        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", R.drawable.kasharpark));
        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", R.drawable.vaqarripark));
        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", R.drawable.farkapark));
        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", R.drawable.pezapark));
        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", R.drawable.dajtirecreative));
        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", R.drawable.dajtinational));
        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", R.drawable.botanicpark));
        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", R.drawable.riniapark));

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


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

        //add event listener so we can handle clicks
    }

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

        super.onViewCreated(view, savedInstanceState);
    }
}

The arrayadapter class:

import java.util.List;



    public class parkArrayAdapter extends ArrayAdapter<Park> {

        private Context context;
        private List<Park> parks;
        private AnimatedStateListDrawable inflater;

        //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
        @NonNull
        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 title = (TextView) view.findViewById(R.id.p_title);
            TextView description = (TextView) view.findViewById(R.id.p_description);
            TextView streetname = (TextView) view.findViewById(R.id.address);
            ImageView image = (ImageView) view.findViewById(R.id.image);

            //set title and description
            String titleT = par.getPark_title();
            title.setText(titleT);

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

            streetname.setText(par.getPark_streetname());

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

            return view;
        }
    }

The park class:

public class Park {

    private static String title;
    private String description;
    private static String streetname;
    private int image;


    public Park(String title, String description, String streetname, int image){
        this.title = title;
        this.description = description;
        this.streetname = streetname;
        this.image = image;

    }

    public static String getPark_title() { return title; }

    public String getPark_description() {
        return description;
    }

    public static String getPark_streetname() {
        return streetname;
    }

    public int getPark_image() {
        return image;
    }
}

The park layout for a row:

<?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:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/p_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="5dp"
            android:text="Park Description"
            android:textColor="@android:color/black"
            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"
        android:layout_alignParentBottom="true">

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

    </RelativeLayout>

</RelativeLayout>

screenshot of the listview with same titles and addresses in all items

Sindi B
  • 62
  • 10

3 Answers3

0

Because you are using static variables remove static from the title and streetname in Park class

 private String title; 
 private String streetname;

The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves.

Pavan
  • 5,016
  • 1
  • 26
  • 30
0

Remove static from fields title and streetname in class Park.

Also, classes should be capitalised, so ParkArrayAdapter, not parkArrayAdapter

thorin86
  • 604
  • 11
  • 26
  • thank you very much! Actually i have declared them static at the beggining but due to some java suggestions i changed them to static. then got confused and could not see the difference. Now i realize why my description and image were working fine. It all works perfectly ! Thank you !! – Sindi B Jun 17 '17 at 15:12
0

This is not related with Android. If you check your class you will see that you described title and streetname fields and their getter methods static. You are facing this issue because when you get value from adapter it is same for all title and streetname fields. You made a good example for describing differences between static members and instance members in any application on any platform. Instance members are on different addresses of memory, but static members are on same addresses and when you assign a value to static member, then all static members's values are changing because they are on same place on memory. When you delete "static" words, your listview will works well.

CanDroid
  • 633
  • 6
  • 15
  • Thank you very very much for the detailed explanation! Now i realize the difference to which i didn't pay attention. – Sindi B Jun 17 '17 at 15:15
  • Your welcome. Happy to read your comment. Best lessons are coming with good mistakes, you will never forget then static and instance members issue. – CanDroid Jun 17 '17 at 15:19
  • Can you please give me a hint, about how implementing the onItemClickedListener for my list items, and start a new activity or a new fragment (i'm not sure) with the details of the item. Having the listview implemented in a fragment is confusing me, as it is not the same way of implementation as for an activity. I don't know if you can help.. Thanks in advance!!! – Sindi B Jun 17 '17 at 19:44
  • Can you check my comment please? @CanDroid – Sindi B Jun 18 '17 at 12:01
  • Hello Sindi. You can check this link: https://stackoverflow.com/questions/6510550/android-listview-in-fragment – CanDroid Jun 18 '17 at 12:16
  • Hello!! Thank you for the response. @CanDroid I'll check immediately! – Sindi B Jun 18 '17 at 12:21