I'm storing some values in a text file and each line looks like that, Cellid: -----, LAC: ------, Lat: ------, Long: --------- what I want to do is to compare the cellid that the phone currently operates, with the cellids from the text file and if it matched then print a marker in google maps. My question is how navigate in the text file while reading it. The file is stored in the phones sdcard.
Asked
Active
Viewed 364 times
1
-
Your question is vague. Where is this text file? Is it server side? If so what language are you using? Perhaps show some code of what you have tried. – Seain Malkin Mar 08 '13 at 18:57
-
It is stored in my android phone sdcard – Mar 08 '13 at 19:16
-
Then you may want to update your question to mention that and add some related android tags. – Seain Malkin Mar 08 '13 at 19:19
-
Ok thanx and sorry about that. – Mar 08 '13 at 19:24
2 Answers
0
Try this
First: add these in public area
public static final String Key_Cellid = "Cellid";
public static final String Key_LAC = "LAC";
public static final String Key_Lat = "Lat";
public static final String Key_Long = "Long";
ArrayList<HashMap<String,String>> values = new ArrayList<HashMap<String, String>>();
then add this method to seperate name and value for each item:
public void stringExtracter(String st){
String mainStr = st;
int items = 4; // Numbers of items in each line
HashMap<String,String> hash = new HashMap<String, String>();
for(int i=0; i<items; i++){
String num="";
int sep = mainStr.indexOf(":");
String fst = mainStr.substring(0, sep);
mainStr = mainStr.replaceFirst(fst+":", "");
if(i<items - 1){
sep = mainStr.indexOf(",");
num = mainStr.substring(0, sep);
mainStr = mainStr.replaceFirst(num+",", "");
}
else{
num = mainStr;
//mainStr = mainStr.replaceFirst(num+",", "");
}
hash.put(fst, num);
}
values.add(hash);
}
Now edit your code :
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
stringExtracter(line);
}
//here you can get only numbers from text and convert it to integer then compare it
}
catch (IOException e) {
//You'll need to add proper error handling here
}
Know you can get data from values
array list like:
for(int i=0;i<values.size();i++){
HashMap<String,String> hash = values.get(i);
System.out.println(hash.get(Key_Cellid));
System.out.println(hash.get(Key_LAC));
System.out.println(hash.get(Key_Lat));
System.out.println(hash.get(Key_Long));
System.out.println("--------------");
}
Important Note:
I assumed no space in each line in text file
Cellid:123,LAC:456,Lat:223,Long:213
I stored numbers as string because that you should convert it to Integer or Double or what you want to using it in map.
Hope this help you.

AwadKab
- 3,054
- 2
- 17
- 17
-
Thanx a lot AwadKab. Is the value of cellid stored as string and how do i store these values in variables so I can use them in map? – Mar 09 '13 at 14:20
-
-
It returns me that the hash.get(Key_Cellid) is null is that means that it cant read the file? – Mar 10 '13 at 12:44
-
Be sure Key_Cellid equal Cellid spelling in your file, and every line in file text no has spaces, must be like `Cellid:123,LAC:456,Lat:223,Long:213` – AwadKab Mar 10 '13 at 14:20
-
"Cellid:"+cellid+","+"LAC:"+lac+","+"Lat:"+latitude+","+"Long:"+longitude+"\n" Thats how I'm writing in the file. Also Key_Cellid is equal Cellid spelling in my file. Do you know how I can check if it is reading the file or not? – Mar 10 '13 at 14:53
-
I used the Log.t to see how the values are stored and it returns me only the value of cellid not the other three values. I pasted the code bellow to check it and if you can point the error please tell me. – Mar 10 '13 at 17:50
0
public static final String Key_Cellid = "Cellid";
public static final String Key_LAC = "LAC";
public static final String Key_Lat = "Lat";
public static final String Key_Long = "Long";
ArrayList<HashMap<String,String>> values = new ArrayList<HashMap<String, String>>();
int cellidtt,lact;
double latt,longt;
public void stringExtracter(String st){
String mainStr = st;
int items = 4; // Numbers of items in each line
HashMap<String,String> hash = new HashMap<String, String>();
for(int i=0; i<items; i++){
String num="";
int sep = mainStr.indexOf(":");
String fst = mainStr.substring(0, sep);
mainStr = mainStr.replaceFirst(fst+":", "");
if(i<items - 1){
sep = mainStr.indexOf(",");
num = mainStr.substring(0, sep);
}
else{
num = mainStr;
mainStr = mainStr.replaceFirst(num+",", "");
}
hash.put(fst, num);
}
values.add(hash);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
GoogleMap gmap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapg)).getMap();
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gmap.setMyLocationEnabled(true);
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"cellidt.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
Log.d("line=",line); // print the text file in the LogCat to ensure that the program is reading the file proper.
stringExtracter(line);
}
//here you can get only numbers from text and convert it to integer then compare it
for(int i=0;i<values.size();i++){
HashMap<String,String> hash = values.get(i);
cellidtt = Integer.parseInt(hash.get(Key_Cellid));
Log.d("cellidtt=",hash.get(Key_Cellid));
lact = Integer.parseInt(hash.get(Key_LAC));
Log.d("lact=",hash.get(Key_LAC));
latt = Double.parseDouble(hash.get(Key_Lat));
Log.d("latt=",hash.get(Key_Lat));
longt = Double.parseDouble(hash.get(Key_Long));
Log.d("longt=",hash.get(Key_Long));
gmap.addMarker(new MarkerOptions()
.position(new LatLng(latt,longt))
.title(cellidtt+","+lact));
}
br.close();
}
catch (IOException e) {
}
}
-
I think that the problem is that is storing only the first value of cellid and not the other three values. – Mar 10 '13 at 17:58
-
Finnaly, it is work now. error in `stringExtracter()` in `mainStr = mainStr.replaceFirst(num+",", "");` I move it up. See my edited answer now. Sorry for that. – AwadKab Mar 10 '13 at 19:39
-
With this code when I'm comparing the phones cellid with the cellid from the text file is not highliting me the correct marker but the last on the values listarray. How can I use the correct position? Basically I don't want to print all the markers on the map but the one that I'm currently communicate with. – Mar 12 '13 at 10:01
-
I don't know how you used it to compare it with phones cellid. Can you past code here? Note: I never useed google map and GPS. – AwadKab Mar 12 '13 at 11:15