2014-07-11 161 views
0

我工作在android csv文件閱讀庫。我在庫中創建一個函數並在其他項目中調用它。庫函數讀取csv文件(它位於我的項目的資產文件夾中,它是從庫類擴展而來的),分別在字符串GotDbname和GotDbValue中存儲名稱和值。但它沒有返回任何東西。任何類型的幫助將不勝感激。HasMap函數返回空值

public class ReadFile extends Activity { 
String GetDbName = "DBName"; 
String GetDbVersion = "DbVersion"; 
String GotDbName; 
String GotDbVersion; 
HashMap<String, String> DatabaseMap = new HashMap<String, String>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
} 

public void hello() { 
    Log.d("Library Is working ", "Hello "); 

} 

public HashMap<String, String> read(Context con, String name, String version) { 
    Log.d("Read File", "Getting Assest Manager"); 

    AssetManager assetManager = con.getAssets(); 
    Log.d("Read File", "Assest Manager Object Created"); 
    if (assetManager == null) { 
     Log.d("Reading file", "Nothing in assets"); 
    } 
    else { 
     InputStream inputStream = null; 
     Log.d("Read File", "Getting input Stream"); 
     try { 
      Log.d("Read File", "Getting File"); 
      inputStream = assetManager.open("Db.csv"); 
      Log.d("Read File", "File read"); 
      String text = loadTextFile(inputStream); 

      String[] text1 = text.split(","); 

      Log.d("Read File", "LoadTextFile" + text + " " + text1); 

      String id[] = new String[text1.length]; 
      String value[] = new String[text1.length]; 
      Log.d("Read File", "Getting Strings" + "DbName " + id 
        + "DbVersion " + value); 
      int c = 0; 
      for (int i = 0; i < text1.length; i += 2) 
      { 
       id[c] = text1[i].replace('"', ' '); 
       Log.d("Read File", "Getting Db name" + " " + id[c]); 
       c++; 
      } 
      int d = 0; 
      for (int i = 1; i < text1.length; i += 2) 
      { 
       value[d] = text1[i].replace('"', ' '); 
       Log.d("Read File", "Getting getting Version" + " " 
         + value[d]); 
       d++; 
      } 
      int indexofid = -1; 
      for (int i = 0; i < id.length; i++) { 
       Log.d("Reading CSV", "In for Loop"); 
       if (GetDbName.equals(id[i].trim())) { 
        indexofid = i; 

        GotDbName = value[indexofid].toString(); 
        Log.d("Read File final", "Getting Db Name" + " " 
          + GotDbName); 
        break; 

       } 
      } 
      for (int i = 0; i < id.length; i++) { 

       if (GetDbVersion.equals(id[i].trim())) { 
        indexofid = i; 

        GotDbVersion = value[indexofid].toString(); 
        Log.d("Read File final", "Getting Db Version" + " " 
          + GotDbVersion); 
        break; 

       } 

      } 
     } catch (IOException e) { 
      Log.d("Read File", "No file found"); 
     } 

    } 
    DatabaseMap.put(name, GotDbName); 
    DatabaseMap.put(version, GotDbVersion); 
    Log.d("Read File", " Database Name =" + GotDbName 
      + "Database Version =" + GotDbVersion); 
    return DatabaseMap; 

} 

    public String loadTextFile(InputStream inputStream) throws IOException { 
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
    byte[] bytes = new byte[4096]; 
    int len = 0; 
    while ((len = inputStream.read(bytes)) > 0) 
     byteStream.write(bytes, 0, len); 
    return new String(byteStream.toByteArray(), "UTF8"); 
} 
} 

這裏是類中的其他項目中的哪一個使用該庫

public class MainClass extends ReadFile { 
String DbName; 
String DbVersion; 
String a, b; 
TextView txt_dbName, txt_version; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txt_dbName = (TextView) findViewById(R.id.txt_dbName); 
    txt_version = (TextView) findViewById(R.id.txt_version); 
    HashMap<String, String> map = new HashMap<>(); 
    map = read(this, DbName, DbVersion); 
    String abc = map.get("name"); 
    Log.d("Main Class", "Map =" + abc); 

    Log.d("MainClass", "DbName = " + DbName + ": DbVersion = " 
      + DbVersion); 

    txt_dbName.setText(DbName); 
    txt_version.setText(DbVersion); 
} 
} 

回答

-1
String DbName; 
String DbVersion; 
... 
map = read(this, DbName, DbVersion); 

//The DbName and DbVersion is null or empty? 

public HashMap<String, String> read(Context con, String name, String version) { 

    ... 
    DatabaseMap.put(name, GotDbName); // If name is null, how to get? 
    DatabaseMap.put("name", GotDbName); // Maybe 
    DatabaseMap.put(version, GotDbVersion); 
    Log.d("Read File", " Database Name =" + GotDbName 
      + "Database Version =" + GotDbVersion); 
    return DatabaseMap; 
} 
+1

dbname爲空。 –

+0

和你期望什麼? – mihail

+0

爲DbName和DbVersion設置一個字符串,然後你可以在這裏得到一些東西: String abc = map.get(DbName); String abc2 = map.get(DbVersion); – jDur