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