开发者

Android - Custom Icons in ListView

Is there any way to place a custom icon for each group item? Like for phone I'd like to place a phone, for housing I'd like to place a house. Here is my code, but it keeps throwing a Warning and locks up on me.

ListView myList = (ListView) findViewById(R.id.myList);
                //ExpandableListAdapter adapter = new MyExpandableListAdapter(data);
                List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
               // List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, String>>>();

                Iterator it = data.entrySet().iterator();
                while (it.hasNext()) 
                {
                    //Get the key name and value for it
                    Map.Entry pair = (Map.Entry)it.next();
                    String keyName = (String) pair.getKey();
                    String value = pair.getValue().toString();

                    //Add the parents -- aka main categories
                    Map<String, Object> curGroupMap = new HashMap<String, Object>();
                    groupData.add(curGroupMap);
                    Log.i("VAL", keyName);
                    if (keyName.equalsIgnoreCase("Phone"))
                        curGroupMap.put("ICON", findViewById(R.drawable.phone_icon));
                    else if (keyName.equalsIgnoreCase("Housing"))
                        curGroupMap.put("ICON", findViewById(R.drawable.house_icon));
                    else
                        curGroupMap.put("ICON", findViewById(R.drawable.house_icon));

                    curGroupMap.put("NAME", keyName);
                    curGroupMap.put("VALUE", value);


                }

 开发者_开发技巧               // Set up our adapter
                mAdapter = new SimpleAdapter(
                        mContext,
                        groupData,
                        R.layout.exp_list_parent,
                        new String[] { "ICON", "NAME", "VALUE" },
                        new int[] { R.id.photoAlbumImg, R.id.rowText1, R.id.rowText2  }
                        );

                myList.setAdapter(mAdapter);

The error i'm getting:

    05-28 17:36:21.738: WARN/System.err(494): java.io.IOException: Is a directory
05-28 17:36:21.809: WARN/System.err(494):     at org.apache.harmony.luni.platform.OSFileSystem.readImpl(Native Method)
05-28 17:36:21.838: WARN/System.err(494):     at org.apache.harmony.luni.platform.OSFileSystem.read(OSFileSystem.java:158)
05-28 17:36:21.851: WARN/System.err(494):     at java.io.FileInputStream.read(FileInputStream.java:319)
05-28 17:36:21.879: WARN/System.err(494):     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:183)
05-28 17:36:21.908: WARN/System.err(494):     at java.io.BufferedInputStream.read(BufferedInputStream.java:346)
05-28 17:36:21.918: WARN/System.err(494):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-28 17:36:21.937: WARN/System.err(494):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
05-28 17:36:21.948: WARN/System.err(494):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
05-28 17:36:21.958: WARN/System.err(494):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296)
05-28 17:36:21.978: WARN/System.err(494):     at android.graphics.drawable.Drawable.createFromPath(Drawable.java:801)
05-28 17:36:21.988: WARN/System.err(494):     at android.widget.ImageView.resolveUri(ImageView.java:501)
05-28 17:36:21.998: WARN/System.err(494):     at android.widget.ImageView.setImageURI(ImageView.java:289)

Thanks in advance for your help!!


if (value == "Phone")

You need to use equals to compare strings. Most probably your code to set the drawable is thus never executed, and then the simpleadapter tries to load your string as a file, and you get your weird error.


One problem is that you shouldn't compare string values with the == operator, instead use the .equals function. == asks if the two values point to the same object. .equals asks if the two objects are functionally equivalent, i.e. do they have the same string value.

Also, I'm not sure what you are trying to do with this:

else if (value == "Housing")
curGroupMap.put("NAME", keyName);
curGroupMap.put("VALUE", value);

It has the effect if this:

else if (value == "Housing") {
    curGroupMap.put("NAME", keyName);
}
curGroupMap.put("VALUE", value);

I doubt that is what you intended? It is considered good style to always use the {}, even if there is only one line in the if statement so you don't run into problems like this.

As to the error message, is that realy what is says? "Is a directory"? It seems like it is having a hard time reading your image... Is it somehow corrupt, or misconfigured in your drawable file?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜