gridView

来源:互联网 发布:初级程序员考证 编辑:程序博客网 时间:2024/05/21 08:45
package com.asc.order.product.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asc.R;

/**
 *
 * 类描述  .品牌图片
 * @author fucaihua
 * @version 版本信息 创建时间 2012-6-28 上午09:58:13
 */
public class BreadImageAdapter extends BaseAdapter {
    private List<String> fileList;
     private static final String BRAND_PATH="/sdcard/asc/product/brand";
     ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
     private Context mContext;
        
             public BreadImageAdapter(Context c) {
                 mContext = c;
             }

    @Override
    public int getCount() {
        return getImgFromSDcard().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          View view = View.inflate(mContext, R.layout.breedlist, null);
                              RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.relaGrid);
                      ImageView image = (ImageView) rl.findViewById(R.id.chooseImage);
                      TextView text = (TextView) rl.findViewById(R.id.chooseText);   
                    List<Integer> integers= getImgFromSDcard();
                     Integer [] hh= new Integer[integers.size()];
                      image.setImageResource(hh[position]);
                      text.setText(mTextValues[position]);
                    getImgFromSDcard();
//                      manage
                      return rl;
    }
    
     private Integer[] mThumbIds = {
                             R.drawable.view_gm,R.drawable.view_kl,R.drawable.view_ksf,
                             R.drawable.view_yl,R.drawable.view_yl,R.drawable.view_yl
                     };
                     private String[] mTextValues = {
                                     "Button","TextView","EditText",
                                     "CheckBox"
                     };
                     
                    //获取SD卡上指定目录下的图片
                    public List<Integer> getImgFromSDcard()
                    {
                        List<Integer>   integers=new ArrayList<Integer>();
                            File file = new File(BRAND_PATH);
                               File[] files = file.listFiles();
                               for(int i=0 ;i<files.length ;i++)
                                {
                                       if(files[i].isFile())
                                       {
                                               String filename = files[i].getName();
                                             //获取jpg格式的图片
                                             if(filename.endsWith(".jpg"))
                                              {
                                                     String filePath = files[i].getAbsolutePath();
                                                      fileList.add(filePath);
                                             }
                               }
//                               ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
                               Iterator<String> it = fileList.iterator();
                                while(it.hasNext())
                                {
                                    integers.add(Integer.parseInt(it.next()));
                                }
                                }
                      
                               return integers;
                  }
                    /*
                     * /生成动态数组,并且转入数据  
         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
      
                Iterator<String> it = fileList.iterator();
                 while(it.hasNext())
                 {
                          HashMap<String, Object> map = new HashMap<String, Object>();
                  map.put("ItemImage", it.next());//添加图像资源的ID  
                  lstImageItem.add(map);  
                 }
       
         //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应  
         SimpleAdapter saImageItems = new SimpleAdapter(this,
                                                   lstImageItem,  //数据来源   
                                                   R.layout.imagelayout,  //night_item的XML实现动态数组与ImageItem对应的子项         
                                                   new String[] {"ItemImage"},  
                                                   new int[] {R.id.ItemImage});  
         //添加并且显示  
         gridview.setAdapter(saImageItems);
                     */

}



============================

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

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/hello" />

    <GridView android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:verticalSpacing="10dp"></GridView>
</LinearLayout>


原创粉丝点击