如何从方法中获取数据并放入ListView?

时间:2012-10-26 02:50:40

标签: android

我有这堂课:

public class Bookmark extends ArrayList<Bkm> {

    public static Bookmark getBookmark(Context context) {
        Bookmark bookmarks = new Bookmark ();

        String[] titles  = context.getResources().getStringArray(R.array.bookmark_titles);
        String[] urls    = context.getResources().getStringArray(R.array.bookmark_urls);
        TypedArray icons = context.getResources().obtainTypedArray(R.array.bookmark_icons);

        for (int i = 0; i < titles.length; i ++) {
            bookmarks.add(titles[i], urls[i], icons.getDrawable(i));
        }

        return bookmarks;
    }
}

该类具有“getBookmark”方法,该方法返回“书签”对象,它包含字段“titles”,“urls”和“icons”。我怎样才能在主课上获得这些字段?我想创建一个带有“titles”项的ListView,并在WebView中访问相应的URL。

在我的主要课程中,我有这个ListView

ListView lv = (ListView) findViewById(R.id.favoritos_listView);

我试图以那种方式获取数据

Context context = getApplicationContext();        
ArrayList<Bookmark> my_array = BookmarkCollection.getTestBookmarks(context);
ArrayAdapter<Bookmark> aa = new ArrayAdapter<Bookmark>(context, R.array.bookmark_titles, android.R.layout.simple_list_item_1, my_array);
ListView lv = (ListView) findViewById(R.id.favoritos_listView);
lv.setAdapter(aa);

但ListView没有出现。

1 个答案:

答案 0 :(得分:1)

Boomark无需延长ArrayList。您需要做的是为Bookmark对象创建一个自定义适配器,并在ListView上使用该适配器。

首先创建您的Bookmark模型类。

public class Bookmark {

    private String title;
    private String url;
    private Drawable icon;

    public Bookmark(String title, String url, Drawable icon) {

        this.title = title;
        this.url = url;
        this.icon = icon;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Drawable getIcon() {
        return icon;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }
}

然后创建书签适配器

public class BookmarkAdapter extends BaseAdapter{

    private ArrayList<Bookmark> bookmarks;
    private Context context;

    public BookmarkAdapter(Context context, ArrayList<Bookmark> bookmarks) {

        this.context = context;
        this.bookmarks = bookmarks;
    }

    public int getCount() {

        return bookmarks.size();
    }

    public Object getItem(int position) {

        return bookmarks.get(position);
    }

    public long getItemId(int arg0) {

        return 0;
    }

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

         View row = convertView;
           if ( row == null ) {

               row = View.inflate( context, R.layout.row_bookmark, null );
           }

           Bookmark bookmark = (Bookmark)getItem(position);

           if (  bookmark!= null ) {

               TextView name = (TextView) row.findViewById( R.id.title );

               if ( name != null ) {

                   name.setText( bookmark.getTitle() );
               }
           }

           return row;
    }
}

row_bookmark.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

将它放在Activity xml

<ListView
    android:id="@+id/bookmark_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

填充活动中的列表

   String[] titles  = getResources().getStringArray(R.array.bookmark_titles);
   String[] urls    = getResources().getStringArray(R.array.bookmark_urls);
   TypedArray icons = getResources().obtainTypedArray(R.array.bookmark_icons);

   ArrayList<Bookmark> bookmarks = new ArrayList<Bookmark>();

   for (int i = 0; i < titles.length; i ++) {

       bookmarks.add(new Bookmark(titles[i], urls[i], icons.getDrawable(i)));
   }

   ListView bookmarkList = (ListView)findViewById(R.id.bookmark_list);
   bookmarkList.setAdapter(new BookmarkAdapter(this, bookmarks));

要在选择列表项时获取URL,您需要设置项目单击侦听器

bookmarkList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String url = ((Bookmark)parent.getAdapter().getItem(position)).getUrl();
        }
    });