论坛首页 移动开发技术论坛

【Android】说做就做:带图标的list item

浏览 24857 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-01-23  
我用mwjian,Quake Wang的方法实现时是按照以下代码实现的,但在执行时报错java.lang.ClassCastException.时说类型转换错误吧?但我不清楚是什么错误,麻烦各位达人帮忙看一下,多谢!!
共有两个.java文件,
一个是主文件HelloTwoB .java,代码如下:
package com.google.android.hellotwob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;

public class HelloTwoB extends ListActivity {

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setTheme(android.R.style.Theme_Dark);
        setContentView(R.layout.main);
       
 
        //SimpleAdapter demo
        List listitems = fillMaps();
        IconTextAdapter adapter=new IconTextAdapter( this,
        listitems,
        R.layout.list_row,
        new String[]{"icon", "city"},
        new int[]{R.id.icon, R.id.city},
        new int[]{R.drawable.icon_smile, R.drawable.icon_smile},
        R.id.icon );

        setListAdapter(adapter);
    }
   
    private List fillMaps(){
        List items = new ArrayList();
      
        Map i1 = new HashMap();
        i1.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i1.put("city", "Beijing");
        items.add(i1);

        Map i2 = new HashMap();
        i2.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i2.put("city", "Tokyo");
        items.add(i2);
       
        return items;
    }
   
   
}

另一个是文件IconTextAdapter.java,代码如下:
package com.google.android.hellotwob;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

public class IconTextAdapter extends SimpleAdapter {
 
private int[] mIcons; 
private int mIconId; 
      
/**
* @param icons  Icon's id will display.
* @param iconId icon in layout's id
*/ 
public IconTextAdapter( Context context,
List data, 
int resource,
String[] from,
int[] to, 
int[] icons,
int iconId ) { 
    super(context, data, resource, from, to); 
  
    mIcons = icons; 
    mIconId = iconId; 

  
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = super.getView(position, convertView, parent); 
    ImageView img = (ImageView) v.findViewById(mIconId); 
    img.setImageResource(mIcons[position]); 
    return v; 
}
}


xml文件有两个:
一个是main.xml
<?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 id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    />
</LinearLayout>

另一个是:list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView id="@+id/icon"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
<TextView id="@+id/city"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
</LinearLayout>

另外还有一个icon,icon_smail.jpg文件
icon_smile.jpg


还有一个问题,List,Map可以如下这样使用吗?
    private List fillMaps(){
        List items = new ArrayList();
      
        Map i1 = new HashMap();
        i1.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i1.put("city", "Beijing");
        items.add(i1);

        Map i2 = new HashMap();
        i2.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i2.put("city", "Tokyo");
        items.add(i2);
       
        return items;
    }

多谢各位了!!!
0 请登录后投票
   发表时间:2008-04-18  
山水之间 写道
我用mwjian,Quake Wang的方法实现时是按照以下代码实现的,但在执行时报错java.lang.ClassCastException.时说类型转换错误吧?但我不清楚是什么错误,麻烦各位达人帮忙看一下,多谢!!
共有两个.java文件,
一个是主文件HelloTwoB .java,代码如下:
package com.google.android.hellotwob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;

public class HelloTwoB extends ListActivity {

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setTheme(android.R.style.Theme_Dark);
        setContentView(R.layout.main);
       
 
        //SimpleAdapter demo
        List listitems = fillMaps();
        IconTextAdapter adapter=new IconTextAdapter( this,
        listitems,
        R.layout.list_row,
        new String[]{"icon", "city"},
        new int[]{R.id.icon, R.id.city},
        new int[]{R.drawable.icon_smile, R.drawable.icon_smile},
        R.id.icon );

        setListAdapter(adapter);
    }
   
    private List fillMaps(){
        List items = new ArrayList();
      
        Map i1 = new HashMap();
        i1.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i1.put("city", "Beijing");
        items.add(i1);

        Map i2 = new HashMap();
        i2.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i2.put("city", "Tokyo");
        items.add(i2);
       
        return items;
    }
   
   
}

另一个是文件IconTextAdapter.java,代码如下:
package com.google.android.hellotwob;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

public class IconTextAdapter extends SimpleAdapter {
 
private int[] mIcons; 
private int mIconId; 
      
/**
* @param icons  Icon's id will display.
* @param iconId icon in layout's id
*/ 
public IconTextAdapter( Context context,
List data, 
int resource,
String[] from,
int[] to, 
int[] icons,
int iconId ) { 
    super(context, data, resource, from, to); 
  
    mIcons = icons; 
    mIconId = iconId; 

  
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = super.getView(position, convertView, parent); 
    ImageView img = (ImageView) v.findViewById(mIconId); 
    img.setImageResource(mIcons[position]); 
    return v; 
}
}


xml文件有两个:
一个是main.xml
<?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 id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    />
</LinearLayout>

另一个是:list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView id="@+id/icon"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
<TextView id="@+id/city"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          />
</LinearLayout>

另外还有一个icon,icon_smail.jpg文件
icon_smile.jpg


还有一个问题,List,Map可以如下这样使用吗?
    private List fillMaps(){
        List items = new ArrayList();
      
        Map i1 = new HashMap();
        i1.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i1.put("city", "Beijing");
        items.add(i1);

        Map i2 = new HashMap();
        i2.put("icon", getResources().getDrawable(R.drawable.icon_smile));
        i2.put("city", "Tokyo");
        items.add(i2);
       
        return items;
    }

多谢各位了!!!

HashMap j2me好像不支持吧!
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics