Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从游标获取数据并将其存储在字符串数组中_Java_Android - Fatal编程技术网

Java 从游标获取数据并将其存储在字符串数组中

Java 从游标获取数据并将其存储在字符串数组中,java,android,Java,Android,游标保存类似于此表的数据。现在,我想获取像ID | TOPIC | TITLE | TYPE | NAME |这样的数据,在这里NAME可以根据ID进行多次。与ID类似,1K将是费拉里、丰田、奥迪、宝马等等。我想在customlistview中以一行的形式显示此信息 我的问题是 是否有任何方法将名称数据存储在字符串数组中,或者您是否有其他建议如果我正确理解您的问题,您希望将数据库表中的值存储在数组中?为此,您可以创建如下并行可调整大小的通用列表: ID| TOPIC | TITLE | TYPE

游标保存类似于此表的数据。现在,我想获取像
ID | TOPIC | TITLE | TYPE | NAME |
这样的数据,在这里
NAME
可以根据
ID
进行多次。与ID类似,1K将是费拉里、丰田、奥迪、宝马等等。我想在
customlistview
中以一行的形式显示此信息

我的问题是


是否有任何方法将名称数据存储在字符串数组中,或者您是否有其他建议

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

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 |
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>(); 
另外,您的数据库看起来是错误的-如果ID是主键,则ID列中的值应该是唯一的(看起来应该是唯一的)

p.S.p.S 另一种选择是使用面向对象设计——创建一个类,该类包含id、主题、标题、类型、名称等成员

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")));
此产品可能位于以下产品列表中:

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 products=new ArrayList();
产品=新产品(光标);
产品。添加(产品);//或者更简单:products.add(新产品)(光标);
您可以将此列表用于多种用途,如:

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

products.add(product); // or simpler: products.add(new Product(cursor);
ArrayList id=new ArrayList();
ArrayList topics=新建ArrayList();
ArrayList titles=新的ArrayList();
ArrayList类型=新的ArrayList();
ArrayList name=新的ArrayList();
用于(产品:产品){
//对于产品列表中的每种产品,请执行以下操作:
add(product.getId);
topics.add(product.getTopic);
titles.add(product.getTitle);
添加(product.getType);
name.add(product.getName);
}

你想拥有像
{Id,Cars}
这样的对象集合吗?你的代码也没有做任何有用的事情,而且看起来是错误的。有多个非唯一的Id?!我尝试使用stringList.add(getID,getName);但它抛出indexoutofboundexception。我在这里通过连接两个表获得的数据。
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);
}