我已经尝试过这段代码并且它在活动中运行得很好但是我将其更改为片段,现在ListView没有显示但我可以在日志中看到数据,但不能在屏幕上看到。
我已经尝试过在这个问题上给出的每个解决方案已经像最后的返回视图等仍然无法正常工作。有帮助吗?这是我的片段代码
public class BloodBankGovtFragment extends Fragment {
public static BloodBankGovtFragment newInstance() {
return new BloodBankGovtFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_bloodbank, container, false);
final ListView listView1 = (ListView) view.findViewById(R.id.listView);
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
progressDialog.setCancelable(false);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<Hospitals> call = api.getHospitals();
call.enqueue(new Callback<Hospitals>() {
@Override
public void onResponse(Call<Hospitals> call, Response<Hospitals> response) {
progressDialog.dismiss();
Hospitals hospitals = response.body();
String[] hospitalsName = new String[hospitals.getRecords().size()];
String[] ContactNumber = new String[hospitals.getRecords().size()];
for(int i= 0;i<hospitals.getRecords().size();i++)
{
Log.i("hello", "onResponse: "+hospitals.getRecords().get(i).getBloodBankName());
hospitalsName[i]= hospitals.getRecords().get(i).getBloodBankName();
// ContactNumber[i] = hospitals.getRecords().get(i).getContactNo();
}
listView1.setAdapter(new ArrayAdapter<String>
(getActivity(),android.R.layout.simple_list_item_1, hospitalsName));
}
@Override
public void onFailure(Call<Hospitals> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(getActivity(), "failed to fetch", Toast.LENGTH_LONG).show();
t.printStackTrace();
}
});
return view;
}
` 这是我的片段xml代码。
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:id="@+id/setting"
android:background="#FFF"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/toolbar_bloodbank_fragment" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
>
</ListView>
</LinearLayout>
</RelativeLayout>`