2012-10-26 60 views
1

我有這個類:如何從方法獲取數據並放入ListView?

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」的方法,它包含的字段「標題」,「網址」和「圖標」。我如何在我的主課堂中獲得這些字段?我想創建一個帶有「標題」項目的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

Boomark不需要延長ArrayList。您需要做的是爲您的書籤對象創建一個自定義適配器,並在您的ListView上使用該適配器。

首先創建您的書籤模型類。

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> 

填充列表我n您的活動

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(); 
     } 
    }); 
+0

謝謝你的偉大的答案。我已經有了Bookmark類。還有一個問題。我已經有了getBookmark()方法,它已經完成了在我的活動中填充列表的動作。我怎樣才能在我的主類中調用這個方法? – androidstudent

+0

您可以使用'bookmarkList.setAdapter(new BookmarkAdapter(this,Bookmark.getBookmark(this));' –

+0

再次感謝您。 – androidstudent

相關問題