1

在设置我的列表视图时,我将 OverlayItems 的 Arraylist 设置为列表视图的适配器,因为当我单击列表中的项目时,我想访问 Overlayitems 附带的地理点。

然而,这个解决方案给出了一个相当丑陋的列表视图,因为它没有显示 Overlayitems 的不同标题,而是显示类似OverlayItem@jiozl26.

有谁知道解决这个问题的最简单方法是什么(既显示标题,又能访问地理点?)我想我应该使用自定义列表视图来做到这一点,但我不确定这是最简单/最有效的方式。

adapter = new ArrayAdapter<OverlayItem>(this, android.R.layout.simple_list_item_1, custom.pinpoints);
        listView.setAdapter(adapter);

==>整个代码:

public class Main extends MapActivity implements OnTabChangeListener{
    /** Called when the activity is first created. */
    private static final String LIST_TAB_TAG = "List";
    private static final String MAP_TAB_TAG = "Map";

    MapView map;
    ListView listView;

    TabHost tabHost;

    long start;
    long stop;
    int x, y;

    MyLocationOverlay compass;
    MyLocationOverlay MyLoc;
    MapController controller;

    GeoPoint touchedPoint;
    Drawable d;
    List<Overlay> overlayList;
    CustomPinpoint custom;

    static Context context;
    ArrayAdapter<OverlayItem> adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        context = getApplicationContext();

        listView = (ListView) findViewById(R.id.list);
        listView.setEmptyView((TextView) findViewById(R.id.empty));
        d = getResources().getDrawable(R.drawable.ic_launcher);
        custom = new CustomPinpoint(d,Main.this);
        listView.setLongClickable(true);
        adapter = new ArrayAdapter<OverlayItem>(this, android.R.layout.simple_list_item_1, custom.pinpoints);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GeoPoint geoPoint = ((OverlayItem) listView.getAdapter().getItem(position)).getPoint();
                if(geoPoint != null) {

                    map.getController().animateTo(geoPoint);

                    tabHost.setCurrentTab(1);
                        }
                    }
            });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                final AlertDialog alert3 = new AlertDialog.Builder(Main.this).create();     
                alert3.setTitle("Pick an option.");
                alert3.setButton(DialogInterface.BUTTON_POSITIVE,"Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
            });
                alert3.setButton(DialogInterface.BUTTON_NEUTRAL,"Modify", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        LayoutInflater factory = LayoutInflater.from(context);
                        View promptsView = factory.inflate(R.layout.prompts, null);

                        final AlertDialog alert4 = new AlertDialog.Builder(Main.this).create();
                        alert4.setView(promptsView);
                            final EditText userInput1 = (EditText) promptsView.findViewById(R.id.editTextInput1);
                            final EditText userInput2 = (EditText) promptsView.findViewById(R.id.editTextInput2);
                            userInput1.setInputType(InputType.TYPE_CLASS_TEXT);
                            userInput1.setCursorVisible(true);
                            userInput2.setInputType(InputType.TYPE_CLASS_TEXT);
                            userInput2.setCursorVisible(true);
                            alert4.setButton (DialogInterface.BUTTON_NEGATIVE,"Save", new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                    custom.modifyPinpoint(userInput1.getText().toString(), userInput2.getText().toString(),position);
                                                    map.postInvalidate(); 
                                                        }
                            });         

                            alert4.setButton(DialogInterface.BUTTON_POSITIVE, "Cancel", new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                        }                       
                                });     
                    alert4.show();

                    }
            });
                alert3.setButton(DialogInterface.BUTTON_NEGATIVE,"Delete", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            custom.deletePinpoint(position);
                            adapter.notifyDataSetChanged();
                        }
                });
        alert3.show();
        return true;
            }
            });


        map = (MapView) findViewById(R.id.mapview);
        map.setBuiltInZoomControls(true);
        map.postInvalidate();

        Touch t = new Touch();
        overlayList = map.getOverlays();
        overlayList.add(t);
        compass = new MyLocationOverlay(Main.this, map);
        overlayList.add(compass);
        controller = map.getController();



        MyLoc = new MyLocationOverlay(Main.this, map);
        overlayList.add(MyLoc);
        map.postInvalidate();
        MyLoc.runOnFirstFix(new Runnable() {
            public void run() {
                map.getController().animateTo(MyLoc.getMyLocation());
                }
        }); 

        tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG).setIndicator("List").setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return listView;
            }
        }));
        tabHost.addTab(tabHost.newTabSpec(MAP_TAB_TAG).setIndicator("Map").setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return map;
            }
        }));

        tabHost.setCurrentTab(1);
        tabHost.setCurrentTab(0);


    }



    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        compass.disableCompass();
        super.onPause();
        MyLoc.disableMyLocation();
        finish();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        compass.enableCompass();
        super.onResume();
        MyLoc.enableMyLocation();

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }



    class Touch extends Overlay {
        public boolean onTouchEvent(MotionEvent e, MapView m) {
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                start = e.getEventTime();
                x = (int) e.getX();
                y = (int) e.getY();
                touchedPoint = map.getProjection().fromPixels(x, y);    
                        }
            if (e.getAction() == MotionEvent.ACTION_UP) {
                stop = e.getEventTime();
                        }
            if (stop - start > 1200) {
                final AlertDialog alert = new AlertDialog.Builder(Main.this).create();
                alert.setTitle("Pick an option.");
                alert.setButton(DialogInterface.BUTTON_POSITIVE,"Place a pinpoint.", new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {

                                    LayoutInflater factory = LayoutInflater.from(context);
                                    View promptsView = factory.inflate(R.layout.prompts, null);

                                    final AlertDialog alert2 = new AlertDialog.Builder(Main.this).create();
                                    alert2.setView(promptsView);
                                        final EditText userInput1 = (EditText) promptsView.findViewById(R.id.editTextInput1);
                                        final EditText userInput2 = (EditText) promptsView.findViewById(R.id.editTextInput2);
                                        userInput1.setInputType(InputType.TYPE_CLASS_TEXT);
                                        userInput1.setCursorVisible(true);
                                        userInput2.setInputType(InputType.TYPE_CLASS_TEXT);
                                        userInput2.setCursorVisible(true);
                                        alert2.setButton (DialogInterface.BUTTON_NEGATIVE,"Save", new DialogInterface.OnClickListener() {

                                                        public void onClick(DialogInterface dialog, int whichButton) {
                                                                OverlayItem overlayItem = new OverlayItem(touchedPoint, userInput1.getText().toString(), userInput2.getText().toString());
                                                                custom.insertPinpoint(overlayItem);
                                                                overlayList.add(custom);
                                                                map.postInvalidate(); 
                                                                    }
                                            });         

                                        alert2.setButton(DialogInterface.BUTTON_POSITIVE, "Cancel", new DialogInterface.OnClickListener() {

                                                        public void onClick(DialogInterface dialog, int whichButton) {
                                                                    }                       
                                            });     
                                alert2.show();
                                }
                });  

                alert.setButton(DialogInterface.BUTTON_NEUTRAL,"Get address.",new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog,int which) {
                                // TODO Auto-generated method stub

                                    Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
                                        try{

                                                List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() /1E6, touchedPoint.getLongitudeE6()/1E6, 1);                          

                                                if (address.size() > 0){
                                                    String display = "";                                                
                                                    for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++){

                                                        display += address.get(0).getAddressLine(i) + "\n";
                                                        }               
                                                    Toast t3 = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
                                                    t3.show();
                                                }

                                        } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }finally{

                                        }

                            }
                });

                alert.setButton(DialogInterface.BUTTON_NEGATIVE,"Toggle View", new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub

                                        if (map.isSatellite()){
                                            map.setSatellite(false);

                                            }else{

                                            map.setSatellite(true);
                                            }


                                }
                });
                alert.show();
                return true;
            }
            return false;
        }
    }




     public void gpsCurrentLocation()
     {

         tabHost.setCurrentTab(1);
         GeoPoint p = MyLoc.getMyLocation();
         if (p == null){
             Toast.makeText(Main.this, "Your location wasn't found.", Toast.LENGTH_SHORT).show();
         }
         else{
         map.getController().animateTo(p);
         }

     }


    // Menu XML file (menu.xml)
    @Override
     public boolean onCreateOptionsMenu(Menu menu)
     {
     MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.map_main, menu);

               return true;
     }

    @Override
     public boolean onOptionsItemSelected(MenuItem item)
     {

     switch (item.getItemId())
     {
     case R.id.my_location:
     Toast.makeText(Main.this, "Moving To Current location", Toast.LENGTH_SHORT).show();
     gpsCurrentLocation();
     return true;
     case R.id.menu_clear:   
         custom.clearPinpoint();
         adapter.notifyDataSetChanged();

     }

    return false;
     }



    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub

    }

}

4

2 回答 2

2

只需覆盖toString()您的子类的方法OverlayItem即可返回该对象的标题。

于 2012-07-23T10:48:58.850 回答
2

使用自定义数组适配器扩展ArrayAdapter<OverlayItem>. 您可以在重写getView()方法中定义项目的视图,这样您就可以更好地控制显示的内容和方式。有很多关于如何创建自定义数组适配器的教程。例如参见https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

您会看到OverlayItem@jiozl26,因为 ArrayAdapter 的默认行为是显示toString()数组中的每个对象。

于 2012-07-23T10:47:59.893 回答