Adapter for a custom layout of listview

2019-03-03 01:37发布

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.

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". 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

How to do this Please kindly help me out thank you :)

3条回答
一夜七次
2楼-- · 2019-03-03 01:54

If you don't have a cursor, why are you using a SimpleCursorAdapter?

Read this article about creating a SimpleListView using SimpleAdapter for alternate ideas.

查看更多
等我变得足够好
3楼-- · 2019-03-03 01:55

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/

查看更多
时光不老,我们不散
4楼-- · 2019-03-03 01:56

There are lots of examples of populating lists from various data sources in the ApiDemos project that comes with the Android SDK - browse through those, and you should find one that fits what you're trying to do.

查看更多
登录 后发表回答