Issue
I working on android csv file reading library. I create a function in library and calling it in other project. The library function reads the csv file ( which is in the assets folder of my project which is extended from library class ) ,store the name and value in strings GotDbname and GotDbValue respectively . But it is not returning anything. Any type help will be appreciated.
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");
}
}
And here is the class of other project which is using that library
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);
}
}
Solution
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;
}
Answered By - Deffi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.