Android 如何从方法中获取数据并将其放入ListView?
我有这门课:Android 如何从方法中获取数据并将其放入ListView?,android,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_ti
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;
}
}
我正试图通过这种方式获取数据
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);
Context-Context=getApplicationContext();
ArrayList my_array=BookmarkCollection.getTestBookmarks(上下文);
ArrayAdapter aa=新的ArrayAdapter(上下文,R.array.bookmark\u标题,android.R.layout.simple\u列表\u项目\u 1,我的\u数组);
ListView lv=(ListView)findViewById(R.id.favoritos\u ListView);
低压设置适配器(aa);
但是列表视图不会出现。
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;
}
}
将其放入您的活动
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));
String[]titles=getResources().getStringArray(R.array.bookmark\u titles);
字符串[]URL=getResources().getStringArray(R.array.bookmark\u URL);
TypedArray icons=getResources().obtainTypedArray(R.array.bookmark_icons);
ArrayList bookmarks=新建ArrayList();
对于(int i=0;i
要在选择列表项时获取URL,需要设置该项,然后单击listener
bookmarkList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = ((Bookmark)parent.getAdapter().getItem(position)).getUrl();
}
});
bookmarkList.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串url=((书签)parent.getAdapter().getItem(position)).getUrl();
}
});
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;
}
}
将其放入您的活动
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));
String[]titles=getResources().getStringArray(R.array.bookmark\u titles);
字符串[]URL=getResources().getStringArray(R.array.bookmark\u URL);
TypedArray icons=getResources().obtainTypedArray(R.array.bookmark_icons);
ArrayList bookmarks=新建ArrayList();
对于(int i=0;i
要在选择列表项时获取URL,需要设置该项,然后单击listener
bookmarkList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = ((Bookmark)parent.getAdapter().getItem(position)).getUrl();
}
});
bookmarkList.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串url=((书签)parent.getAdapter().getItem(position)).getUrl();
}
});
感谢您的精彩回答。我已经有书签课了。还有一个问题。我已经有了getBookmark()方法,它已经执行了填充活动中列表的操作。我怎么能在我的主类中简单地调用这个方法呢?你可以使用bookmarkList.setAdapter(新的bookmarkapter(this,Bookmark.getBookmark(this));
谢谢你的回答。我已经有了Bookmark类。还有一个问题。我已经有了getBookmark()方法,该方法已执行填充活动中列表的操作。我如何在主类中调用此方法?您可以使用bookmarkList.setAdapter(新bookmarkapter(this,Bookmark.getBookmark(this));