首先介绍一下我为什么要问这个问题的背景......
我正在为一家机械车间开发一个日程安排应用程序。我有一个包含机器、工作和员工表的数据库。可能并非所有这些机器都有工作或运算符(operator),所以我需要一种方法来将“无”添加到微调器(有几个原因我不会讨论不将其添加到数据库本身)。为了实现这一点,我决定从数据库中获取游标并对其进行迭代,将我需要的那些位加载到 ArrayList 中,并在末尾添加一个“无”元素。
这工作得很好,但现在我遇到了一个我似乎无法弄清楚的症结点。
作业表有两列(不包括索引):作业编号和零件编号。我创建了一个自定义布局以将它们放在一起,然后在微调器中使用它(因此您会在微调器的每一行上看到 Job # - Part #)。从游标填充它很简单。试图将其转换为两个数组让我感到难过。
我可以毫无问题地将数据从游标获取到两个数组中,但我不知道如何将它们与布局中包含的布局 ID 相关联。
我是 android 的新手,很久以前就学过 Java,所以如果我遗漏了一些明显的东西,请指出!
liSTLayoutdouble.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="12dip"
android:gravity="left|center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/ListItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="12dip"
android:gravity="right|center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
我目前拥有的:
private void fillJobSpinner() {
Cursor jobsCursor = mDBHelper.fetchAllJobs();
startManagingCursor(jobsCursor);
ArrayList<String> jobnumbers = new ArrayList<String>();
ArrayList<String> jobnparts = new ArrayList<String>();
if (jobsCursor != null) {
operatorsCursor.moveToFirst();
while (operatorsCursor.isAfterLast() == false) {
jobnumbers.add(jobsCursor.getString(jobsCursor
.getColumnIndex(ScheduleDBAdapter.JOB_NUMBER)));
jobnparts.add(jobsCursor.getString(jobsCursor
.getColumnIndex(ScheduleDBAdapter.JOB_PART)));
jobsCursor.moveToNext();
}
jobnumbers.add("None");
jobparts.add("None");
}
}
最佳答案
我认为您需要为您的微调器定制一个适配器。
你需要扩展BaseAdapter
http://developer.android.com/reference/android/widget/BaseAdapter.html
然后覆盖getView方法。
快速谷歌一下发现了这个教程:
http://app-solut.com/blog/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/
关于android - 可以使用两个 ArrayLists 来填充一个带有自定义布局的微调器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9055643/