Java 在 Android 中显示字符串列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20750118/
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
Displaying list of strings in Android
提问by Sarp Kaya
I started developing for Android yesterday, so I am a complete newbie.
I am using ADT bundle that works with Eclipse.
What I want to do is I have got an array of strings. I would like to display them in a scroll supported view. I found ListView
can do that but all tutorials that I found are extremely complex and explaining nested views.
我昨天开始为 Android 开发,所以我是一个完全的新手。我正在使用适用于 Eclipse 的 ADT 包。我想要做的是我有一个字符串数组。我想在支持滚动的视图中显示它们。我发现ListView
可以做到这一点,但我发现的所有教程都非常复杂并且解释了嵌套视图。
I tried using ListView
but I cannot even see the dummy view.
Here is what I have tried:
activity_main.xml:
我尝试使用,ListView
但我什至看不到虚拟视图。这是我尝试过的:activity_main.xml:
<ListView
android:id="@+id/wifiScanResList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/wifiScanButton" >
</ListView>
in MainActivity Class:
在 MainActivity 类中:
private ListView wifiListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
wifiListView = (ListView) findViewById(R.id.wifiScanResList);
wifiListView.setEnabled(true);
return true;
}
I have got two questions in regards to my problem now.
我现在有两个关于我的问题的问题。
First one is, am I using the right view for my needs? If not what should I use?
第一个是,我是否使用正确的视图来满足我的需求?如果不是我应该使用什么?
Second one is, what is the reason that dummy view does not appear? Also how can I simply add strings into ListView?
第二个是,没有出现虚拟视图的原因是什么?另外,我如何简单地将字符串添加到 ListView 中?
采纳答案by gaurav5430
first you need to create a ArrayList that would store all the strings.
首先,您需要创建一个 ArrayList 来存储所有字符串。
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
then you would need to create an adapter from this arraylist
那么你需要从这个数组列表中创建一个适配器
ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
list);
then you would need to set this adapter on the listview
那么你需要在列表视图上设置这个适配器
listview.setAdapter(adapter);
please see this link for a simple list view implementation:
请参阅此链接以获取简单的列表视图实现:
回答by COLD ICE
Here is my simple way of displaying a list.
这是我显示列表的简单方法。
1 ) Create a class called MySimpleArrayAdapter
1 ) 创建一个名为 MySimpleArrayAdapter 的类
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = null;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.rowlayout, parent, false);
// Displaying a textview
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values.get(position));
return rowView;
}
2) Create an xml file in the layout called rowlayout.xml.We have textview because we want to display items but you could display an item with an image. Use ImageView tag
2) 在布局中创建一个名为 rowlayout.xml 的 xml 文件。我们有 textview 是因为我们想要显示项目,但您可以显示带有图像的项目。使用 ImageView 标签
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30sp" >
</TextView>
</LinearLayout>
3) Back to your main activity xml file (activity_main).We have your wifiscan list
3)回到你的主要活动xml文件(activity_main)。我们有您的 wifiscan 列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/wifiScanResList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/wifiScanButton" >
</ListView>
</LinearLayout>
4) Lastly,in your main activity we set the adapter to your list
4)最后,在您的主要活动中,我们将适配器设置为您的列表
public class MainActivity extends Activity {
// The adapter that we gonna use
MySimpleArrayAdapter adapter;
// List of wifi results
ArrayList<String> wifi_results= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MySimpleArrayAdapter(this, wifi_results);
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// DO something if the user clicked on the item
}
});
}
}