Android的 - 添加项目到一个ArrayList然后导出为CSV [英] Android - Adding items to an ArrayList then export to CSV

查看:261
本文介绍了Android的 - 添加项目到一个ArrayList然后导出为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将项目添加到一个ArrayList然后将列表导出为CSV上的SD卡文件,这里是一个code样品:<​​/ P>

 尝试
{  字符串的csv =/sdcard/order.csv;
      CSVWriter作家=新CSVWriter(新的FileWriter(CSV));
  HdwOrderCursor = HDWDBHelper.fetchAllOrders();
  startManagingCursor(HdwOrderCursor);
  清单&LT;的String []&GT; mArrayList =新的ArrayList&LT;的String []&GT;();
  HdwOrderCursor.moveToFirst();
  而(!HdwOrderCursor.isAfterLast()){
                      mArrayList.add(HdwOrderCursor.getString(HdwOrderCursor.getColumnIndex(HDWDBHelper.KEY_ITEMNUM))); //添加项目
                      HdwOrderCursor.moveToNext();
  }  writer.writeAll(mArrayList);
  writer.close();
}

在'mArrayList.add'是有错误在类型列表Add方法(字符串[])不适用的参数(字符串),不知道这意味着什么。


解决方案

  

在'mArrayList.add'是有错误在类型列表Add方法(字符串[])不适用的参数(字符串),不知道这意味着什么。


这是一个pretty清除错误,它告诉你的是:

1),该方法mArrayList.add()需要一个String [因为它的说法。
2)你正在尝试在传递一个字符串。新增()方法,而不是一个String []。

所以,你需要检查并确保你的方法正在返回你所期望的参数,如果他们那么你需要修改你的插件code解决这个问题。

基本上,你做了什么:

 列表&LT;的String []&GT; mArrayList =新的ArrayList&LT;的String []&GT;();

此调用创建的String [] s的名单。

在此呼吁:

<$p$p><$c$c>mArrayList.add(HdwOrderCursor.getString(HdwOrderCursor.getColumnIndex(HDWDBHelper.KEY_ITEMNUM))); //添加项目

您explictely从DB获得一个字符串,然后尝试把一个字符串到您的String []的列表。

有一个快速的工作围绕这是做:

  mArrayList.add(新的String [] {HdwOrderCursor.getString(HdwOrderCursor.getColumnIndex(HDWDBHelper.KEY_ITEMNUM))}); //添加项目

不过,我并不真的认为你想要的String []的列表,所以更好的选择是改变:

 列表&LT;的String []&GT; mArrayList =新的ArrayList&LT;的String []&GT;();

 列表&LT;串GT; mArrayList =新的ArrayList&LT;串GT;();

I am trying to add items to an ArrayList then export the list to a CSV file on the SDCARD, here is a code sample:

try 
{

  String csv = "/sdcard/order.csv";
      CSVWriter writer = new CSVWriter(new FileWriter(csv));                                        
  HdwOrderCursor = HDWDBHelper.fetchAllOrders();
  startManagingCursor(HdwOrderCursor);
  List<String[]> mArrayList = new ArrayList<String[]>();
  HdwOrderCursor.moveToFirst();
  while(!HdwOrderCursor.isAfterLast()) {
                      mArrayList.add(HdwOrderCursor.getString(HdwOrderCursor.getColumnIndex(HDWDBHelper.KEY_ITEMNUM))); //add the item                      
                      HdwOrderCursor.moveToNext();                                               
  }

  writer.writeAll(mArrayList);  
  writer.close();        
} 

The 'mArrayList.add' is having an error "The method add(String[]) in the type List is not applicable for the arguments (String)", not sure what this means.

解决方案

The 'mArrayList.add' is having an error "The method add(String[]) in the type List is not applicable for the arguments (String)", not sure what this means.

This is a pretty clear error, what it tells you is:

1) That the method "mArrayList.add()" takes a String[] as it's argument. 2) That you're attempting to pass a String in to the .add() method, not a String[].

So you need to check and make sure that your methods are returning the arguments you expect, and if they are then you need to modify your add code to address this.

Basically what you have done:

List<String[]> mArrayList = new ArrayList<String[]>();

This call creates a List of String[]s.

In this call:

mArrayList.add(HdwOrderCursor.getString(HdwOrderCursor.getColumnIndex(HDWDBHelper.KEY_ITEMNUM))); //add the item

You explictely get a STRING from the DB, and then attempt to put a String into your list of String[]'s.

A quick work around for this is to do:

 mArrayList.add(new String[] {HdwOrderCursor.getString(HdwOrderCursor.getColumnIndex(HDWDBHelper.KEY_ITEMNUM))}); //add the item

However, I don't actually think that you want a list of String[]'s, so the better option would be to change:

List<String[]> mArrayList = new ArrayList<String[]>();

to:

List<String> mArrayList = new ArrayList<String>();

这篇关于Android的 - 添加项目到一个ArrayList然后导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆