首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >列表视图中的不同图标

列表视图中的不同图标
EN

Stack Overflow用户
提问于 2012-10-11 19:34:05
回答 4查看 23.4K关注 0票数 14

我必须创建一个列表视图,每个项目在右边都有一个单独的图标。我已经写了一段代码,但它不能工作?我该如何继续?应用程序崩溃

代码

代码语言:javascript
运行
复制
public class LoginMenu extends ListActivity
{

TextView maintext, subtext;
QuickContactBadge icon;

private static final String[] menuitems = { "Availability", "Messages",
"Greetings", "Address Book", "Calls", "Settings" };

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
maintext = (TextView) findViewById(R.id.tvMainText);
subtext = (TextView) findViewById(R.id.tvSubText);
setListAdapter(new IconicAdapter());    
}    
class IconicAdapter extends ArrayAdapter<String>
{    
public IconicAdapter()
{
super(LoginMenu.this, R.layout.menu, R.id.tvMainText, menuitems);
// TODO Auto-generated constructor stub
}   
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View row = super.getView(position, convertView, parent);
icon = (QuickContactBadge) findViewById(R.id.iContactBadge);
if (menuitems[0] != null)
{
icon.setImageResource(R.drawable.menu_availability);
}
else if (menuitems[1] != null)
{
icon.setImageResource(R.drawable.menu_messages);
}    
else if (menuitems[2] != null)
{
icon.setImageResource(R.drawable.menu_greetings);
}
else if (menuitems[3] != null)
{
icon.setImageResource(R.drawable.menu_contacts);    
}
else if (menuitems[4] != null)
{
icon.setImageResource(R.drawable.menu_calls);
}
else if (menuitems[5] != null)
{
icon.setImageResource(R.drawable.menu_settings);
}
return (row);
}    
}
}

图像我的XML文件包含一个图像、文本视图和另一个根据下图排列的子文本

LOGCAT

代码语言:javascript
运行
复制
10-11 04:46:15.088: E/AndroidRuntime(726): FATAL EXCEPTION: main
10-11 04:46:15.088: E/AndroidRuntime(726): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.avst.callxpressmobile/com.example.avst.callxpressmobile.MenuScreen}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.os.Looper.loop(Looper.java:137)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-11 04:46:15.088: E/AndroidRuntime(726):  at java.lang.reflect.Method.invokeNative(Native Method)
10-11 04:46:15.088: E/AndroidRuntime(726):  at java.lang.reflect.Method.invoke(Method.java:511)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-11 04:46:15.088: E/AndroidRuntime(726):  at dalvik.system.NativeStart.main(Native Method)
10-11 04:46:15.088: E/AndroidRuntime(726): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.Activity.setContentView(Activity.java:1867)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.example.avst.callxpressmobile.MenuScreen.onCreate(MenuScreen.java:22)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.Activity.performCreate(Activity.java:5008)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-11 04:46:15.088: E/AndroidRuntime(726):  ... 11 more
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-10-11 19:45:48

你可以在这里获得简单的代码,它在列表的每一行中显示一个图像和一些文本:

http://www.mkyong.com/android/android-listview-example/

但是,如果行数很高,并且要从服务器获取每行中的图像,那么您最好使用以下代码:

https://github.com/thest1/LazyList

票数 11
EN

Stack Overflow用户

发布于 2012-10-11 19:54:26

只需使用包含行详细信息(图像、两个文本)的HashMap<String,String>即可。请参考此link,它将帮助您实现您想要的内容。

票数 6
EN

Stack Overflow用户

发布于 2012-10-11 19:37:47

下面的一些链接对你根据需要编写相同的代码很有用……

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

http://wptrafficanalyzer.in/blog/listview-with-images-and-text-using-simple-adapter-in-android/

http://android-example-code.blogspot.in/p/dynamic-custoized-list-view-in-android.html

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

代码语言:javascript
运行
复制
   if (menuitems[0] != null) {
              if(menuitems[0].equalsIgnoreCase("Availability")
                {
                    icon.setImageResource(R.drawable.menu_availability);
                 // Set your another textview and another subtext here....
                 }
         } 
  // Set another conditions also as per above code

谢谢

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12838575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档