你好,我想在选项卡布局内的回收站视图中显示附近的医院,但是回收站视图甚至都没有显示,并且在调试控件时甚至都没有进入Volly的onResponse()进行JSON解析...请帮助我>
URL =“ https://maps.googleapis.com/maps/api/place/search/json?location=18.5668801,73.8290394&radius=3000&sensor=true&type=hospital&key=AIzaSyBLEPBRfw7sMb73Mr88L91Jqh3tuE4mKsE”
item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!--<ImageView-->
<!--android:id="@+id/image_view"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="200dp"-->
<!--android:src="@mipmap/jon_snow" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_view_creator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Creator name"
android:textColor="@android:color/black"
android:textSize="20sp" />
<TextView
android:id="@+id/text_view_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rating : "
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:orientation="vertical"-->
<!--android:paddingTop="8dp"-->
<!--android:layout_marginLeft="50dp">-->
<!--<TextView-->
<!--android:id="@+id/text_view_tags"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Tags : "-->
<!--android:textColor="@android:color/black"-->
<!--android:textSize="14sp" />-->
<!--<TextView-->
<!--android:id="@+id/text_view_downloads"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Downloads : "-->
<!--android:textColor="@android:color/black"-->
<!--android:textSize="14sp" />-->
<!--</LinearLayout>-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
tabsPager.java
public class tabsPager extends FragmentStatePagerAdapter {
private final ArrayList<Fragment> fragmentList=new ArrayList<>();
private final ArrayList<String> fragmentListTitles=new ArrayList<>();
public tabsPager(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
public void addFragment(Fragment fragment,String title){
fragmentList.add(fragment);
fragmentListTitles.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentListTitles.get(position);
}
}
SecondFragment.java
public class SecondFragment extends Fragment {
private RecyclerView mrecyclerView;
private ListAdapter mListAdapter;
private ArrayList<PojoList> mPojolist;
private RequestQueue mRequestQueue;
// String url;
double latitude;
double longitude;
private FusedLocationProviderClient client;
String url;
RecyclerView.LayoutManager mLayoutManager;
public SecondFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mPojolist=new ArrayList<>();
View v= inflater.inflate(R.layout.fragment_second, container, false);
mrecyclerView=v.findViewById(R.id.recycler_view);
mrecyclerView.setHasFixedSize(true);
mrecyclerView=new RecyclerView(getActivity());
mLayoutManager=new LinearLayoutManager(this.getActivity());
mrecyclerView.setLayoutManager(mLayoutManager);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requestPermission();
//mrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayout.VERTICAL,false));
mRequestQueue= Volley.newRequestQueue(getContext());
client = LocationServices.getFusedLocationProviderClient(getContext());
if (ActivityCompat.checkSelfPermission(getActivity(), ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
client.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null){
latitude=location.getLatitude();
longitude=location.getLongitude();
String url ="https://maps.googleapis.com/maps/api/place/search/json?location="+latitude+","+longitude+"&radius=3000&sensor=true&type=hospital&key=AIzaSyBLEPBRfw7sMb73Mr88L91Jqh3tuE4mKsE";
//parseJson();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,url,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("BNT",response.toString());
JSONArray jsonArray=response.getJSONArray("results");
for(int i=0;i<jsonArray.length();i++){
JSONObject resultObject=jsonArray.getJSONObject(i);
String name=resultObject.getString("name");
Log.d("Name",name.toString());
// int rating=resultObject.getInt("rating");
//Log.d("Rating", String.valueOf(rating));
// JSONArray photoArray=resultObject.getJSONArray("photos");
// for(int j=0;j<photoArray.length();j++){
// JSONObject photoObject=photoArray.getJSONObject(j);
// String imgUrl=photoObject.getString("photo_reference");
//
// ;
// }
mPojolist.add(new PojoList(name));
Log.d("PojoList",mPojolist.toString());
}
mListAdapter=new ListAdapter(getContext(),mPojolist);
Log.d("MListAdapter",mListAdapter.toString());
mrecyclerView.setAdapter(mListAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
Log.d("mRequestQueue",mRequestQueue.toString());
}
}
});
}
private void requestPermission(){
ActivityCompat.requestPermissions(getActivity(),new String[]{ACCESS_FINE_LOCATION},1);
}
}
fragmentSecond.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/darker_gray" />
</RelativeLayout>
</FrameLayout>