Android - ListView 中的自定义图标
有没有办法为每个组项目放置自定义图标?就像电话一样,我想放置电话,对于住房,我想放置房子。这是我的代码,但它不断抛出警告并锁定我。
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);
我收到的错误:
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)
提前感谢您的帮助!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
equals
来比较字符串。因此,很可能您设置可绘制对象的代码永远不会执行,然后 simpleadapter 尝试将您的字符串作为文件加载,然后您会收到奇怪的错误。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.一个问题是您不应该将字符串值与 == 运算符进行比较,而应使用 .equals 函数。 == 询问两个值是否指向同一个对象。 .equals 询问两个对象在功能上是否等效,即它们是否具有相同的字符串值。
另外,我不确定你想用这个做什么:
它会产生这样的效果:
我怀疑这就是你的意图?始终使用 {} 被认为是一种良好的风格,即使 if 语句中只有一行,这样您就不会遇到这样的问题。
至于错误信息,真的是这样吗? “是一个目录”?看起来它很难读取你的图像......它是否以某种方式损坏,或者在你的可绘制文件中配置错误?
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:
It has the effect if this:
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?