Tuesday, August 11, 2015

Basic ListActivity No Layout With HashMap


.

Basic ListActivity No Layout With HashMap
Using Android Studio 1.4

1) Create New Project

Application Name: MyListActivityNoLayoutHashMap1
Select API 14:Android 4.0 (IceCreamSandwich)
Follow the wizard to create New Empty Activity.
Deselect “Generate Layout File”
We do not require ListView Layout as List Activity has a built-in layout.
However, we need to define a Row Layout and Custom Adapter in order to implement HashMap structure

2) Add Row Layout and Drawables

2.1) res/layout/row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <ImageView
       android:id="@+id/logo"
       android:layout_width="55dp"
       android:layout_height="70dp"
       android:layout_alignParentLeft="true"
       android:scaleType="fitXY"/>
   <TextView
       android:id="@+id/name"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_toRightOf="@id/logo"
       android:textSize="18sp"/>
   <TextView
       android:id="@+id/region"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_toRightOf="@id/logo"
       android:layout_below="@id/name"/>
</RelativeLayout>

2.2) Drawables

Download MyListActivityNoLayoutHashMap1-drawable.zip and place the images into res/drawable folder.

3) Edit MainActivity.java

package com.notarazi.mylistactivitynolayouthashmap1;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends ListActivity {
   //the ArrayList that will hold the data to be displayed in the ListView
   ArrayList<HashMap<String, Object>> companies;
   LayoutInflater inflater;
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //not necessary as ListActivity has an
       //implicitly defined Layout(with a ListView of course)
       //setContentView(R.layout.main);
       //get the LayoutInflater for inflating the customomView
       inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       //these arrays are just the data that
       //I'll be using to populate the ArrayList
       //You can use our own methods to get the data
       String names[] = {"Toyota""Honda""Hyundai""Kia","Proton""Mercedes","Lada""Skoda",};
       String regions[] = {"Asia""Asia""Asia""Asia""Asia""Europe""Europe","Europe"};
       Integer[] logos = {R.drawable.car_toyota, R.drawable.car_honda, R.drawable.car_hyundai, R.drawable.car_kia,
               R.drawable.car_proton, R.drawable.car_mercedes,R.drawable.car_lada, R.drawable.car_skoda};
       ArrayList companies = new ArrayList<HashMap<String, Object>>();
       //HashMap for storing a single row
       HashMap<String, Object> temp;
       //total number of rows in the ListView
       int noOfcompanies = names.length;
       //now populate the ArrayList companies
       for (int i = 0; i < noOfcompanies; i++) {
           temp = new HashMap<String, Object>();
           temp.put("name", names[i]);
           temp.put("region", regions[i]);
           temp.put("logo", logos[i]);
           //add the row to the ArrayList
           companies.add(temp);
       }
       //create the adapter
       //first param-the context
       //second param-the id of the layout file
       //you will be using to fill a row
       //third param-the set of values that
       //             will populate the ListView
       CustomAdapter adapter = new CustomAdapter(this, R.layout.row_layout, companies);
       //finally,set the adapter to the default ListView
       setListAdapter(adapter);
   }
   //define your custom adapter
   private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
   {
       public CustomAdapter(Context context, int textViewResourceId,
                            ArrayList<HashMap<String, Object>> Strings) {
           //let android do the initializing :)
           super(context, textViewResourceId, Strings);
       }
       //class for caching the views in a row
       private class ViewHolder
       {
           ImageView logo;
           TextView name,region;
       }
       ViewHolder viewHolder;
       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
           if(convertView==null)
           {
               //inflate the custom layout
               convertView=inflater.inflate(R.layout.row_layoutnull);
               viewHolder=new ViewHolder();
               //cache the views
               viewHolder.logo=(ImageView) convertView.findViewById(R.id.logo);
               viewHolder.name=(TextView) convertView.findViewById(R.id.name);
               viewHolder.region=(TextView) convertView.findViewById(R.id.region);
               //link the cached views to the convertview
               convertView.setTag(viewHolder);
           }
           else
               viewHolder=(ViewHolder) convertView.getTag();
           int logoId=(Integer) companies.get(position).get("logo");
           //set the data to be displayed
           viewHolder.logo.setImageDrawable(getResources().getDrawable(logoId));
           viewHolder.name.setText(companies.get(position).get("name").toString());
           viewHolder.region.setText(companies.get(position).get("region").toString());
           //return the view to be displayed
           return convertView;
       }
   }
}
What is Map and Hash Map?
OUTCOME.

DOWNLOAD

REFERENCES

What is Map and Hash Map?

.

No comments:

Post a Comment