Java android循环出现问题,无法将值放入hashmap
这个问题困扰了我很长时间。我使用了一个名为bmob的云数据库,我发现我可以成功地获得我想要的数据。然而,循环中可能有一些错误,我只能获得最后一个选定项目的信息 p、 我使用一个名为Playlist的数组列表来存储我将在下一个活动中用于显示列表视图的计算数据 这是我的密码:Java android循环出现问题,无法将值放入hashmap,java,android,Java,Android,这个问题困扰了我很长时间。我使用了一个名为bmob的云数据库,我发现我可以成功地获得我想要的数据。然而,循环中可能有一些错误,我只能获得最后一个选定项目的信息 p、 我使用一个名为Playlist的数组列表来存储我将在下一个活动中用于显示列表视图的计算数据 这是我的密码: public class DestinationActivity extends Activity implements OnClickListener, NumberPicker.OnValueChangeListener
public class DestinationActivity extends Activity implements OnClickListener, NumberPicker.OnValueChangeListener {
private TextView from_place, date, days, start_time, end_time, number, money_view;
private Button addButton, subButton;
private ImageView backButton, telephone;
private ListView listView;
private Button destinationOk_btn;
private ShapeLoadingDialog shapeLoadingDialog;
private Tip startTip;
private Calendar calendar;
private DatePickerDialog dialog;
private TimePickerDialog dialog2;
private List<Destination> destinationList = new ArrayList<Destination>();
private DestinationAdapter adapter;
private int number_value = 1;
private String time_start;
private String time_end;
private int travel_days;
double travelTime;//total playing time
double travel_time;
private int money;
private int num = 1;
private ArrayList<Integer> select_placeID = new ArrayList<Integer>();
public Map<Integer,Double> weightMap;
public List<Plan> planList = new ArrayList<Plan>();
int[] selectedID = new int[10];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination_layout);
//initialize the cloud database
Bmob.initialize(this, BmobConfig.APP_ID);
listView = (ListView) findViewById(R.id.list_destination);
destinationOk_btn = (Button) findViewById(R.id.okButton);
initDestinations(); // initialize the data
adapter = new DestinationAdapter(destinationList, DestinationActivity.this);
//adapter = new DestinationAdapter(this, destinationList, DestinationAdapter.getIsSelected());
listView.setAdapter(adapter);
//....listeners and textviews.......
//submit button
destinationOk_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
select_placeID.clear();
for (int i = 0; i < destinationList.size(); i++) {
if (DestinationAdapter.getIsSelected().get(i)) {
select_placeID.add((i + 1));
}
}
//change to int array
selectedID = new int[select_placeID.size()];
for(int i = 0;i<select_placeID.size();i++){
selectedID[i] = select_placeID.get(i);
}
if (select_placeID.size() == 0) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(DestinationActivity.this);
builder1.setMessage("no records");
builder1.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(DestinationActivity.this);
builder.setMessage("waiting for magic...");
builder.show();
/**
* calculate the route
*/
if (validate()) {
new calRoute().execute();
}
}
}
});
}
//initialize the data
private void initDestinations() {
//........
}
@Override
public void onClick(View v) {
//.......
}
/**
* asynctask
*/
private class calRoute extends AsyncTask<Void, Void, List<Plan>>{
public calRoute(){
// TODO Auto-generated constructor stub
}
@Override
protected List<Plan> doInBackground(Void... params) {
List<Plan> result = calculate(time_start, time_end, travel_days);
return result;
}
@Override
protected void onPostExecute(List<Plan> result) {
super.onPostExecute(result);
if (result != null) {
Toast.makeText(DestinationActivity.this, "success", Toast.LENGTH_SHORT).show();
if(planList.size() > 0) {
Intent intent = new Intent();
intent.setClass(DestinationActivity.this, ActivityPlan.class);
intent.putParcelableArrayListExtra("planInfo", (ArrayList<? extends Parcelable>) planList);
startActivity(intent);
}
else{
Toast.makeText(DestinationActivity.this, "no plan", Toast.LENGTH_SHORT).show();
}
}
}
}
/**
*plan
**/
public List<Plan> calculate(String time_start, String time_end, int travel_days) {
SimpleDateFormat df = new SimpleDateFormat(("HH:mm"));
Date starttime = new Date();
Date endtime = new Date();
try {
starttime = df.parse(time_start);
} catch (ParseException e) {
e.printStackTrace();
}
try {
endtime = df.parse(time_end);
} catch (ParseException e) {
e.printStackTrace();
}
double l = endtime.getTime() - starttime.getTime();
double hour = (l / (60 * 60 * 1000));
double min = ((l / (60 * 1000)) - hour * 60);
if(min == 0){
min = 60;
}
else {
travel_time = ((1.0 * travel_days * hour) * (min / 60));
DecimalFormat decimalFormat = new DecimalFormat("#.0");
travelTime = Double.parseDouble(decimalFormat.format(travel_time));
}
weightMap = new LinkedHashMap<Integer, Double>(); //store weight
int totalPriority = 0;//total priority
final Destination start = new Destination(116.32133, 39.92269);
final HashMap<Integer, Integer> pMap = new HashMap<Integer, Integer>();
final HashMap<Integer, String> nameMap = new HashMap<Integer, String>();
final HashMap<Integer, Destination> objectMap = new LinkedHashMap<Integer, Destination>();
/**
* get the data from cloud database
*/
BmobQuery<Destination> query = new BmobQuery<Destination>();
for (int sid: selectedID) {
query.addWhereEqualTo("id", sid);
query.findObjects(new FindListener<Destination>() {
@Override
public void done(List<Destination> list, BmobException e) {
if (e == null) {
System.out.println("success:total" + list.size() + "items。");
for (Destination destination : list) {
int p = destination.getPriority();
int id = destination.getId();
String name = destination.getName();
double longitude = destination.getLongitude();
double latitude = destination.getLatitude();
objectMap.put(id, new Destination(longitude, latitude));
System.out.println(id);
double dis = DistanceUtil.distance(start.getLongitude(), start.getLatitude(),
longitude, latitude);
pMap.put(id, p);
weightMap.put(id, new Double(dis));
nameMap.put(id, name);
}
} else {
Log.i("bmob", "error:" + e.getMessage() + "," + e.getErrorCode());
}
}
});
}
for (Integer key : pMap.keySet()) {
int p = pMap.get(key).intValue();
totalPriority = totalPriority + p;
}
double weight = 0.0;
for (Map.Entry<Integer, Double> hm : weightMap.entrySet()) {
double hm2Value = pMap.get(hm.getKey());
weight = totalPriority / hm.getValue() * hm2Value;
weightMap.put(hm.getKey(), weight);
}
/**
* 按照weight值来排序
* 判断是否传递数据给plan_activity
*/
MapUtil.sortByValue(weightMap);
//排好序后计算距离
Iterator it = weightMap.entrySet().iterator();
int order = 0;
while (it.hasNext()) {
order++;
Map.Entry entry = (Map.Entry) it.next();
objectMap.put(new Integer(order), objectMap.get(entry.getKey()));
}
PlanTask planTask = new PlanTask();//封装了每个plan计算的方法
for (Map.Entry<Integer, Double> entry : weightMap.entrySet()) {
System.out.println("id= " + entry.getKey());
double play_time = planTask.calPlay_time(weightMap.size(),
weightMap.get(entry.getKey()), travelTime);
double driving_time = planTask.calDrive_time(DistanceUtil.distance(
objectMap.get(entry.getKey()).getLatitude(),
objectMap.get(entry.getKey()).getLongitude(),
objectMap.get(entry.getKey() + 1).getLatitude(),
objectMap.get(entry.getKey() + 1).getLongitude()
));
String arrive_time = "hello world";//未完待续
String place_name = nameMap.get(entry.getKey());
Plan plan = new Plan(place_name, arrive_time, driving_time, play_time);
//传递plan对象list
planList.add(entry.getKey(), plan);
}
return planList;
}
公共类DestinationActivity扩展活动实现OnClickListener、NumberPicker.OnValueChangeListener{
地点、日期、天数、开始时间、结束时间、号码、金钱视图的私人文本视图;
私有按钮addButton,subButton;
私人图像查看后退按钮,电话;
私有列表视图列表视图;
专用按钮目的地OK_btn;
专用ShapeLoadingDialog ShapeLoadingDialog;
私人小费;
私人日历;
专用日期选择器对话框;
专用时间选择器对话框2;
private List destinationList=new ArrayList();
专用目的适配器;
私有整数_值=1;
私有字符串时间\u开始;
私有字符串时间结束;
私人旅行日;
双行程时间;//总播放时间
双行程时间;
私人货币;
私有int num=1;
private ArrayList select_placeID=new ArrayList();
公共地图;
public List planList=new ArrayList();
int[]selectedID=newint[10];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.destination_layout);
//初始化云数据库
初始化(这是BmobConfig.APP_ID);
listView=(listView)findViewById(R.id.list\u目的地);
destinationOk_btn=(按钮)findViewById(R.id.okButton);
initDestinations();//初始化数据
适配器=新的DestinationAdapter(destinationList,DestinationActivity.this);
//adapter=newdestinationadapter(this,destinationList,DestinationAdapter.getIsSelected());
setAdapter(适配器);
//..侦听器和文本视图。。。。。。。
//提交按钮
destinationOk_btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
选择_placeID.clear();
对于(int i=0;i putParcelableArrayListExtra(“planninfo”,(ArrayList很不幸,我不能理解太多,因为我还不太适应反应式编程,因为平台通常使用很多rxjava,但是这里有一些东西可以稍微增强代码
final HashMap<Integer, Integer> pMap = new HashMap<Integer, Integer>();
final HashMap<Integer, String> nameMap = new HashMap<Integer, String>();
final HashMap<Integer, Destination> objectMap = new LinkedHashMap<Integer, Destination>();
/**
* get the data from cloud database
*/
BmobQuery<Destination> query = new BmobQuery<Destination>();
// this time only one list of three elements is added instead of three lists of one element each
query.addWhereContainedIn("id", Arrays.asList(selectedID));
query.findObjects(new FindListener<Destination>() {
@Override
public void done(List<Destination> list, BmobException e) {
if (e == null) {
System.out.println("success:total" + list.size() + "items。");
for (Destination destination : list) {
int p = destination.getPriority();
int id = destination.getId();
String name = destination.getName();
double longitude = destination.getLongitude();
double latitude = destination.getLatitude();
objectMap.put(id, new Destination(longitude, latitude));
System.out.println(id);
//calculate the distance
double dis = DistanceUtil.distance(start.getLongitude(), start.getLatitude(),
longitude, latitude);
pMap.put(id, p);
weightMap.put(id, new Double(dis));
nameMap.put(id, name);
}
// continue execution here though you won't be able to return a list of plans here
} else {
Log.i("bmob", "error:" + e.getMessage() + "," + e.getErrorCode());
}
}
});
final HashMap pMap=new HashMap();
final HashMap nameMap=新HashMap();
final HashMap objectMap=新LinkedHashMap();
/**
*从云数据库获取数据
*/
BmobQuery query=新的BmobQuery();
//这一次只添加了三个元素的一个列表,而不是每个元素的三个列表
query.addWhereContainedIn(“id”,Arrays.asList(selectedID));
query.findObjects(新的FindListener(){
@凌驾
公共作废完成(列表,BmobException e){
如果(e==null){
System.out.println(“success:total”+list.size()+“items”);
用于(目的地:列表){
int p=destination.getPriority();
int id=destination.getId();
字符串名称=destination.getName();
double longitude=destination.getLongitude();
双纬度=destination.getLatitude();
put(id,新目的地(经度、纬度));
系统输出打印项次(id);
//计算距离
double dis=DistanceUtil.distance(start.getLongitude(),start.getLatitude(),
经纬度);
pMap.put(id,p);
权重映射put(id,新的Double(dis));
nameMap.put(id,name);
}
//在此处继续执行,但无法在此处返回计划列表
}否则{
Log.i(“bmob”,“error:+e.getMessage()+”,“+e.getErrorCode());
}
}
});
希望这有帮助:)List listmap=new ArrayList;
最终HashMap对象映射;
/**
*从云数据库获取数据
*/
BmobQuery query=新的BmobQuery();
//这一次只添加了三个元素的一个列表,而不是每个元素的三个列表
query.addWhereContainedIn(“id”,Arrays.asList(selectedID));
query.findObjects(新的FindListener(){
@凌驾
公共作废完成(列表,BmobException e){
如果(e==null){
System.out.println(“success:total”+list.size()+“items”);
用于(目的地:列表){
objectMap=新建LinkedHashMap();
int id=destination.getId();
objectMap.put(id,目的地);
add(objectMap);
}
//在此处继续执行,但无法在此处返回计划列表
}否则{
Log.i(“bmob”,“err”
final HashMap<Integer, Integer> pMap = new HashMap<Integer, Integer>();
final HashMap<Integer, String> nameMap = new HashMap<Integer, String>();
final HashMap<Integer, Destination> objectMap = new LinkedHashMap<Integer, Destination>();
/**
* get the data from cloud database
*/
BmobQuery<Destination> query = new BmobQuery<Destination>();
// this time only one list of three elements is added instead of three lists of one element each
query.addWhereContainedIn("id", Arrays.asList(selectedID));
query.findObjects(new FindListener<Destination>() {
@Override
public void done(List<Destination> list, BmobException e) {
if (e == null) {
System.out.println("success:total" + list.size() + "items。");
for (Destination destination : list) {
int p = destination.getPriority();
int id = destination.getId();
String name = destination.getName();
double longitude = destination.getLongitude();
double latitude = destination.getLatitude();
objectMap.put(id, new Destination(longitude, latitude));
System.out.println(id);
//calculate the distance
double dis = DistanceUtil.distance(start.getLongitude(), start.getLatitude(),
longitude, latitude);
pMap.put(id, p);
weightMap.put(id, new Double(dis));
nameMap.put(id, name);
}
// continue execution here though you won't be able to return a list of plans here
} else {
Log.i("bmob", "error:" + e.getMessage() + "," + e.getErrorCode());
}
}
});
List<HashMap<Integer, Destination>> listmap=new ArrayList<>;
final HashMap<Integer, Destination> objectMap;
/**
* get the data from cloud database
*/
BmobQuery<Destination> query = new BmobQuery<Destination>();
// this time only one list of three elements is added instead of three lists of one element each
query.addWhereContainedIn("id", Arrays.asList(selectedID));
query.findObjects(new FindListener<Destination>() {
@Override
public void done(List<Destination> list, BmobException e) {
if (e == null) {
System.out.println("success:total" + list.size() + "items。");
for (Destination destination : list) {
objectMap = new LinkedHashMap<Integer, Destination>();
int id = destination.getId();
objectMap.put(id, destination);
listmap.add(objectMap);
}
// continue execution here though you won't be able to return a list of plans here
} else {
Log.i("bmob", "error:" + e.getMessage() + "," + e.getErrorCode());
}
}