您好我有以下代码,用我使用Retrofit http客户端收到的数据填充列表视图。
listView = (ListView) findViewById(R.id.list_item_details);
if (mlabAPi.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please obtain your API KEY from!", Toast.LENGTH_LONG).show();
return;
}
ApiInterface apiService =
MlabApiClient.getClient().create(ApiInterface.class);
Call<List<Trail>> call = apiService.byTrailID(countyString,mlabAPi);
call.enqueue(new Callback<List<Trail>>() {
@Override
public void onResponse(Call<List<Trail>> call, Response<List<Trail>> response) {
int statusCode = response.code();
List<Trail> trails = response.body();
listView.setAdapter(new WalkDetailsAdapter(trails,R.layout.list_item_details, )); **<-HERE**
}
@Override
public void onFailure(Call<List<Trail>> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
这给了我errorError:(127,60)错误:不兼容的类型:List无法转换为Context。
我认为错误是listView.setAdapter的第三个参数,有人知道我必须添加的第三个参数是什么, (我尝试了getApplicatinonContext,就像我在填充recyclerview时所使用的那样。)listview的等价物是什么?
谢谢。
Walkdetailsadapter
public class WalkDetailsAdapter extends ArrayAdapter<Trail> {
private Context context;
private List<Trail> values;
public WalkDetailsAdapter ( Context context, List<Trail> values){
super(context, R.layout.list_item_details, values);
this.context = context;
this.values = values;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_details, parent, false);
}
TextView textViewName = (TextView) row.findViewById(R.id.name);
TextView textViewCounty = (TextView) row.findViewById(R.id.county);
TextView textViewFormat = (TextView) row.findViewById(R.id.format);
TextView textViewTime = (TextView) row.findViewById(R.id.time);
TextView textViewLength = (TextView) row.findViewById(R.id.length);
//TextView textViewGrade = (TextView) row.findViewById(R.id.grade);
TextView textViewDifficultly = (TextView) row.findViewById(R.id.diffuctlty);
TextView textViewQuality= (TextView) row.findViewById(R.id.quality);
TextView textViewStart = (TextView) row.findViewById(R.id.start);
TextView textViewFinish = (TextView) row.findViewById(R.id.finish);
Trail item = values.get(position);
String trail = item.getTrailName();
textViewName.setText(trail);
String county = item.getCounty();
textViewCounty.setText(county);
String format = item.getFormat();
textViewFormat.setText(format);
String time = item.getTime();
textViewTime.setText(time);
String length = item.getDistance();
textViewLength.setText(length);
String diff = item.getGrade();
textViewDifficultly.setText(diff);
String quality = item.getQuality();
textViewQuality.setText(quality);
String start = item.getStartTown();
textViewStart.setText(start);
String finish = item.getFinishTown();
textViewFinish.setText(finish);
return row;
}
}
答案 0 :(得分:1)
只需切换
listView.setAdapter(new WalkDetailsAdapter(trails,R.layout.list_item_details, ));
到这个
listView.setAdapter(new WalkDetailsAdapter(getApplicationContext(),trails));
问题在于,在WalkDetailsAdapter类中,构造函数参数是Context
和List<Trail>
,并且您为它提供了列表,然后是布局资源。因此,只需切换参数,以便第一个是上下文,使用getApplicationContext()
或getContext()
或this
取决于类的内容,第二个是List<Trail>
的实例然后它应该工作。