我正在研究android csv文件读取库。我在库中创建一个函数并在其他项目中调用它。库函数读取csv文件(位于我的项目的assets文件夹中,从库类扩展),分别将名称和值存储在字符串GotDbname和GotDbValue中。但它没有返回任何东西。任何类型的帮助将不胜感激。
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);
}
}
答案 0 :(得分:-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;
}