issue in getting data in listview using get API calling android

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



issue in getting data in listview using get API calling android



Issue in Getting API data in listview,



api is calling but list not shown in listview, when i dedug program it's run till call.enque method after this line it break on throw on last line


call.enque



i want this response in my Listview but i face issue i don't know where i mistake



This is my Get API response



"success": true,
"message": "",
"user": [

"id": 1,
"username": "Admin",
"email": "admin@gmail.com",
"role": "Administrator"
,

"id": 2,
"username": "Lawyer",
"email": "lawyer@gmail.com",
"role": "Lawyer"
,

"id": 3,
"username": "Driver",
"email": "driver@gmail.com",
"role": "Driver"

]



activity_main


<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ls"/>



Interface class api.java


public interface api
String BASE_URL = "https://efce0212.ngrok.io/api/";
@GET("user")
Call<List<User>> getHeroes();



MainActivity.java


public class MainActivity extends AppCompatActivity

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.ls);
getHeroes();


public void getHeroes()
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

api api = retrofit.create(api.class);


Call<List<User>> call = api.getHeroes();


call.enqueue(new Callback<List<User>>()
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response)
final List<User> userList = response.body();

final String user = new String[userList.size()];

for (int i = 0; i < userList.size(); i++)


user[i] = "Sucess:" + userList.get(i).getSucess() + "n" + "message" + userList.get(i).getMessage() + "n" + userList.get(i).getUserData();


listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, user));


@Override
public void onFailure(Call<List<User>> call, Throwable t)

Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();


);




Pojo USER.JAVA


public class User
private String sucess;
private String message;
public UserData userData;

//UserData userData = new UserData();

public User(String sucess, String message, UserData userData)
this.sucess = sucess;
this.message = message;
//this.userData = userData;
this.userData = userData;


public String getSucess()
return sucess;


public String getMessage()
return message;


public UserData getUserData()
return userData;




UserData.class


public class UserData
public String id;
public String name;
public String email;
public String role;





Please add the error/Issue which u have faced
– Ravindra Kushwaha
14 mins ago






@RavindraKushwaha i didn't get any error, issue is API data is not shown in listview
– aasifghanchi
10 mins ago






Do u need the customer adapter or spinner
– Ravindra Kushwaha
7 mins ago





no i don't need spinner or custom adapter, just want data in list
– aasifghanchi
3 mins ago




3 Answers
3



You need to add a LinearLayout manager to your List View.


LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
listView.setLayoutManager(layoutManager);



Try this way, use the ArrayList and get the getUserData() array from the model class, like below


ArrayList


getUserData()


ArrayList<String> arrayList = new ArrayList<>();

final List<UserData> userList = response.body().getUserData();

for (int i = 0; i < userList.size(); i++)

arrayList.add(userList.get(i).name)



listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList));



From what I see



"success": true,
"message": "",
"user": [

"id": 1,
"username": "Admin",
"email": "admin@gmail.com",
"role": "Administrator"
,

"id": 2,
"username": "Lawyer",
"email": "lawyer@gmail.com",
"role": "Lawyer"
,

"id": 3,
"username": "Driver",
"email": "driver@gmail.com",
"role": "Driver"

]



your response is a User object but as I see


User



call.enqueue(new Callback<List<User>>()


call.enqueue(new Callback<List<User>>()



you expect a list of Users as response which doesn't make sense.


User



I hope changing call response type to User can fix it. As this:


call


User


call.enqueue(new Callback<User>()
@Override
public void onResponse(Call<User> call, Response<User> response)
...


@Override
public void onFailure(Call<User> call, Throwable t)

...


);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard