我是关于Android的新手,我已经尝试填充从Listview的改装响应中获得的数据,但结果是空白的。
这是我调用retrofit并调用ListView适配器的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_dashboard_view, container, false);
ButterKnife.bind(getActivity());
int getUkmId = getArguments().getInt("viewUkmId");
getViewReport(getUkmId);
return view;
}
public void getViewReport(int UkmId){
final ViewReportRequest viewReportRequest = new ViewReportRequest(UkmId);
Call<ViewReportResponse> call = apiService.viewReport(viewReportRequest);
call.enqueue(new Callback<ViewReportResponse>() {
@Override
public void onResponse(Call<ViewReportResponse> call, Response<ViewReportResponse> response) {
if(response.body().getData() != null){
final ArrayList<ViewReportResponseData> viewReportResponseData = response.body().getData();
ListView listView = getView().findViewById(R.id.ukmView);
UkmViewDataAdapter adapter = new UkmViewDataAdapter(viewReportResponseData, getContext());
listView.setAdapter(adapter);
}else{
Toast.makeText(getContext(), "Failed Get View Report", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ViewReportResponse> call, Throwable t) {
}
});
}
以下是我的适配器代码
public class UkmViewDataAdapter extends BaseAdapter {
private Context context;
private ArrayList<ViewReportResponseData> viewReportResponses;
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public UkmViewDataAdapter(ArrayList<ViewReportResponseData> viewReportResponseData, Context context){
super();
this.viewReportResponses = viewReportResponseData;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = inflater.inflate(R.layout.fragment_dashboard_view_list, null);
holder = new ViewHolder();
holder.viewStartDate = convertView.findViewById(R.id.viewStartDate);
holder.viewEndDate = convertView.findViewById(R.id.viewEndDate);
holder.viewFund = convertView.findViewById(R.id.viewFund);
convertView.setTag(holder);
}
holder.viewStartDate.setText(viewReportResponses.get(position).getStartDate());
holder.viewEndDate.setText(viewReportResponses.get(position).getEndDate());
holder.viewFund.setText(viewReportResponses.get(position).getAmount());
return convertView;
}
public class ViewHolder {
public TextView viewStartDate;
public TextView viewEndDate;
public TextView viewFund;
}
}
我也成功从改造中获取响应数据,下面是okhttp3响应
{
"code": 1000,
"message": "Success",
"data": [
{
"ukm_id": 2,
"id": 2,
"start_date": "2018-04-02T00:00:00Z",
"end_date": "2018-11-18T00:00:00Z",
"amount": 4000000
},
{
"ukm_id": 2,
"id": 3,
"start_date": "2018-04-01T00:00:00Z",
"end_date": "2018-05-01T00:00:00Z",
"amount": 2000000
}
]
}
答案 0 :(得分:1)
@Override
public int getCount() {
return 0;
}
you are returning 0, instead return the count.
@Override
public Object getItem(int position) {
return null;
}
your are returning null, instead return list position.
@Override
public long getItemId(int position) {
return 0;
}
your are returning null, instead return id .
see below code:
@Override
public int getCount() {
return viewReportResponses.size();
}
@Override
public Object getItem(int position) {
return viewReportResponses.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public UkmViewDataAdapter(ArrayList<ViewReportResponseData> viewReportResponseData, Context context){
super();
this.viewReportResponses = viewReportResponseData;
this.context = context;
}