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 objects
	private 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);
		}
    	
    	
    	@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return Books.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return Books.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public 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>

 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/list" /> </LinearLayout>


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值