This is an old revision of the document!


Laboratorul 05.

LISTS

Lists fara, cu reciclare, tag list, base adapter

Ex 1 Make a new project and do not generate a xml file. If you have generated a xml file, delete it.
Then, add a new xml file named activity_main.xml and make it a LinearLayout with width match_parent and height wrap_content with the orientation horizontal. In it, add a ListView with width and height match_parent

Ex 3 Create another xml file (a LinearLayout ) named list_item.xml. The orientation shoud be horizontal and the width and the height shoud be wrap_content.
In the LinearLayout, add an ImageView(with id iv_image_element) and a TextView(with id tv_name_element), both with width and height wrap_content.


Ex 3 In the MainActivity.java file create another 2 classes (not in class MainActivity): class Car with a String name, and a int imageResource and another class named TagCar with a TextView name and a ImageView image.
In the onCreate function, connect the ListView from the java to the xml (1), and initializate the object carAdapter(2) and set the carAdapter adapter for list of cars (3)

  public class MainActivity extends Activity {
      private ListView listOfCars;
      CarAdapter carAdapter;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          // TODO 1, 2, 3
      }
  }


Ex 4 Create a new java file named CarAdapter which extends BaseAdapter. In it, add an Activity object named context, and an ArrayList of Car named cars
Implement the 4 methods, and in the getView function add this text:

  public View getView(int i, View view, ViewGroup viewGroup) {
      View element;
      
      LayoutInflater layoutInflater = context.getLayoutInflater();
      element = layoutInflater.inflate(R.layout.list_item, null);
      TagCar car = new TagCar();
      car.name = element.findViewById(R.id.tv_name_element);
      car.image = element.findViewById(R.id.iv_image_element);
      element.setTag(car);
          
      TagCar tag = (TagCar) element.getTag();
      tag.name.setText(cars.get(i).name);
      tag.image.setImageResource(cars.get(i).imageResource);
      return element;
  }

Look in Course 4 - List, adapters, recylcing slides 29-31 and tell the teaching assistant if this method is efficient or not (if it's not, make it efficient).

dapm/laboratoare/05.1521808010.txt.gz ยท Last modified: 2018/03/23 14:26 by ioana_maria.culic
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0