Android多标记依赖于SQLite数据的listview
我正在创建一个应用程序,它显示存储在sqlite中的学生列表。当我点击一个列表时,学生的标记将出现在地图上。是否可能在我运行应用程序时,根据listview显示多个学生标记,当我按next时,标记也将根据下一个学生列表的位置而改变。希望你能帮助我,因为我只是在用谷歌地图创建一个安卓应用程序方面才有新意。我的代码位于下面,我删除了onitemlistener,因此代码将最小化Android多标记依赖于SQLite数据的listview,android,sqlite,google-maps,listview,android-studio,Android,Sqlite,Google Maps,Listview,Android Studio,我正在创建一个应用程序,它显示存储在sqlite中的学生列表。当我点击一个列表时,学生的标记将出现在地图上。是否可能在我运行应用程序时,根据listview显示多个学生标记,当我按next时,标记也将根据下一个学生列表的位置而改变。希望你能帮助我,因为我只是在用谷歌地图创建一个安卓应用程序方面才有新意。我的代码位于下面,我删除了onitemlistener,因此代码将最小化 public class Student extends AppCompatActivity implements Loc
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;
}
}
}
公共类学生扩展AppCompatActivity实现LocationListener{
谷歌地图;
上下文=这个;
数据库助手dbhelper;
DatabaseHelper db=新的DatabaseHelper(此);
int指数=0;
int currentPageIndex=0;
学生列表视图;
列出GetAllStudent;
按钮btnback,btnnext;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.student);
dbhelper=新数据库helper(Student.this);
试一试{
dbhelper.createDataBase();
}
捕获(IOE异常){
e、 printStackTrace();
}
试一试{
dbhelper.openDataBase();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
GetAllStudent=dbhelper.GetAllStudent(索引);
lvstudent=(ListView)findViewById(R.id.student\u列表);
lvstudent.setAdapter(新的ViewAdapterStudentList());
btnback=(按钮)findViewById(R.id.studentBack);
btnnext=(按钮)findViewById(R.id.studentNext);
btnback.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图转换视图){
currentPageIndex-=20;
GetAllStudent=dbhelper.GetAllStudent(currentPageIndex);
lvstudent=(ListView)findViewById(R.id.student\u列表);
lvstudent.setAdapter(新的ViewAdapterStudentList());
setListViewHeightBasedOnChildren(lvstudent);
}
});
btnnext.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图转换视图){
currentPageIndex+=20;
GetAllStudent=dbhelper.GetAllStudent(currentPageIndex);
lvstudent=(ListView)findViewById(R.id.student\u列表);
lvstudent.setAdapter(新的ViewAdapterStudentList());
setListViewHeightBasedOnChildren(lvstudent);
}
});
/****************************************************************************************
*地图
****************************************************************************************/
MapFragment MapFragment=(MapFragment)getFragmentManager().findFragmentById(R.id.map);
//获取地图对象的步骤
map=mapFragment.getMap();
map.getUiSettings().setZoomControlsEnabled(true);
//设置位置管理器的步骤
//LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
//请求位置更新
//locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,1,1,此);
}
//扩展LISTVIEW列表
私有void setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
返回;
int desiredWidth=View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.UNSPECIFIED);
整数总高度=60;
视图=空;
对于(int i=0;i