簡體   English   中英

從光標獲取數據並將其存儲在字符串數組中

[英]Getting data from cursor and store it in string array

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 | ID| TOPIC | TITLE | TYPE | NAME | 在此NAME根據ID可以是多個。 ID 1 K一樣是FERARI,TOYOTA,AUDI,BMW等。 我想在customlistview中連續顯示此信息。

我的問題是

有什么辦法可以將名稱數據存儲在字符串數組中,或者您有其他建議嗎?

如果我正確理解了您的問題,您想將數據庫表中的值存儲在數組中嗎? 為此,您可以創建並行可調整大小的通用列表,如下所示:

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,主題,標題,類型,名稱等。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM