2015-12-18 24 views
0

我正在创建一个应用程序,显示存储在sqlite中的学生列表。当我点击一个列表时,学生的标记将出现在地图上。是否有可能当我运行该应用时,学生的多个标记将出现取决于列表视图,并且当我按下一个标记时,标记也会改变,这取决于下一个学生列表的位置。希望你能帮助我,因为我只是在创建一个谷歌地图的Android应用程序。我的代码位于下方,我删除了onitemlistener,因此代码将最小化。Android多标记取决于SQLite数据的列表视图

public class Student extends AppCompatActivity implements LocationListener { 

    GoogleMap map; 

    Context context = this; 
    DatabaseHelper dbhelper; 
    DatabaseHelper db = new DatabaseHelper(this); 

    int index = 0; 
    int currentPageIndex = 0; 

    ListView lvstudent;        
    List<StudentModel> GetAllStudent;     
    Button btnback, btnnext;       


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.student); 
     dbhelper = new DatabaseHelper(Student.this); 

     try{ 
      dbhelper.createDataBase(); 
     } 
     catch(IOException e){ 
      e.printStackTrace(); 
     } 
     try { 
      dbhelper.openDataBase(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     GetAllStudent = dbhelper.getAllStudent(index); 
     lvstudent = (ListView) findViewById(R.id.student_list); 
     lvstudent.setAdapter(new ViewAdapterStudentList()); 

     btnback = (Button) findViewById(R.id.studentBack); 
     btnnext = (Button) findViewById(R.id.studentNext); 
     btnback.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View convertView) { 

       currentPageIndex -= 20; 
       GetAllStudent = dbhelper.getAllStudent(currentPageIndex); 
       lvstudent = (ListView) findViewById(R.id.student_list); 
       lvstudent.setAdapter(new ViewAdapterStudentList()); 
       setListViewHeightBasedOnChildren(lvstudent); 
      } 
     }); 

     btnnext.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View convertView) { 

       currentPageIndex += 20; 
       GetAllStudent = dbhelper.getAllStudent(currentPageIndex); 
       lvstudent = (ListView) findViewById(R.id.student_list); 
       lvstudent.setAdapter(new ViewAdapterStudentList()); 
       setListViewHeightBasedOnChildren(lvstudent); 
      } 
     }); 

     /**************************************************************************************** 
     *          MAP 
     ****************************************************************************************/ 
     MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 

     //To get map object 
     map = mapFragment.getMap(); 
     map.getUiSettings().setZoomControlsEnabled(true); 

     //To setup location manager 
     //LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     //To request location updates 
     //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this); 


    } 


    //EXTEND LIST OF LISTVIEW 
    private void setListViewHeightBasedOnChildren(ListView listView) { 
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) 
      return; 
     int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); 
     int totalHeight = 60; 
     View view = null; 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      view = listAdapter.getView(i, view, listView); 
      if (i == 0) 
       view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT)); 

      view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 
      totalHeight += view.getMeasuredHeight(); 
     } 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() + 1)); 
     listView.setLayoutParams(params); 
    } 

    /**************************************************************************************** 
    *         MAP USER LOCATION 
    ****************************************************************************************/ 
    @Override 
    public void onLocationChanged(Location location) { 

     //To clear map data 
     map.clear(); 

     //To hold location 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 

     //To create marker in map 
     MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)); 
     markerOptions.position(latLng); 
     markerOptions.title("You are here"); 
     //adding marker to the map 
     map.addMarker(markerOptions); 

     //opening position with some zoom level in the map 
     map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f)); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

    public class ViewAdapterStudentList extends BaseAdapter { 

     LayoutInflater mInflater; 

     public ViewAdapterPharmacyList() { 
      mInflater = LayoutInflater.from(context); 
     } 

     @Override 
     public int getCount() { 
      return GetAllStudent.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.item_pharmacy,null); 
      } 

      final TextView names = (TextView) convertView.findViewById(R.id.studentlist_name); 
      final TextView address = (TextView) convertView.findViewById(R.id.studentlist_address); 

      names.setText(GetAllStudent.get(position).getlisting_title()); 
      address.setText(GetAllStudent.get(position).getstreet()+", "+GetAllStudent.get(position).getcity_name()+", "+GetAllStudent.get(position).getprovince_name()); 

      return convertView; 
     } 
    } 

} 

回答

0

是的,您可以选择ListView上的多个项目。 Vogella有一个关于how to implement it的教程,如果你愿意,你可以检查一下。

一旦你有了,从ListView中获取选定的项目,并将标记添加到它。

相关问题