【问题标题】:fill listView from a HashMap data passed from another activity, android从另一个活动传递的HashMap数据填充listView,android
【发布时间】:2013-11-27 07:28:47
【问题描述】:

我正在编写一个 android 应用程序来使用 Google Places API 查看附近的位置,我的应用程序包含两个活动,第一个活动找到附近的位置,将有关这些位置的信息存储在 HashMap 中,并将它们显示在地图上。 在第二个活动中,我想从第一个活动的 HashMap 中获取位置名称并将它们显示在 ListView 中。

第一个活动:

 HashMap<String, String> hmPlace;

protected void onPostExecute(List> list){

        // Clears all the existing markers
        mGoogleMap.clear();

        for(int i=0;i<list.size();i++){

            // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Getting a place from the places list
            //HashMap<String, String>
            hmPlace = list.get(i);

            // Getting latitude of the place
            double lat = Double.parseDouble(hmPlace.get("lat"));

            // Getting longitude of the place
            double lng = Double.parseDouble(hmPlace.get("lng"));

            // Getting name
            String name = hmPlace.get("place_name");

           // listP[i]=hmPlace.get("place_name");
            Log.d("places=",hmPlace.get("place_name"));

            // Getting vicinity
            String vicinity = hmPlace.get("vicinity");

            LatLng latLng = new LatLng(lat, lng);

            // Setting the position for the marker
            markerOptions.position(latLng);



            // Setting the title for the marker.
            //This will be displayed on taping the marker
            markerOptions.title(name + " : " + vicinity);

            // Placing a marker on the touched position
            Marker m = mGoogleMap.addMarker(markerOptions);

            // Linking Marker id and place reference
            mMarkerPlaceLink.put(m.getId(), hmPlace.get("reference"));
        }  
    }

我是这样传递HashMap的:

intent = new Intent(getApplicationContext(), List_airports.class);
                intent.putExtra("com.example.dashboard_our.hmPlace",hmPlace);

                startActivity(intent);

我在第二个活动中从 HashMap 中获取值:

final ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; i < places1.size(); ++i) {

                list.addAll(places1.values());

            }

places1 是我从另一个活动中获得的 HashMap:

HashMap<String, String> places1=(HashMap<String, String>) extras.getSerializable("com.example.dashboard_our.hmPlace");

但它只带第一个元素并打印多次

这是第二个活动的其余部分:

final StableArrayAdapter adapter = new StableArrayAdapter(this,
                android.R.layout.simple_list_item_1, list);
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> parent, final View view,
                  int position, long id) {
                final String item = (String) parent.getItemAtPosition(position);
                view.animate().setDuration(2000).alpha(0)
                    .withEndAction(new Runnable() {
                      @Override
                      public void run() {
                        list.remove(item);
                        adapter.notifyDataSetChanged();
                        view.setAlpha(1);
                      }
                    });
              }

            });
          }

          private class StableArrayAdapter extends ArrayAdapter<String> {

            HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

            public StableArrayAdapter(Context context, int textViewResourceId,
                List<String> objects) {
              super(context, textViewResourceId, objects);
              for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
              }
            }

            @Override
            public long getItemId(int position) {
              String item = getItem(position);
              return mIdMap.get(item);
            }

            @Override
            public boolean hasStableIds() {
              return true;
            }

          }

【问题讨论】:

  • 从intent获取数组列表的代码在哪里???
  • 你知道数据控制器类老兄保存该类中的所有数据并在任何地方使用它
  • 这里hmPlace是什么,你发送的hmPlace不是完整的Hashmap
  • 不要只是这样传递数据,当有这么多的活动一起交互时,您将失去所有控制:(
  • @BhanuSharma 我该怎么做?

标签: android listview hashmap


【解决方案1】:

像这样创建一个数据控制器类

公共类数据控制器 {

    private static DataController ref;

    public static DataController getInstance()
    {
        if(ref==null)
            ref = new DataController();

        return ref;
    }

public HashMap hashmap_datacntrlr = new HashMap();

}

从此你得到一个单例类,它在整个应用程序中只有一个实例,现在你只需创建任何你想使用的数据类型并保存,就像我提到的 hashmap 现在只需在你获取数据的地方执行此操作并保存在数据控制器中这个

DataController datacntrl;

在创建时

datacntrl = DataController.getInstance();

还有多余的hasmap,比如datacntrl.hashmap_datacntrlr = 粘贴你必须有相同数据类型的数据;

现在在您的目标类中再次创建 datacontoller 实例和多余的并在您想要显示数据的位置获取 datacntrl.hashmap_datacntrlr

当我们使用旧数据而不是从服务器一次又一次触发时,它的调用缓存:)

也许它会帮助你实现你的目标 祝你好运,伙计:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多