Lab 05 - Lists

LISTS

In android, an adapter is a bridge between UI component and data source that helps us to fill data in the UI component. It holds the data and send the data to adapter view then view can takes the data from the adapter view and shows the data on different views like as list view, grid view, spinner etc

BaseAdapter methods:
1. getCount(): The getCount() function returns the total number of items to be displayed in a list. It counts the value from array list size() method or an array’s length.

2. getView(int i, View view, ViewGroup viewGroup): This function is automatically called when the list item view is ready to be displayed or about to be displayed. In this function we set the layout for list items using LayoutInflater class and then add the data to the views like ImageView, TextView etc

3. getItem(int i): This function is used to Get the data item associated with the specified position in the data set to obtain the corresponding data of the specific location in the collection of data items.

4. getItemId(int i): As for the getItemId (int position), it returns the corresponding to the position item ID. The function returns a long value of item position to the adapter.

Exercises :

Ex 1 Create a new project and follow the next tutorial http://abhiandroid.com/ui/baseadapter-tutorial-example.html.

Ex 2 Make a new project and do not generate a XML file.
Add the next image into the project

Ex 3 (If you have generated a xml file in the ex 0, delete it) 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 vertical. In it, add a ListView with width and height match_parent and an id android:id=”@+id/lv_list_cars”

Ex 4 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 5 In the MainActivity.java file create another 2 classes (not in class MainActivity): class Car with a String name, and an int imageResource and another class named TagCar with a TextView name and an ImageView image.

Ex 6 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.
Create a constructor which receive an Activity and initialize the context, and the cars ArrayList, also add the next function addCar:

  Activity context;
  ArrayList<Car> cars;
  
  public CarAdapter (Activity context) {
     //TODO
  }
  
  public void addCar(String nameCar, int resource){
      Car car = new Car();
      car.name = nameCar;
      car.imageResource = resource;
      cars.add(car);
      this.notifyDataSetChanged();
  }
  


Ex 7 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, recycling slides and tell the teaching assistant if this method is efficient or not (if it's not, make it efficient).

Ex 8 In the MainActivity class, in the onCreate function, connect the ListView from the java to the xml (TODO 1)
TODO 2: initializate the object carAdapter(2)
TODO 3: set the carAdapter adapter for list of cars (3).
TODO 4: in the carAdapter, add 4 cars (with addCar function).

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


RUN THE APP
Ex 9 In the activity_main.xml, in the linearLayout , add another LinearLayout (width match parent and height wrap_content, orientation horizontal) and add in it an EditText (with id ed_new_car, height wrap_content and width 200dp) and a Button (id b_add_car, width and height wrap_content)

Ex 10 In MainActivity, in onCreate function, connect the EditText and the Button from the java to the XML file. Set an onClickListener on the button: when clicked, get the Text from the editText and convert it to String, and add a new car in the carAdapter using the name from the editText. After that, set the editText's text to ””.

mdad/laboratoare/05.txt · Last modified: 2021/01/12 11:15 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