我在写一个包含几个简单视图的应用程序;首先是两个按钮。每个按钮都会导向一张地图,并且从该地图上可以看到一个按钮,用于打开网站上的建筑物列表。从这里,用户将能够选择建筑物并查看其中的详细信息(很像实际上的联系人列表)。 到目前为止,我能够通过创建字符串数组,在活动范围内直接填充它,它,像这样以填充ListView:使用文本文件和XML定义的字符串来填充列表
public class BuildingsActivity extends ListActivity {
static final String[] buildings=new String[]{
"Building 1",
"Building 2",
"Building 3",
"Building 4",
"Building 5"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buildings);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,buildings));
}
这工作都很好了5项列表,但在它的最终形式,该列表可能包含多达一百个建筑物,并且我有两个问题: 1)一个一百个项目的长字符串数组在这样的代码中看起来非常粗糙定义2)我担心多长时间如果它必须事先产生如此大的字符串值列表,它将使应用程序打开活动
为了避免这种情况,我有想法来定义这个str在strings.xml文件中创建数组,并将字符串分配给列表活动的相应布局XML中的列表布局。不过,目前这似乎并不奏效;没有错误,但android:entries命令根本不会影响布局。这是XML。如果有人有这种方式填写清单的知识,我会很感激你的答案。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BuildingsActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/buildings">
</ListView>
</RelativeLayout>
我的另一种方法是从一个文本文件,我将有建筑细节做(如文本的体积太大打扰储蓄字符串)检索,但希望避免的名单。无可否认,我还没有深入研究这条路线(因为我还没有真正想使用它,并且基于我在其他论坛上看到的看起来很困难,但是如果对这种方法有很多支持,那么我一定会
请原谅我,如果有任何这已被其他线程覆盖。我DID搜索类似的主题,但没有人似乎想这样填充列表和许多语言从他们的问题在我头上(我很新...这是我第一个真正的应用程序,除了通过开发人员网站上的一些教程。
当你使用'android:entries =“@ array/buildings”'你是否在源代码中更改ListView的适配器? – 2013-02-16 04:20:10
使用android:条目,我根本没有适配器...每个列表项的文本都是在布局中定义的,因此没有适配器用于填充列表。我现在有这个工作,但我不知道如何处理现在不使用适配器的项目选择。 – 2013-02-16 19:29:41