如何在Android的ListView中为菜单项创建图标?

时间:2022-04-12 07:10:38

I am using a ListView to display the main screen of my application.
The main screen is essentially a menu to get into the different sections of application. Currently, I have the ListView whose contents are added programmatically in the onCreate method.

我使用ListView来显示我的应用程序的主屏幕。主屏幕本质上是一个进入应用程序不同部分的菜单。目前,我有ListView,其内容是在onCreate方法中以编程方式添加的。

Here is the code snippet that does this:

以下是执行此操作的代码段:

String[] mainItems = {
    "Inbox", "Projects", "Contexts", "Next Actions"
}

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, mainItems));
    registerForContextMenu(getListView());
}

So the menu is essentially just a bunch of nodes with the text contained in the mainItems array. I know that I can create an XML layout (i.e. R.layout.mainMenu_item) that has an ImageView and TextView in it, but I am unsure how to set the ImageView's icon. I have seen that there is a setImageResouce(int resId) method, but the way to use this when generating with an ArrayAdapter is eluding me. Is there a better way to do this?

所以菜单基本上只是一堆节点,文本包含在mainItems数组中。我知道我可以创建一个XML布局(即R.layout.mainMenu_item),其中包含ImageView和TextView,但我不确定如何设置ImageView的图标。我已经看到有一个setImageResouce(int resId)方法,但是在使用ArrayAdapter生成时使用它的方法让我望而却步。有一个更好的方法吗?

2 个解决方案

#1


16  

What I typically do for a ListView is to implement my own Adapter by extending the handy BaseAdapter class. One of the abstract methods you'll implement will be getView() as the previous poster mentioned. From there you can inflate a layout containing an ImageView, get a reference to it using findViewById, and set the image to whatever drawable you've added into your resources.

我通常为ListView做的是通过扩展方便的BaseAdapter类来实现我自己的Adapter。您将实现的抽象方法之一是getView(),如前面提到的海报。从那里你可以膨胀一个包含ImageView的布局,使用findViewById获取它的引用,并将图像设置为你添加到资源中的任何drawable。

public View getView(int position, View convertView, ViewGroup parent) {

    View row = inflater.inflate(R.layout.menu_row, null);

     ImageView icon = (ImageView) row.findViewById(R.id.icon);
     icon.setImageResource(..your drawable's id...);

     return view;
}

#2


1  

From the google docs for ArrayAdapter.

来自ArrayAdapter的Google文档。

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

要使用TextViews之外的其他内容来显示数组,例如ImageViews,或者除了toString()之外还有一些数据填充视图,覆盖getView(int,View,ViewGroup)以返回所需的视图类型。

#1


16  

What I typically do for a ListView is to implement my own Adapter by extending the handy BaseAdapter class. One of the abstract methods you'll implement will be getView() as the previous poster mentioned. From there you can inflate a layout containing an ImageView, get a reference to it using findViewById, and set the image to whatever drawable you've added into your resources.

我通常为ListView做的是通过扩展方便的BaseAdapter类来实现我自己的Adapter。您将实现的抽象方法之一是getView(),如前面提到的海报。从那里你可以膨胀一个包含ImageView的布局,使用findViewById获取它的引用,并将图像设置为你添加到资源中的任何drawable。

public View getView(int position, View convertView, ViewGroup parent) {

    View row = inflater.inflate(R.layout.menu_row, null);

     ImageView icon = (ImageView) row.findViewById(R.id.icon);
     icon.setImageResource(..your drawable's id...);

     return view;
}

#2


1  

From the google docs for ArrayAdapter.

来自ArrayAdapter的Google文档。

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

要使用TextViews之外的其他内容来显示数组,例如ImageViews,或者除了toString()之外还有一些数据填充视图,覆盖getView(int,View,ViewGroup)以返回所需的视图类型。