2016-06-13 84 views
0

我正在使用多維ArrayLists。 我正在讀取具有3行ID,日期和信息的Excel文件中的數據。 我設法設置arrayList,但在每個索引 即3列數據,即數組(1)id1 date1 info1。無法單獨調用info1。 我需要創建一個multy。 arraylist在那裏我可以打電話給arrayindex(1)項目info1。將EXCEL數據添加到多維ArrayList

public class MainActivity extends AppCompatActivity { 
String xx; 

String show; 
int count=0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


} 
    public void order(View v) { 
     try { 
      AssetManager am=getAssets(); 
      InputStream is=am.open("test.xls"); 
      Workbook wb =Workbook.getWorkbook(is); 
      Sheet s=wb.getSheet(0); 
      int row =s.getRows(); 
      int col=s.getColumns(); 
      xx=""; 
      for(int i=0; i<row ; i++) 
      { 
       for (int c=0; c<col; c++) 
       { 
        Cell z=s.getCell(c,i); 
        xx=xx+z.getContents(); 
        xx=xx+" "; 
         mainArray(); 

       } 
       xx=xx+"\n"; 
      } 

      display(xx); 
     } 
     catch (Exception e) 
     { 
     } 
    } 
public void display(String value) 
{ 
    TextView x=(TextView) findViewById(R.id.display); 
    x.setText(value); 


} 
public void mainArray() { 

    ArrayList arrayList = new ArrayList(); 



    arrayList.add(xx); 
    Log.d("log1","Size of al after additions: " + arrayList.size()); 


    Log.d("log1","Contents of al: " + arrayList); 

} 

}

如何啓動一個多維數組列表(ID,日期,資訊),以及如何在迭代我可以根據行/線,數據在正確的名單追加。

ArrayList 
    [ 
    list_Id {id1, id2,id3,.............} 
    list_Date {date1, date2,date3.......} 
    list_Info {info1, info2, info3.......} 
    ] 
+0

您不應該使用id,date,info as seperate Lists。你應該使用一個List來存儲行的三維值 – Jens

回答

0

分而治之的是你需要採取的辦法 - 打破每一個問題,以較小的問題,並試圖解決這些一個接一個。

這在我看來,你正在嘗試做的兩件事情 - 從Excel中讀取數據,將數據轉換爲「多維的ArrayList」,現在你不知道什麼是不工作...

請,嘗試從Excel首先打印數據到控制檯 - 您將驗證,您的閱讀部分正在工作。


爲什麼你想要「多維ArrayLists」?你有行和列,所以你可以從String[][] table = new String[rows][cols];開始?


有在你的代碼的幾個問題 - 字符串XX不應該存在的,但它只是一個臨時我會說。

永遠不要忽略異常 - 你如何知道有什麼異常,至少記錄一些東西!

0
///////////****** WRITE Excel TO SD ***///////////// 
    Fnamexls="testfile" + ".xls"; 
    File sdCard = Environment.getExternalStorageDirectory(); 
    directory = new File (sdCard.getAbsolutePath() + "/My File"); 
    directory.mkdirs(); 
    sdFile = new File(directory, Fnamexls); 
    if(sdFile.exists()) { 
//Do something 
     Log.d("logW","exixts"); 
    } 
    else { 
// Do something else. 
     Log.d("logW", "does not exixt"); 
     wbSettings = new WorkbookSettings(); 
     wbSettings.setLocale(new Locale("en", "EN")); 
     try { 
      wrWorkbook = Workbook.createWorkbook(sdFile, wbSettings); 
      writeSheet = wrWorkbook.createSheet("First Sheet", 0); 
      int row = s.getRows(); 
      int col = s.getColumns(); 
      xx = ""; 
      for (int c = 0; c < col; c++) { 
       for (int r = 0; r < row; r++) { 
        Cell z = s.getCell(c, r); 
        xx = z.getContents(); 
        Label label4 = new Label(c, r, String.valueOf(xx)); 
        try { 
         writeSheet.addCell(label4); 
        } catch (RowsExceededException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (WriteException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
      wrWorkbook.write(); 
      try { 
       wrWorkbook.close(); 
      } catch (WriteException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      //createExcel(excelSheet); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    readSD();