安卓:ListView中,ArrayAdapter和二维数组 [英] Android: ListView, ArrayAdapter and 2-d array
问题描述
我需要在一个列表视图显示从2-D字符串数组内容(最有可能只是一个索引)。
I need to display contents (most likely just one index) from a 2-d string array in a listview.
我可以为一维数组这样创建arrayadapter:
I can create an arrayadapter for a 1-d array like this:
String[] values ... blah-blah
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
这个工程只是花花公子。但是,如果我这样做:
This works just dandy. However, if I do this:
String[][] values ... blah-blah
ArrayAdapter<String[]> adapter = new ArrayAdapter<String[]>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
这,自然会显示阵列中的阵列,可以这么说,而不是内容。
This, naturally, will display the arrays in the array, so to speak, and not the contents.
现在的问题是,我如何显示f.ex.在ListView的2-D阵列的第一或第二索引
The question is, how to I display f.ex. the first or second index of the 2-d array in the ListView?
在细节:
2-D数组的内容:
3,03-09-2012,text,moretext
2,03-09-2012,text,moretext
1,03-09-2012,text,moretext
我想f.ex.显示指数[Ⅰ] [2]在列表中。
I'd like to display f.ex. index[i][2] in the list.
我不一定需要显示每个阵列的所有指标,但我需要能够访问它们。由于每个阵列包含多个条目,其中包括一个ID,我不能只是转换我想显示为1-D阵列的信息,从此我会失去每个阵列的ID。
I don't necessarily need to display all the indexes of each array, but I need to have access to them. Since each array contains several entries, among others an ID, I cannot just convert the information I want to display to a 1-d array, since then I'd lose the ID of each array.
推荐答案
有两种解决方案供您选择:
There are two solutions for you to choose from:
- 创建是一种把二维数组简单数组,用你自己的逻辑transfomation的方法。
- 延长
BaseAdapter
类来创建自己的实现,在那里你可以有一个二维数组作为基础数据,并在您希望的方式显示其内容。
- Create a method that transforms a 2D array to a simple array, using your own transfomation logic.
- Extend the
BaseAdapter
class to create your own implementation, where you can have a 2D array as the underlying data and display its contents in a way you want.
希望这有助于。
这篇关于安卓:ListView中,ArrayAdapter和二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!