How to show json data in Listview with Retrofit2 - java

I'm a newbie about Android programming :(
I have no idea to set Listview for show data
How to Show data in Listview.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "fong";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(UdacityService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UdacityService service = retrofit.create(UdacityService.class);
Call<UdacityCatalog> requestCatalog = service.listCatalog();
requestCatalog.enqueue(new Callback<UdacityCatalog>() {
#Override
public void onResponse(Call<UdacityCatalog> call, Response<UdacityCatalog> response) {
if (!response.isSuccessful()) {
Log.i(TAG,"Errorr: " +response.code());
}else{
UdacityCatalog catalog =response.body();
for (Course c : catalog.courses){
Log.i(TAG,c.title);
Log.i(TAG,"--------------");
}
}
}
#Override
public void onFailure(Call<UdacityCatalog> call, Throwable t) {
Log.e(TAG,"Errorr: " + t.getMessage());
}
});
}
}
I want to show data c.title in ListView
Log.i(TAG,c.title) show
I/fong: Richard Kalehoff
I/fong: Firebase Analytics: iOS
I/fong: Firebase Analytics: Android
Thanks for yor help :)

It`s easy, First you have to add a ListView component in your Layout:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And then, find it in your JAVA code and put it in a instance variable:
ListView listview;
//in your onCreate() after setContentView():
listview = (ListView) findViewById(R.id.list);
You have to create a list with all your titles:
List<String> courses = new ArrayList<>();
for (Course c : catalog.courses){
courses.add(c.title);
}
Now, you have to create a SimpleAdapter and pass your data to it:
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.r.layout.simple_list_item_1, courses);
And finally, add your adapter in your ListView:
listview.setAdapter(adapter);

Take a list of Course
List<Course> courseList = catalog.courses;
then make a custom list Adapter and from this list get the title and show it in textView of the list
textView.setText(courseList.get(position).title);

Related

How to set a DIFF background color of EACH item clicked on my List- can only get it to send 1 color for each item selected using setBackgroundColor()

public class SecondActivity extends AppCompatActivity {
private String [] myList = new String[]{"Blue", "Green", "Red"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ListView listView = (ListView)findViewById(R.id.ListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter);
}
}
Don't use an ArrayAdapter. Use a custom adapter and set the background of each item.
Better yet- don't use ListView. It's been deprecated for most of a decade. Use a RecyclerView, and set the background color in onBindViewHolder.

Java does not recognize ListView variable

I trying to create a ListView in AndroidStudio with value "nome". I created a list and a adapter for this, but my application not recognize variable "services".
This is my activity code
public class MainActivity extends AppCompatActivity {
private String service;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item1);
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, services); <-- in this line
listView.setAdapter(servicesAdapter);
}
private List<Services> gerarServices(){
List<Services> services = new ArrayList<Services>();
services.add(criarServices("Home"));
services.add(criarServices("Delivery"));
return services;
}
private Services criarServices(String nome){
Services service = new Services(nome);
return service;
}
}
Any suggestion? Thank's in advance!
The services variable is not in the scope of your onCreate method. As mentioned by #Code-Apprentice, you might want to find out more about scope of local variables.
So, what you might want to do is to invoke the gerarServices() method in the onCreate method and assign it to a local variable which will then be passed to the ArrayAdapter. Like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item1);
List<Services> services = gerarServices();
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, services);
listView.setAdapter(servicesAdapter);
}
or
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_item1);
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, gerarServices());
listView.setAdapter(servicesAdapter);
}
Hope it helps!
In your code, I dont see any mehod for declaring and initializing services.
But gerarServices() return a list of Services. So I guess you have to do above action(s) before using any variables or instances of class, etc.
Like so,
ListView listView = (ListView) findViewById(R.id.list_item1);
// Add this line of code
List<Services> services = gerarServices();
ArrayAdapter<Services> servicesAdapter = new ArrayAdapter<Services>(this, android.R.layout.simple_list_item_1, services);
Hope this help.

RecyclerView: No adapter attached; skipping layout - recycleView error [duplicate]

This question already has answers here:
recyclerview No adapter attached; skipping layout
(38 answers)
No adapter attached; skipping layout [duplicate]
(2 answers)
Closed 3 years ago.
Its giving me "RecyclerView: No adapter attached; skipping layout" error but in my opinion adapter is correctly attached. Please help.
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private RecyclerViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
requestJsonObject();
}
private void requestJsonObject(){
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://api.myjson.com/bins/2t4j3";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG, "Response " + response);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<ItemObject> posts = new ArrayList<ItemObject>();
posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
adapter = new RecyclerViewAdapter(MainActivity.this, posts);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
}
});
queue.add(stringRequest);
}
}
I was moving my methods, but its not working. I was looking here for answer but there was only problems with wrong implemented adapters. I dont't know whats wrong here.
Try to put this line:
adapter = new RecyclerViewAdapter(MainActivity.this, posts);
After adding a LayoutManager to the RecyclerView. Of course the list will be empty, but just init RecyclerAdapter and assign it to the RecyclerView
Then, when Volley completes it's request, use:
recyclerView.getAdapter().addAll(posts);
recyclerView.getAdapter().notifyDataSetChaged();
The last commands add elements to the RecyclerView's adapter and notify LayoutManager of the change.
You are calling setAdapter in a delay thread. When the view is created in the main thread, the Recyclerview do not have an adapter.
Put this two lines before setLayoutManager
List<ItemObject> posts = new ArrayList<ItemObject>();
adapter = new RecyclerViewAdapter(MainActivity.this, posts);
In onResponse, you can ethier set a new adpater or update the data in the adapter which depends on your implementation.

where isimple_list_item_1 is declare in adapter parameters android java

In android application I am using adapter for populating ListView control like below:
public class ChildActivity extends ActionBarActivity implements GetChildList {
private ListView lv_child;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
List<String> array = new ArrayList<String>(); // for e.g array ["num1", "num2"]
lv_child = (ListView)findViewById(R.id.lv_child);
ArrayAdapter<String> adapterChild = new ArrayAdapter<String>(ChildActivity.this, android.R.layout.simple_list_item_1,array);
lv_child.setAdapter(adapterChild);
}
}
but i don't understand where its is simple_list_item_1 declare at this line ArrayAdapter<String> adapterChild = new ArrayAdapter<String>(ChildActivity.this, android.R.layout.simple_list_item_1,array);
Kindly suggest me, waiting for reply.

Listview with spinner in Android

Hey guys I am trying to create a menu using a listview with the code I have shown below. The code I have shown will show Item1, Item2 and Item3 in a list. Now I want to be able to add a spinner to item1 and item2. Is that possible? If so how would i go about doing it.
public class MyList extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] MyList = new String[] { "Item1","Item2","Item3"};
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, MyList));
}
Yes, It is possible. Try the following code. You will get it.
String[] MyList = new String[] { "Item1","Item2","Item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, MyList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I think you need to create custom list view for doing that you can start by following this particular tutorial :
Tutorial
This link may be helpful to you :
Link

Categories