Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a super simple List / ListAdapter example out there for android

Tags:

I have a web service that returns a super simple list of objects

MyObject[] data = webServiceCall();

MyObject has 1 field i want to display, "Name" (i.e. data[0].Name )

How can i turn this into an activity that lists just the name of these objects in a scrollable listActivity on Android. I am getting really confused with Cursors and am not sure if I need Cursors and I"m not sure what kind of adapter to implement (BaseAdapter, SimpleAdapter etc)

So i guess i'm looking for three things,

the activity, the adapter and the layout.xml

Just trying to figure this android stuff out, definitely a noob here

like image 861
slim Avatar asked Apr 22 '10 04:04

slim


People also ask

How do I create a custom list view?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above activity_main.

What is a ListAdapter in android?

ListAdapter is a wrapper around the AyncListDiffer helper. It includes computing diffs between Lists on a background thread and handling updates at different adapter positions. All you need to do is make a call to the submitList(data: List<T>) function and it will compute the best way to update the items for you.

How do you show data in a list view?

ListView listView = (ListView) findViewById(R. id. listview); listView. setAdapter(adapter);


2 Answers

so i think i figured it out with a little inspiration from RobGThai's answer, i'm posting the code for anyone else to see, i guess i didn't really use a custom adapter

This is the super simple example that got me started, so once i had this, I made sure my "MyObject" had a toString() method on it to show properly in the list and i passed the MyObject[] array into the "new ArrayAdapter" constructor instead of listItems

FooList.java

import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter;  public class FooList extends ListActivity {     String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", };      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.temp);          setListAdapter(new ArrayAdapter(this,  android.R.layout.simple_list_item_1, listItems));      }  } 

the layout xml i used (temp.xml)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">     <ListView android:id="@android:id/list" android:layout_width="fill_parent"         android:layout_height="wrap_content" />     <TextView android:id="@android:id/empty" android:layout_width="wrap_content"         android:layout_height="wrap_content" android:text="Empty set" /> </LinearLayout> 
like image 191
slim Avatar answered Oct 19 '22 04:10

slim


make your Activity extends ListActivity as well.

Then this should help you get started.

Object[] sArray = {"This", "is", 3.5, true, 2, "for", "bla"}; ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray); setListAdapter(adp); 

The second parameters can be change to your preferred layout. See API document for further information.

like image 22
RobGThai Avatar answered Oct 19 '22 05:10

RobGThai