vendredi 18 décembre 2015

Android Multiple Marker depends on the listview of SQLite Data

I'm creating an app that display a list of students stored in sqlite. When i click a list, marker of the student will appear on the map. Is it possible that when i run the app, multiple markers of students will appear depends on the listview and when i press next, markers also will change depends on the location of the next list of students. Hope you can help me since im new only in creating an android app with google map. My code is located below and i remove the onitemlistener so the code will be minimize.

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;
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire