android


Populate ListView using Retrofit 2


I'm trying to populate a ListView with a JSON from a server. I'm getting the data from the server but I can't seem to figure out how to put it in a ListView. The error I'm getting is in the onResponse.
It says: "Error:(84, 54) error: constructor JucatoriLiberiArrayAdapter in class JucatoriLiberiArrayAdapter cannot be applied to given types;
required: Context,ArrayList
found: FragmentActivity,Call>
reason: actual argument Call> cannot be converted to ArrayList by method invocation conversion"
I think I've tried almost every Alt+Enter fix there is. Also tried to change type from Call<ArrayList<FreePlayers> to just ArrayList<> or just Call<>.
This is my class:
#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) {
}
});
}
My API interface:
public interface API {
#FormUrlEncoded
#POST("/fan-sport/app-test/")
Call<ArrayList<FreePlayers>> getFreePlayers(#Field("getFreePlayers") int freePlayers);
}
And this is my custom adapter:
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;
}
}
In your onResponse() you receive a Call object wheras your adapter expects a List<FreePlayers>, the response argument is the list of players you want to give to your adapter.
Try this code:
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) {
}
});
call is assgined to players, but players type is ArrayList<FreePlayers> and call type is Call<List<FreePlayers>. Not the same. The adapter expects ArrayList<FreePlayers>.
You're declaring
Call<FreePlayers>,
but expect to receive
Call<ArrayList<FreePlayers>>.
Also, you're Adapter is broken. These methods should look something like:
#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.
}

Related Links

how do i view methods/step through code during orientation change in android
Efficient way to request touch focus from WindowManager?
Appcelerator titanium - MapView on android causes app crash
How can I recreate a dialog, including its whole content?
Opengl scrolling world guidance
Use this.setVolumeControlStream(AudioManager.STREAM_MUSIC); to control android phone rocker when activity is out of focus (with service)?
How to avoid getting both called: onItemClicked and onTextChanged on AutoCompleteTextView
How to check internet accessibility in hotspot (Android)
Allowing rectangle to be clickable - android
Phonegap Plugin Error during initialization
Android XML Rich Text Processing
Context menu on the top
Why use the word “invalidate” to request a view to be redrawn?
ListPreference default value not showing up
Landscape mode creating issues in app
android - how to prefill Edittext?

Categories

HOME
notepad++
embedded
phantom-dsl
phpword
ggplot2
alexa-skills-kit
object
mjml
sh
javacc
database-normalization
rds
vxml
zip
tvos
drop-down-menu
endeca
scrolltop
selectize.js
heat
screen-readers
video-editing
html5-appcache
digital-signature
nodemcu
exit
gmock
froala
licensing
outliers
firebase-analytics
datastax-enterprise-graph
bringtofront
complex-numbers
laravel-eloquent
hreflang
github3.py
nested-lists
imx6
fiware-cygnus
protege4
xcode7.3
git-rewrite-history
jpad
timing
amazon-ses
metaprogramming
c9.io
portforwarding
httr
dbcontext
tinymce-3
google-cse
page-replacement
java-6
avx
quicktime
gameplay-kit
stripe-connect
myspace
uicolor
ng-grid
android-wake-lock
fuzzywuzzy
findfirst
pushbots
invoke-sqlcmd
qt5.2
batik
lasso
viola-jones
sql-server-data-tools
linked-tables
phpldapadmin
addressing-mode
crowd
entry-point
mdichild
live555
artemis
model-driven-development
unit-of-work
route-provider
wp7test
onmousemove
saleslogix
subproject
infobright
stress
flex-mobile
gobject-introspection
catransform3d
webdev.webserver
jqote
w3c-geolocation
svk
web-garden
request-headers
skype4java
glui
wcf-instancing
alsb
todos

Resources

Encrypt Message