0
ID| TOPIC | TITLE | TYPE | NAME |
---------------------------------
1 | AB    | BCD   | ref  | Ferari|
----------------------------------
1 | AB    | BCD   | ref  | TOYOTA|
----------------------------------
1 | AB    | BCD   | ref| AUDI |
----------------------------------
1 | AB    | BCD    | ref| BMW  |
---------------------------------
2 | BC    | ABC   | ref  | NISSAN|
----------------------------------
2 | BC    | ABC   | ref  | SUZKI|
----------------------------------
2 | BC    | ABC   | ref| TATA |

光标保存像这张表一样的数据。ID| TOPIC | TITLE | TYPE | NAME |现在,我想NAME根据ID. 就像ID1 K 会FERARI,TOYOTA,AUDI,BMW等等。我想customlistview连续显示这些信息。

我的问题是

有什么方法可以将名称数据存储在字符串数组中,或者您对我有什么替代建议

4

2 回答 2

0

如果我正确理解您的问题,您想将数据库表中的值存储在数组中吗?为此,您可以创建并行可调整大小的通用列表,如下所示:

ArrayList<int> ids = new ArrayList<int>(); 
ArrayList<String> topics = new ArrayList<String>(); 
ArrayList<String> titles = new ArrayList<String>(); 
ArrayList<String> types = new ArrayList<String>(); 
ArrayList<String> names = new ArrayList<String>(); 

然后您可以像这样向其中添加项目:

ids.add(cursor.getInt(cursor.getColumnIndexOrThrow("_id")));
topics.add(cursor.getString(cursor.getColumnIndexOrThrow("TOPIC")));
titles.add(cursor.getString(cursor.getColumnIndexOrThrow("TITLE")));
types.add(cursor.getString(cursor.getColumnIndexOrThrow("TYPE")));
names.add(cursor.getString(cursor.getColumnIndexOrThrow("NAME")));

PS您的数据库看起来错误 - ID 列中的值应该是唯一的,如果 ID 是主键(它看起来应该是)。

PSPS 另一种选择是使用面向对象的设计 - 创建一个具有 id、主题、标题、类型、名称等成员的类。

于 2013-07-31T09:27:05.117 回答
0
public class Product {
    private int id;
    private String topic;
    private String title;
    private String type;
    private String name;

    public Product(Cursor cursor) {
        this.id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
        this.topic = cursor.getString(cursor.getColumnIndexOrThrow("TOPIC"));
        this.title = cursor.getString(cursor.getColumnIndexOrThrow("TITLE")); 
        this.type = cursor.getString(cursor.getColumnIndexOrThrow("TYPE"));
        this.name = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
    }

    //Getter & Setter here...
}

此产品可能在产品列表中,例如:

ArrayList<Product> products = new ArrayList<Product>();
Product product = new Product(cursor);

products.add(product); // or simpler: products.add(new Product(cursor);

您可以将此列表用于多种用途,例如:

ArrayList<int> ids = new ArrayList<int>(); 
ArrayList<String> topics = new ArrayList<String>(); 
ArrayList<String> titles = new ArrayList<String>(); 
ArrayList<String> types = new ArrayList<String>(); 
ArrayList<String> names = new ArrayList<String>();

for (Product product : products) {
    // for every product in your products list do:  
    ids.add(product.getId);
    topics.add(product.getTopic);
    titles.add(product.getTitle);
    types.add(product.getType);
    names.add(product.getName);
}
于 2013-07-31T11:06:57.880 回答