首先很抱歉,如果问题标题不清楚,我想不出如何简明扼要地说出来。
我有一个具有基本数据库功能的应用程序,可以将人员的姓名,出生日期和工作类型添加到数据库中。作业类型是从XML资源文件中的字符串数组填充的微调器中选择的;
<string-array name="job_type_array">
<item>Job 1</item>
<item>Job 2</item>
<item>Job 3</item>
...
</string-array>
在数据库中没有存储作业名称,它存储在字符串数组中的位置(即'作业1'保存为整数0等)
当我在listView中显示数据库的内容时,我想显示实际的作业而不是它的位置,但我不知道该怎么做。
我用于listView的代码来自Android Developers示例;
public class PersonList extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
public String[] mTypeArray;
public List typeList;
private PersonDbAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.horses_list);
mDbHelper = new PersonDbAdapter(this);
mDbHelper.open();
mTypeArray = getResources().getStringArray(R.array.job_type_array);
typeList = Arrays.asList(mTypeArray);
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
// Get all of the rows from the database and create the item list
Cursor personCursor = mDbHelper.fetchAllPeople();
startManagingCursor(personCursor);
// Create an array to specify the fields we want to display in the list (only Name and job)
String[] from = new String[]{PersonDbAdapter.KEY_NAME, PersonDbAdapter.KEY_TYPE};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.rowPersonName, R.id.rowPersonJobType};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter people =
new SimpleCursorAdapter(this, R.layout.people_row, peopleCursor, from, to);
setListAdapter(people);
}
游标fetchAllPeople只获取数据库中每个人的所有数据。
我的问题是;如何显示实际作业而不仅仅是存储在数据库中的位置。任何帮助将不胜感激。
编辑:加密代码
答案 0 :(得分:1)
我假设您要放置职位的行布局中的TextView
(?!?)是ID为R.id.rowHorseType
的视图(在to
数组中,我还假设PersonDbAdapter.KEY_TYPE
表示带有job int值的游标中的列。在这种情况下,您将使用SimpleCursorAdapter.ViewBinder
将存储在数据库中的作业int
值转换为实际的作业标题,如下所示:
people.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
if (view.getId() == R.id.rowHorseType) {
int jobInt = cursor.getInt(columnIndex);
((TextView) view).setText(mTypeArray[jobInt]); // if the view with the id R.id.rowHorseType is a TextView!!!
return true;
}
return false;
}
});
答案 1 :(得分:0)
如果你想从你应该使用的数据库中获取数据,我没有得到你的正确使用:
Cursor previous_repotData = getdataBaseRecord();
Stirng stdate = getString(previous_repotData.getColumnIndexOrThrow("startDate")));