Android 如何将值与存储在文本文件中的整数进行比较
我在一个文本文件中存储了一些值,每一行都是这样的, Cellid:--,LAC:--,Lat:--,Long:----- 我想做的是将手机当前运行的手机ID与文本文件中的手机ID进行比较,如果匹配,然后在谷歌地图中打印一个标记。我的问题是如何在读取文本文件时在其中导航。该文件存储在手机SD卡中。试试这个 第一:在公共区域添加这些Android 如何将值与存储在文本文件中的整数进行比较,android,google-maps,text-files,latitude-longitude,cellid,Android,Google Maps,Text Files,Latitude Longitude,Cellid,我在一个文本文件中存储了一些值,每一行都是这样的, Cellid:--,LAC:--,Lat:--,Long:----- 我想做的是将手机当前运行的手机ID与文本文件中的手机ID进行比较,如果匹配,然后在谷歌地图中打印一个标记。我的问题是如何在读取文本文件时在其中导航。该文件存储在手机SD卡中。试试这个 第一:在公共区域添加这些 public static final String Key_Cellid = "Cellid"; public static final String Key_LAC
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>>();
知道您可以从值数组列表中获取数据,如:
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("--------------");
}
重要提示:
我假设文本文件中的每一行都没有空格
细胞ID:123,LAC:456,Lat:223,Long:213
我将数字存储为字符串,因为您应该将其转换为整数或双精度,或者在映射中使用它
希望这对你有所帮助。你的问题很模糊。这个文本文件在哪里?是服务器端吗?如果是,您使用的是什么语言?可能会显示一些您尝试过的代码。它存储在我的android手机SDCard中,然后您可能希望更新您的问题以提及这一点,并添加一些相关的android标签。好的,谢谢。谢谢。cellid的值是否存储为字符串?如何将这些值存储在变量中以便在映射中使用?//从文件字符串文本中读取文本;为什么要声明此字符串?它返回我hash.getKey\u Cellid为null,这意味着它无法读取文件?请确保Key\u Cellid等于文件中的Cellid拼写,并且文件文本中的每一行都没有空格,必须像Cellid:123、LAC:456、Lat:223、Long:213 Cellid:+Cellid+、+LAC:+LAC+、+Lat:+Lat+,+Long:+longide+\n这就是我在文件中写入的方式。另外,Key_Cellid是我的文件中相同的Cellid拼写。你知道我如何检查它是否正在读取文件吗?我认为问题在于只存储cellid的第一个值,而不存储其他三个值。最后,它现在可以工作了。mainStr=mainStr.replaceFirstnum+,,中的StringExtractor出错;我把它向上移动。现在查看我编辑的答案。很抱歉,谢谢你,伙计。你是最棒的。当我比较手机的手机号和文本文件中的手机号时,使用这段代码并不是给我正确的标记,而是给我值列表数组中的最后一个标记。如何使用正确的位置?基本上,我不想打印地图上的所有标记,但我目前正在与之通信的标记。我不知道你是如何使用它与手机ID进行比较的。你能在这里输入密码吗?注:我从未使用过谷歌地图和GPS。
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) {
}
}
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("--------------");
}
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) {
}
}