0

I'm super new to Android Studio and even java, so I have some basic questions. I want to fill a ListView with Data about animals.

What would be the best way to do so? For example I have a cat and a dog class containing a price tag ( both different prize ). Now I want to display cat and dog in my ListView with their respective prize.

How can I set this up correctly? I'm not sure how I get that Info inside the ArrayList, so it can be transferred to the ListView.

Hope you guys can support me a little there. Thank you :)

  • Possible duplicate of http://stackoverflow.com/questions/5070830/populating-a-listview-using-arraylist – AL. Mar 11 '16 at 07:08
  • Thank you, there I can see how to add info in there. I can put catprize there and dogprize. How does the Listview know which info belongs to what animal then? – Daniel Scholz Mar 11 '16 at 07:10
  • Like a normal multidimensional array. animal[0][10] and animal[1][20] ? or something like that. How does the arrayadapter or listview know which info belongs where. Thats what I dont really get : / – Daniel Scholz Mar 11 '16 at 07:11
  • 1
    You'll have to create a custom adapter, have a look: http://www.vogella.com/tutorials/AndroidListView/article.html – camelCaseCoder Mar 11 '16 at 07:13
  • You can create an Object Class that holds those information. Like for example: private class Animal{ String name; String prize; } Simple enough.. You can do it. :) Try to research around. ;) – AL. Mar 11 '16 at 07:13
  • 1
    Thank you camelCaseCoder, I will try to get it with this =D I think I got the object classes right, with cat and dog being childs of animal, it's the arrayList which I dont get yet, I think. Thank you guys. – Daniel Scholz Mar 11 '16 at 07:16

1 Answers1

0
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // initial the arraylist
        List<Animal> animals = new ArrayList<Animal>() {{
            add(new Dog());
            add(new Cat());
        }};
        // get a list view
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(new MyAdapter(animals));
    }

    // this is your custom adapter
    static class MyAdapter<Animal> extends BaseAdapter {

        private List<Animal> animalList;

        public MyAdapter(List<Animal> animalList) {
            this.animalList = animalList;
        }

        @Override
        public int getCount() {
            return animalList.size();
        }

        @Override
        public Animal getItem(int position) {
            return animalList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Animal animal = getItem(position);
            if(animal instanceof Dog){
                // do dog things
            }else if(animal instanceof Cat){
                // do cat things
            }
            // you might want inflate your custom item view at here
            return null;
        }
    }

    static interface Animal {
        // customize your interface as you wish
    }

    static class Cat implements Animal {

    }

    static class Dog implements Animal {

    }
}
Weizhou He
  • 202
  • 2
  • 11
  • Vielen lieben Dank für dieses Beispiel!! Das hat mir sehr geholfen, für das Verständnis, der Link von oben ebenso. Ich glaub ich habs verstanden :) Hat super funktioniert! – Daniel Scholz Mar 12 '16 at 01:40