hashmap listView
package com.dev.multicolumn.listview;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.RatingBar;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;public class MultiColumnListView extends Activity { /** Called when the activity is first created. */// I use HashMap arraList which takes objectsprivate ArrayList <HashMap<String, Object>> myBooks;private static final String BOOKKEY = "bookname";private static final String PRICEKEY = "bookprice";private static final String IMGKEY = "iconfromraw";private static final String RATINGKEY = "ratings"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView listView = (ListView)findViewById(R.id.list); myBooks = new ArrayList<HashMap<String,Object>>(); HashMap<String, Object> hm; //With the help of HashMap add Key, Values of Book, like name,price and icon path hm = new HashMap<String, Object>(); hm.put(BOOKKEY, "Android"); hm.put(PRICEKEY, "Price Rs: 500"); hm.put(IMGKEY, R.raw.android); //i have images in res/raw folder hm.put(RATINGKEY, 2); myBooks.add(hm); hm = new HashMap<String, Object>(); hm.put(BOOKKEY, "PHP"); hm.put(PRICEKEY, "Price Rs: 250"); hm.put(IMGKEY, R.raw.php); hm.put(RATINGKEY, 1); myBooks.add(hm); hm = new HashMap<String, Object>(); hm.put(BOOKKEY, "Java"); hm.put(PRICEKEY, "Price Rs: 399"); hm.put(IMGKEY, R.raw.java); hm.put(RATINGKEY,3); myBooks.add(hm); hm = new HashMap<String, Object>(); hm.put(BOOKKEY, "C++"); hm.put(PRICEKEY, "Price Rs: 450"); hm.put(IMGKEY, R.raw.cplusplus); hm.put(RATINGKEY, 2); myBooks.add(hm); // // SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox, //new String[]{BOOKKEY,PRICEKEY,IMGKEY}, new int[]{R.id.text1, R.id.text2, R.id.img}); // ListAdapter adapter = new myListAdapter(myBooks); listView.setAdapter(new myListAdapter(myBooks,this)); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } private class myListAdapter extends BaseAdapter{ private ArrayList<HashMap<String, Object>> Books; private LayoutInflater mInflater; public myListAdapter(ArrayList<HashMap<String, Object>> books, Context context){Books = books;mInflater = LayoutInflater.from(context);} @Overridepublic int getCount() {// TODO Auto-generated method stubreturn Books.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn Books.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub// A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row.ViewHolder holder;// When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null if (convertView == null) { convertView = mInflater.inflate(R.layout.listbox, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.v = (TextView) convertView.findViewById(R.id.text1); holder.v1 = (TextView) convertView.findViewById(R.id.text2); holder.icon = (ImageView) convertView.findViewById(R.id.img); holder.rating = (RatingBar)convertView.findViewById(R.id.star); convertView.setTag(holder); }else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } // Bind the data with the holder. holder.v.setText((String) Books.get(position).get(BOOKKEY));holder.v1.setText((String) Books.get(position).get(PRICEKEY));holder.icon.setImageResource((Integer)Books.get(position).get(IMGKEY));holder.rating.setRating((Integer)Books.get(position).get(RATINGKEY));return convertView;}static class ViewHolder {TextView v; TextView v1; ImageView icon; RatingBar rating; } }}?
?
?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="265dip" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/text1" android:textSize="25dip" android:text="This is text1"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/text2" android:text="This is text2"/> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/ratingBarStyleSmall" android:id="@+id/star" android:numStars="10" android:stepSize="0.1" android:isIndicator="true" /> </LinearLayout> <ImageView android:layout_width="55dip" android:layout_height="fill_parent" android:id="@+id/img" /> </LinearLayout>