简体   繁体   English

适用于listview的自定义布局的适配器

[英]Adapter for a custom layout of listview

I am developing an android project in which I have to load from group of String arrays(say title,description,id) to the listview item TextView. 我正在开发一个android项目,我必须从String数组(比如title,description,id)加载到listview项TextView。

I did something similar with a database using a cursor like this 我使用像这样的游标对数据库进行了类似的操作

String[] from = new String[]{"medicine","healthsystem"};
            int[] to = new int[] {R.id.textlist1,R.id.textlist2};
             // Now creating an array adapter and set it to display using my row
            SimpleCursorAdapter notes =new SimpleCursorAdapter(this,R.layout.notes_row, c, from, to);

I listed all the targets in "from" and all the origin in "to". 我列出了“from”中的所有目标以及“to”中的所有目标。 Now my problem is I don't have a database so cant use a cursor. 现在我的问题是我没有数据库所以不能使用游标。

I have 3 arrays of strings which i want to load into textviews(title,description,id) of each item 我有3个字符串数组,我想加载到每个项目的textviews(标题,描述,id)

How to do this Please kindly help me out thank you :) 怎么做请帮助我谢谢你:)

If you don't have a cursor, why are you using a SimpleCursorAdapter? 如果您没有光标,为什么使用SimpleCursorAdapter?

Read this article about creating a SimpleListView using SimpleAdapter for alternate ideas. 阅读本文,了解如何使用SimpleAdapter创建SimpleListView以获取其他想法。

在Android SDK附带的ApiDemos项目中有很多来自各种数据源的填充列表的示例 - 浏览这些,你应该找到一个适合你想要做的事情。

First of all you need to create a Class that holds that information, something like: 首先,您需要创建一个包含该信息的类,例如:

public class StringHolder{
    String titte;
    String description;
    int id;
}

Then you create the layout of your row that says where you want your title, your description and your image. 然后,您可以创建行的布局,说明您希望标题,描述和图像的位置。

Then you create an Adapter. 然后创建一个适配器。 The adapter will hold your data and will say for each position in the list what information should be loaded. 适配器将保存您的数据,并将为列表中的每个位置说明应加载哪些信息。

At last you need to use your adapter in an activity. 最后,您需要在活动中使用适配器。

For more information you can see a tutorial here: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ 有关更多信息,请参阅此处的教程: http//www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM