2017-03-27 6 views
6

मैं सबूत-ऑफ-अवधारणा ऐप पर काम कर रहा हूं ताकि मैं इस सुविधा को एक बड़े ऐप में कार्यान्वित कर सकूं जो मैं कर रहा हूं। मैं जावा और एंड्रॉइड देव के लिए थोड़ा नया हूं लेकिन उम्मीद है कि यह एक प्रश्न का बहुत सरल या जटिल नहीं होना चाहिए।एंड्रॉइड ऐप में सीएसवी फ़ाइल पढ़ना

मूल रूप से, मैं एक CSV फ़ाइल से स्ट्रिंग की एक सूची में पढ़ सकते हैं और उस पर एप्लिकेशन की मुख्य गतिविधि सूची प्रदर्शित करने में उपयोगी बनाने के लिए कोशिश कर रहा हूँ।

मैं सीएसवी के दशक में पढ़ने के लिए एक बाहरी वर्ग का उपयोग कर रहा हूँ। यहाँ वर्ग कोड है:

CSVFile.java

package com.yourtechwhiz.listdisplay; 

import android.util.Log; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

public class CSVFile { 
    InputStream inputStream; 

    public CSVFile(InputStream inputStream){ 
     this.inputStream = inputStream; 
    } 

    public List read(){ 
     List resultList = new ArrayList(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
     try { 
      String csvLine; 
      while ((csvLine = reader.readLine()) != null) { 
       String[] row = csvLine.split(","); 
       resultList.add(row); 
       Log.d("VariableTag", row[0].toString()); 
      } 
     } 
     catch (IOException ex) { 
      throw new RuntimeException("Error in reading CSV file: "+ex); 
     } 
     finally { 
      try { 
       inputStream.close(); 
      } 
      catch (IOException e) { 
       throw new RuntimeException("Error while closing input stream: "+e); 
      } 
     } 
     return resultList; 
    } 
} 

यहाँ मेरी मुख्य गतिविधि कोड है:

MainActivity.java

package com.yourtechwhiz.listdisplay; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 

    // Array of strings that's used to display on screen 
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     prepArray(); 

     //Display List on Activity 
     ArrayAdapter adapter = new ArrayAdapter<String>(this, 
       R.layout.activity_listview, mobileArray); 
     ListView listView = (ListView) findViewById(R.id.mobile_list); 
     listView.setAdapter(adapter); 

    } 

    //Get list of strings from CSV ready to use 
    private void prepArray() { 

     InputStream inputStream = getResources().openRawResource(R.raw.strings); 
     CSVFile csvFile = new CSVFile(inputStream); 
     List myList = csvFile.read(); 

     //This is where it has an error 
     //Set first array in myList to this new array myArray 
     String[] myArray = myList.get(0); 

    } 

} 

मैं करने के लिए वास्तव में नहीं हूँ अभी तक मोबाइलएरे सरणी सेट करने का बिंदु। अभी मैं सिर्फ सूची वस्तु MyList से बाहर जानकारी "निकालने" करने के लिए कोशिश कर रहा हूँ ...

किसी ने मुझे यह कैसे किया जाता व्याख्या कर सकते हैं? शायद मैं पूरी तरह से सूची प्रकार को समझ नहीं रहा हूं। ऐसा लगता है कि जब परिणामसूची CSVFile रीड विधि में वापस आती है, तो इसे स्ट्रिंग सरणी ऑब्जेक्ट्स वाली एक सूची ऑब्जेक्ट के रूप में वापस कर दिया जाता है। लेकिन मुझे ऐसा काम करने के लिए प्रतीत नहीं होता है।

किसी भी मदद की सराहना की जाती है!

अंतिम संपादन (काम कर कोड)

private void prepArray() { 

     try{ 
      CSVReader reader = new CSVReader(new InputStreamReader(getResources().openRawResource(R.raw.strings)));//Specify asset file name 
      String [] nextLine; 
      while ((nextLine = reader.readNext()) != null) { 
       // nextLine[] is an array of values from the line 
       System.out.println(nextLine[0] + nextLine[1] + "etc..."); 
       Log.d("VariableTag", nextLine[0]); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
      Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show(); 
     } 

    } 

संपादित

अब मेरी prepArray समारोह ऐसा दिखाई देता है:

private void prepArray() { 

     try{ 
      String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "strings.csv" 
      File csvfile = new File(csvfileString); 
      CSVReader reader = new CSVReader(new FileReader("csvfile.getAbsolutePath()")); 
      String [] nextLine; 
      while ((nextLine = reader.readNext()) != null) { 
       // nextLine[] is an array of values from the line 
       System.out.println(nextLine[0] + nextLine[1] + "etc..."); 
      } 
     }catch(FileNotFoundException e){ 
      e.printStackTrace(); 
      Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show(); 
     } 

    } 

फिर भी FileNotFoundException पैदा करता है।

संपादित 2/3

यहाँ लॉग उत्पादित की जाती है कि जब मैं तार (src \ मुख्य \ संपत्ति \ तार \ तार का एक सबफ़ोल्डर में strings.csv के साथ एक वास्तविक फोन पर अनुप्रयोग चलाने के लिए है।csv) में परिवर्तन के साथ आप कोड के लिए अनुरोध:

03/27 17:44:01: Launching app 
     $ adb push C:\Users\Roy\AndroidStudioProjects\ListDisplay\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.yourtechwhiz.listdisplay 
     $ adb shell pm install -r "/data/local/tmp/com.yourtechwhiz.listdisplay" 
     pkg: /data/local/tmp/com.yourtechwhiz.listdisplay 
     Success 


     $ adb shell am start -n "com.yourtechwhiz.listdisplay/com.yourtechwhiz.listdisplay.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D 
     Connecting to com.yourtechwhiz.listdisplay 
     D/HyLog: I : /data/font/config/sfconfig.dat, No such file or directory (2) 
     D/HyLog: I : /data/font/config/dfactpre.dat, No such file or directory (2) 
     D/HyLog: I : /data/font/config/sfconfig.dat, No such file or directory (2) 
     W/ActivityThread: Application com.yourtechwhiz.listdisplay is waiting for the debugger on port 8100... 
     I/System.out: Sending WAIT chunk 
     I/dalvikvm: Debugger is active 
     I/System.out: Debugger has connected 
     I/System.out: waiting for debugger to settle... 
     Connected to the target VM, address: 'localhost:8609', transport: 'socket' 
     I/System.out: waiting for debugger to settle... 
     I/System.out: waiting for debugger to settle... 
     I/System.out: waiting for debugger to settle... 
     I/System.out: waiting for debugger to settle... 
     I/System.out: waiting for debugger to settle... 
     I/System.out: waiting for debugger to settle... 
     I/System.out: waiting for debugger to settle... 
     I/System.out: debugger has settled (1498) 
     I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts 
     W/dalvikvm: VFY: unable to resolve interface method 16152: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V 
     D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
     W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
     I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
     W/dalvikvm: VFY: unable to resolve interface method 16154: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
     D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
     I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
     W/dalvikvm: VFY: unable to resolve interface method 16158: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
     D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
     I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
     W/dalvikvm: VFY: unable to resolve virtual method 455: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
     D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
     I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
     W/dalvikvm: VFY: unable to resolve virtual method 477: Landroid/content/res/TypedArray;.getType (I)I 
     D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008 
     I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild 
     W/dalvikvm: VFY: unable to resolve virtual method 16589: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
     D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002 
     I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList 
     W/dalvikvm: VFY: unable to resolve virtual method 269: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 
     D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 
     I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 
     W/dalvikvm: VFY: unable to resolve virtual method 418: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
     D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
     I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 
     W/dalvikvm: VFY: unable to resolve virtual method 420: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
     D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
     E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 
     W/dalvikvm: VFY: unable to resolve instanceof 140 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper; 
     D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c 
     W/System.err: java.io.FileNotFoundException: /csvfile.getAbsolutePath(): open failed: ENOENT (No such file or directory) 
     W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:462) 
     W/System.err:  at java.io.FileInputStream.<init>(FileInputStream.java:78) 
     W/System.err:  at java.io.FileInputStream.<init>(FileInputStream.java:105) 
     W/System.err:  at java.io.FileReader.<init>(FileReader.java:66) 
     W/System.err:  at com.yourtechwhiz.listdisplay.MainActivity.prepArray(MainActivity.java:43) 
     W/System.err:  at com.yourtechwhiz.listdisplay.MainActivity.onCreate(MainActivity.java:26) 
     W/System.err:  at android.app.Activity.performCreate(Activity.java:5287) 
     W/System.err:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
     W/System.err:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2145) 
     W/System.err:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231) 
     W/System.err:  at android.app.ActivityThread.access$700(ActivityThread.java:139) 
     W/System.err:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401) 
     W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
     W/System.err:  at android.os.Looper.loop(Looper.java:137) 
     W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5082) 
     W/System.err:  at java.lang.reflect.Method.invokeNative(Native Method) 
     W/System.err:  at java.lang.reflect.Method.invoke(Method.java:515) 
     W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
     W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598) 
     W/System.err:  at dalvik.system.NativeStart.main(Native Method) 
     W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
     W/System.err:  at libcore.io.Posix.open(Native Method) 
     W/System.err:  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
     W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:446) 
     W/System.err: ... 19 more 
     I/Adreno-EGL: <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: () 
     OpenGL ES Shader Compiler Version: E031.24.00.01 
     Build Date: 12/27/13 Fri 
     Local Branch: qualcomm_only 
     Remote Branch: 
     Local Patches: 
     Reconstruct Branch: 
     D/OpenGLRenderer: Enabling debug mode 0 
     D/OpenGLRenderer: GL error from OpenGLRenderer: 0x502 
     E/OpenGLRenderer: GL_INVALID_OPERATION 
+0

नोट: 'csvLine.split (", ")' सही ढंग से कॉलम में अल्पविराम के * के लिए काम नहीं करेगा * –

उत्तर

9

कोशिश OpenCSV - ऐसा लगता है कि सही मुख्य पृष्ठ पर आसान

http://opencsv.sourceforge.net/

आसान निर्देशों का अपने जीवन कर देगा,:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); 
String [] nextLine; 
while ((nextLine = reader.readNext()) != null) { 
    // nextLine[] is an array of values from the line 
    System.out.println(nextLine[0] + nextLine[1] + "etc..."); 
} 

या

CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); 
List myEntries = reader.readAll(); 

बस का उपयोग करने के अपने Gradle निर्भरता से जोड़ें:

compile 'com.opencsv:opencsv:3.9' 

अपनी टिप्पणी प्रति संपादित:

try{ 
File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv"); 
CSVReader reader = new CSVReader(new FileReader("csvfile.getAbsolutePath()")); 
String [] nextLine; 
while ((nextLine = reader.readNext()) != null) { 
    // nextLine[] is an array of values from the line 
    System.out.println(nextLine[0] + nextLine[1] + "etc..."); 
} 
}catch(Exception e){ 
e.printStackTrace(); 
Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show(); 
} 

आप आवेदन के साथ csv फ़ाइल पैकेज चाहते हैं और यह आंतरिक भंडारण पर स्थापित है, तो जब ऐप इंस्टॉल हो जाता है,

आपके प्रोजेक्ट src/main फ़ोल्डर (यानी) में एक संपत्ति फ़ोल्डर बनाएं c: \ MyApp \ एप्लिकेशन \ src \ मुख्य \ संपत्ति \) और वहाँ में csv फ़ाइल शब्दों में कहें, तो इस तरह इसे संदर्भ अपनी गतिविधि में:

String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv" 
File csvfile = new File(csvfileString); 
+0

धन्यवाद त्वरित जवाब के लिए! मैंने इसे जोड़ने का प्रयास किया है और ऐसा लगता है कि मुझे एक दिलचस्प त्रुटि मिल रही है। 'नया FileReader ("strings.csv")' त्रुटि से बाहर आता है "अनचाहे अपवाद: java.io.FileNotFoundException"। यह मुझे दो प्रश्नों की ओर ले जाता है। एंड्रॉइड प्रोजेक्ट फाइलों में मेरी स्ट्रिंग्स सीएसवी फाइल कहां जाती है? और मैं इस त्रुटि को कैसे ठीक करूं? आपकी सहायताके लिए धन्यवाद! – TheBlindDeveloper

+0

अधिक विशिष्ट जानकारी के लिए फिर से धन्यवाद। जब कभी-कभी कोड की बात आती है तो मैं मदद नहीं कर सकता लेकिन लूप से थोड़ा सा महसूस कर सकता हूं। मुझे अभी भी एक ही त्रुटि मिल रही है लेकिन अब एक अलग जगह पर। मैंने अपनी पोस्ट के नीचे संपादित किया है, जो फ़ंक्शन मैंने आपके पोस्ट के अनुसार संपादित किया है ... मैं यहाँ क्या गलत कर रहा हूं? – TheBlindDeveloper

+0

मैं इसे एक वास्तविक डिवाइस पर चलाऊंगा। लेकिन इस बिंदु पर, मुझे संपादक में त्रुटि मिल रही है (एंड्रॉइड स्टूडियो)। मेरे पास मेरी strings.csv फ़ाइल है जो उस ऐप \ src \ main \ assets \ फ़ोल्डर के अंतर्गत स्थित है। तो मुझे यकीन नहीं है कि त्रुटि कहां से आ रही है ... – TheBlindDeveloper

1

एंड्रॉयड डिफ़ॉल्ट रूप से कच्चे folder.Create एक का निर्माण नहीं करता आपकी परियोजना में कच्चे/कच्चे के तहत कच्चा फ़ोल्डर। उसमें अपनी सीएसवी फ़ाइल कॉपी करें। सीएसवी फ़ाइल के निचले मामले का नाम रखें और पूछे जाने पर टेक्स्ट प्रारूप में कनवर्ट करें। मेरा सीएसवी फ़ाइल नाम welldata.scv वेलडाटा - यह गेटर और सेटर के साथ मॉडल क्लास है। अच्छीडेटालिस्ट डेटा स्टोर करने के लिए ArrayList है।

private void readData() { 
    InputStream is = getResources().openRawResource(R.raw.welldata); 
    BufferedReader reader = new BufferedReader(
      new InputStreamReader(is, Charset.forName("UTF-8"))); 
    String line = ""; 
    try { 
     while ((line = reader.readLine()) != null) { 
      //set splitter 
      String[] tokens = line.split(","); 

      //read the data 
      WellData wellData = new WellData(); 
      wellData.setOwner(tokens[0]); 
      wellData.setApi(tokens[1]); 
      wellData.setLongitude(tokens[2]); 
      wellData.setLatitude(tokens[3]); 
      wellData.setProperty(tokens[4]); 
      wellData.setWellName(tokens[5]); 
      wellDataList.add(wellData); 

      Log.d("MainActivity" ,"Just Created " +wellData); 

     } 
    } catch (IOException e1) { 
     Log.e("MainActivity", "Error" + line, e1); 
     e1.printStackTrace(); 
    } 

} 

}

संबंधित मुद्दे