是否有一个超级简单的 List / ListAdapter 示例适用于 android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2688193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a super simple List / ListAdapter example out there for android
提问by slim
I have a web service that returns a super simple list of objects
我有一个 Web 服务,它返回一个超级简单的对象列表
MyObject[] data = webServiceCall();
MyObject[] 数据 = webServiceCall();
MyObject has 1 field i want to display, "Name"
(i.e. data[0].Name
)
MyObject 有 1 个字段我想显示,"Name"
(即data[0].Name
)
How can i turn this into an activity that lists just the name of these objects in a scrollable listActivity
on Android. I am getting really confused with Cursors and am not sure if I need Cursors and I"m not sure what kind of adapter to implement (BaseAdapter
, SimpleAdapter
etc)
我怎样才能把它变成一个活动,listActivity
在 Android 上的可滚动中只列出这些对象的名称。我变得非常混淆光标和肯定我不是,如果我需要游标和我“米不知道那种适配器什么来实现(BaseAdapter
,SimpleAdapter
等等)
So i guess i'm looking for three things,
所以我想我在寻找三件事,
the activity, the adapter and the layout.xml
活动、适配器和 layout.xml
Just trying to figure this android stuff out, definitely a noob here
只是想弄清楚这个安卓的东西,这里绝对是个菜鸟
回答by slim
so i think i figured it out with a little inspiration from RobGThai's answer, i'm posting the code for anyone else to see, i guess i didn't really use a custom adapter
所以我想我从 RobGThai 的回答中得到了一点灵感,我正在发布代码供其他人查看,我想我并没有真正使用自定义适配器
This is the super simple example that got me started, so once i had this, I made sure my "MyObject" had a toString() method on it to show properly in the list and i passed the MyObject[] array into the "new ArrayAdapter" constructor instead of listItems
这是让我开始的超级简单的例子,所以一旦我有了这个,我确保我的“MyObject”有一个 toString() 方法可以在列表中正确显示,我将 MyObject[] 数组传递到“new ArrayAdapter" 构造函数而不是 listItems
FooList.java
FooList.java
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class FooList extends ListActivity {
String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
}
}
the layout xml i used (temp.xml)
我使用的布局 xml (temp.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty set" />
</LinearLayout>
回答by RobGThai
make your Activity extends ListActivity as well.
让您的 Activity 也扩展 ListActivity 。
Then this should help you get started.
那么这应该可以帮助您入门。
Object[] sArray = {"This", "is", 3.5, true, 2, "for", "bla"};
ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray);
setListAdapter(adp);
The second parameters can be change to your preferred layout. See API document for further information.
第二个参数可以更改为您喜欢的布局。有关更多信息,请参阅 API 文档。
回答by barryku
You can also attach an AlertDialog
to the ListView
when user clicks on a ListItem
. I am including a working code below in case anyone is trying the same thing,
当用户单击 时,您还可以将 附加AlertDialog
到。我在下面包含了一个工作代码,以防有人尝试同样的事情,ListView
ListItem
final ListView v = getListView();
v.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parentView, View view, int position,
long id) {
final String fileName = (String) parentView.getItemAtPosition(position);
new AlertDialog.Builder(parentView.getContext()).setTitle("Upload").setMessage("Upload " + fileName +"?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int arg1) {
//do something here...
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface di, int arg1) {
di.dismiss();
}
}).show();
}
});
The key is that you need to pass your enclosing activity's context to the dialog. I found that out when getApplicationContext()
failed with a strange error message.
关键是您需要将封闭活动的上下文传递给对话框。我发现getApplicationContext()
失败时出现了奇怪的错误消息。