我尝试使用服务器中的JSON填充ListView。我从服务器获取数据但我似乎无法弄清楚如何将它放在ListView中。我得到的错误在onResponse中。
它说:"错误:(84,54)错误:类JucatoriLiberiArrayAdapter中的构造函数JucatoriLiberiArrayAdapter不能应用于给定的类型; required:Context,ArrayList 发现:FragmentActivity,Call> 原因:实际参数调用>无法通过方法调用转换"
转换为ArrayList我想我几乎尝试了所有Alt + Enter修复程序。还尝试将类型从Call<ArrayList<FreePlayers>
更改为仅ArrayList<>
或仅Call<>
。
这是我的班级:
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
final Retrofit builder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.baseUrl(ROOT)
.build();
REST_CLIENT = builder.create(API.class);
final Call<FreePlayers> request = REST_CLIENT.getFreePlayers(1);
request.enqueue(new Callback<FreePlayers>() {
@Override
public void onResponse(final Call<FreePlayers> call, final Response<FreePlayers> response) {
ArrayList<FreePlayers> players = null;
players = call;
ListView lv = (ListView) getView().findViewById(android.R.id.list);
JucatoriLiberiArrayAdapter adapter = new JucatoriLiberiArrayAdapter(getActivity(),
players);
lv.setAdapter(adapter);
}
@Override
public void onFailure(final Call<FreePlayers> call, final Throwable t) {
}
});
}
我的API界面:
public interface API {
@FormUrlEncoded
@POST("/fan-sport/app-test/")
Call<ArrayList<FreePlayers>> getFreePlayers(@Field("getFreePlayers") int freePlayers);
}
这是我的自定义适配器:
public class JucatoriLiberiArrayAdapter extends BaseAdapter {
private Context context;
private ArrayList<FreePlayers> players;
public JucatoriLiberiArrayAdapter(Context context, ArrayList<FreePlayers> playersList) {
super();
this.context = context;
this.players = playersList;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(final int position) {
return null;
}
@Override
public long getItemId(final int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_view_jucatori_liberi, null);
TextView playerName = (TextView) convertView.findViewById(R.id.name);
playerName.setText((CharSequence) players.get(position));
}
return convertView;
}
}
答案 0 :(得分:0)
在onResponse()
您收到Call
对象时,您的适配器需要List<FreePlayers>
,响应参数是您要为适配器提供的播放器列表。
试试这段代码:
final Call<List<FreePlayers>> request = REST_CLIENT.getFreePlayers(1);
request.enqueue(new Callback<List<FreePlayers>>() {
@Override
public void onResponse(final Call<List<FreePlayers>> call, final Response<List<FreePlayers>> response) {
ArrayList<FreePlayers> players = null;
players = response.body();
ListView lv = (ListView) getView().findViewById(android.R.id.list);
JucatoriLiberiArrayAdapter adapter = new JucatoriLiberiArrayAdapter(getActivity(),
players);
lv.setAdapter(adapter);
}
@Override
public void onFailure(final Call<FreePlayers> call, final Throwable t) {
}
});
答案 1 :(得分:0)
呼叫被分配给玩家,但玩家类型为ArrayList<FreePlayers>
,呼叫类型为Call<List<FreePlayers>
。不一样。适配器需要ArrayList<FreePlayers>
。
答案 2 :(得分:0)
你宣布
Call<FreePlayers>,
但希望收到
Call<ArrayList<FreePlayers>>.
另外,你的适配器坏了。这些方法应该类似于:
@Override
public int getCount() {
if (players == null) return 0;
return players.size();
}
@Override
public Object getItem(final int position) {
if (players == null || players.size() == 0) return null;
return players.get(position);
}
@Override
public long getItemId(final int position) {
return getItem(position).getId(); // or something like an id that suits you.
}