简体   繁体   中英

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 |

Cursor hold data like this table. Now, I want get data like ID| TOPIC | TITLE | TYPE | NAME | ID| TOPIC | TITLE | TYPE | NAME | here NAME can be multiple according to the ID . Like for ID 1 K will be FERARI,TOYOTA,AUDI,BMW and so on. I want to show this information in customlistview in a row .

My question is

is there any way to store the name data in String Array or do you have any alternative suggestion for me

If I understand your question correctly, you want to store your values from your database table in arrays? For that, you could create parallel resizeable generic lists like this:

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

And you can then add items to it like this:

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 your database looks wrong - values in ID column should be unique, if ID is the primary key (it looks like it should be).

PSPS Another option would be to use Object-Oriented design - Create a class that has members like id, topic, title, type, name and so on.

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...
}

this Product could be in a list of Products like:

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

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

and you could use this list for many purposes like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM