列表视图显示重叠项的ID,而不是标题

时间:2012-07-23 10:30:12

标签: android listview

在设置我的列表视图时,我将OverlayItems的Arraylist设置为listview的适配器,因为当我点击列表中的项目时,我想访问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

    }

}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

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