ListActivity - Functionality

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

ListActivity - Functionality

Postby plusminus » Thu Nov 15, 2007 9:55 pm

Things you can do with a ListActivity


Adding items to a ListActivity:
Create a standard-Project, and replace the _YourApp_.java with the following (adapt class name/package name):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.listviews;
  2.  
  3. import android.app.ListActivity;
  4. import android.os.Bundle;
  5. import android.widget.ArrayAdapter;
  6.  
  7. public class ListViewDemo extends ListActivity {
  8.     /** Called when the activity is first created. */
  9.     @Override
  10.     public void onCreate(Bundle icicle) {
  11.         super.onCreate(icicle);
  12.         // Create an array of Strings, that will be put to our ListActivity
  13.         String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
  14.  
  15.         // Create an ArrayAdapter, that will actually make the Strings above appear in the ListView
  16.         this.setListAdapter(new ArrayAdapter<String>(this,
  17.                                         android.R.layout.simple_list_item_1, mStrings));
  18.     }
  19. }
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Reacting on item-clicks in a ListActivity:
See this solution: :idea: (click)
Last edited by plusminus on Sun Nov 18, 2007 2:27 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby mrspeaker » Sat Nov 17, 2007 12:24 pm

Is it possible to have more list item comprised of multiple components? For example - what if I wanted each list item line to have a checkbox, a string, and a button? Or an image and a label?

Can this be done with the ListActivity?

Thanks!
mrspeaker
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Nov 17, 2007 12:18 pm

Postby plusminus » Sat Nov 17, 2007 1:29 pm

Hey mrspeaker,

what I can offer you by now is the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle icicle) {
  2.  
  3.         super.onCreate(icicle);
  4.  
  5.         CheckBox c = new CheckBox(this);
  6.  
  7.         this.addContentView(c, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  8.  
  9. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

When I put this to an loop, I did not yet find, how to specify the position on the screen the checkboxes are put to...
The all appear on the same screen location...
As you will see this is definitely no perfect solution. Also haven't found info about that in the Google-Groups :?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby atr » Wed Nov 21, 2007 12:56 pm

Hi,

could you guys explain a little bit about this simple_list_item_1 found in the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.setListAdapter(new ArrayAdapter<String>(this,
  2.                          android.R.layout.simple_list_item_1, mStrings));
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Thanks!
atr
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 19, 2007 11:36 am
Location: Singapore

Postby plusminus » Wed Nov 21, 2007 7:30 pm

Hi atr,

lets take a look at the JavaDoc of the ArrayAdapter-Constructor we are using:
Javadoc wrote: public ArrayAdapter(Context context, int resource, T[] objects)
Constructor
Parameters:
context The current context.
resource The resource ID for a layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.


So we use the constant resourceID of a pre-defined layout ( in our case: 'android.R.layout.simple_list_item_1' ) from the list of all those other constants (you can find here: 'android.R.layout.xyz' )

For example you could simply replace:
android.R.layout.simple_list_item_1 :arrow: android.R.layout.simple_list_item_1_small

and tadaaaaa your list is now filled with items that have smaller text (as we now referr to another Layout which the Google-Guys pre-made a bit smaller).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby atr » Thu Nov 22, 2007 5:32 am

Really nice, +/-
Thanks for the explanation!
atr
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 19, 2007 11:36 am
Location: Singapore

Top

Listing out the items via Strings.xml

Postby Nitinkcv » Thu Nov 29, 2007 1:49 pm

[font=Arial] Hi,

Was just wondering whether it is possible for us to display the list items from the strings.xml.

Say my strings.xml is something like:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Simple List Screen</string>
    <string name="first_item">Google</string>
    <string name="second_item">eBay</string>
    <string name="third_item">Yahoo</string>
</resources>


So is it possible to get the contents of the first_item, second_item and third_item as the o/p in the list view,
using the setListAdapter method.

Thanks,
Nitin

[Edit by plusminus]
Appended you here :)
[s]P.S. Sorry i was refering to the post in the thread
[/font]ListActivity.[/s]
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby bloodcarter » Fri Jan 18, 2008 4:12 pm

That's cool, but how to display some data from ArrayAdapter into my own layout, not predefined one?

My layout looks like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="horizontal"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="wrap_content"
  10.  
  11.    >
  12.  
  13. <TextView  id="@+id/row_number"
  14.  
  15.    android:layout_width="wrap_content"
  16.  
  17.    android:layout_height="wrap_content"
  18.  
  19.    android:text="Name: "
  20.  
  21.    />
  22.  
  23.        
  24.  
  25.  </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



How to fill it simple with a list of numbers: 1,2,3,4.... ?
Thanks.
User avatar
bloodcarter
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jan 18, 2008 4:08 pm
Location: Novosibirsk, Russia

Postby Anil » Fri Jan 25, 2008 9:55 pm

Look at NoteEdit.java in Notepadv2 tutorial
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      titleText = (EditText) findViewById(R.id.title);
  2.   ...
  3.  titleText.setText(title);
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


bloodcarter wrote:That's cool, but how to display some data from ArrayAdapter into my own layout, not predefined one?

My layout looks like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="horizontal"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="wrap_content"
  6.    >
  7. <TextView  id="@+id/row_number"
  8.    android:layout_width="wrap_content"
  9.    android:layout_height="wrap_content"
  10.    android:text="Name: "
  11.    />
  12.        
  13.  </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



How to fill it simple with a list of numbers: 1,2,3,4.... ?
Thanks.
Anil
Developer
Developer
 
Posts: 44
Joined: Fri Jan 04, 2008 9:15 pm

Postby Anil » Fri Jan 25, 2008 10:02 pm

mrspeaker wrote:Is it possible to have more list item comprised of multiple components? For example - what if I wanted each list item line to have a checkbox, a string, and a button? Or an image and a label?

Can this be done with the ListActivity?

Thanks!


Yes it can.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         View myView = inflateView(R.layout.myLayout);
  2.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

get a hold of each view using the resource id, and set the data.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView t = (TextView) myView.findViewById(R.id.title);
  2.  
  3. t.setText("HALLOOO!!!");
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Now associate the ArrayAdapter with the data, and associate the ArrayAdapter with the ListAdapter.

ListAdapter <--------> ArrayAdapter <-------> data

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayAdapter<String> notes =
  2.                 new ArrayAdapter<String>(mContext, R.layout.notes_row, items);
  3.             myView.setAdapter(notes);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Anil
Developer
Developer
 
Posts: 44
Joined: Fri Jan 04, 2008 9:15 pm

Postby raquibulbari » Sat Jan 26, 2008 2:06 pm

check this link, here in a list, image and textview are added, based on it i also made a list which has additional buttons/components . This link might be useful for you

[Edit by plusminus]
Very similar code with comments added, also available here on anddev: :arrow: viewtopic.php?t=97
[/Edit]
http://steven.bitsetters.com/articles/2 ... fied-list/

SHIMUL
Shimugool
User avatar
raquibulbari
Developer
Developer
 
Posts: 25
Joined: Sun Dec 16, 2007 6:31 am
Location: dhaka,bangladesh

RE: Listing out the items via Strings.xml

Postby greval » Tue Feb 05, 2008 2:09 pm

Hey Nitinkcv,

All the strings you have defined with <string name="first_item">Google</string> in your file strings.xml are in the R.class under
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static final class string {
  2.   public static final int first_item=....;
  3.  ....
  4. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So, you can get the contents of the strings:

1) in XML layout file using:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.       android:text="@string/first_item"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


2) in Java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     // method getString from android.content.Resources
  2.     getString(R.string.first_item)
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


The R.class is saving all the resources as integers, so you have to get the String value
associated withe the particular resource ID.
greval
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 05, 2008 12:46 pm

Postby exilid1 » Thu Feb 14, 2008 8:36 am

Hi Plusminus

When using a SimpleCursorAdapter, i am getting the error as in attached pic, what could be the reason?

Code snippet is like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. lisCursor = managedQuery(getIntent().getData(), OPTPROJECTION, null, null);
  2.  
  3.                 if(lisCursor!=null)
  4.  
  5.                 {       cAdapter = new SimpleCursorAdapter(this,
  6.  
  7.                     android.R.layout.simple_list_item_1, lisCursor,
  8.  
  9.                     new String[] {NotePad.Options.OPTION_VAL}, null);
  10.  
  11.                 setListAdapter(cAdapter);
  12.  
  13.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Logcat shows:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. E/AndroidRuntime(1236): Caused by: java.lang.IllegalStateException: Unable to get field slot
  2.  
  3. E/AndroidRuntime(1236):     at android.database.CursorWindow.getLong(Native Method)
  4.  
  5. E/AndroidRuntime(1236):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:82)
  6.  
  7. E/AndroidRuntime(1236):     at android.widget.CursorAdapter.getItemId(CursorAdapter.java:110)
  8.  
  9. E/AndroidRuntime(1236):     at android.widget.AdapterView.obtainItemID(AdapterView.java:250)
  10.  
  11. E/AndroidRuntime(1236):     at android.widget.AdapterView.setSelectedPositionInt(AdapterView.java:510)
  12.  
  13. E/AndroidRuntime(1236):     at android.widget.ListView.setAdapter(ListView.java:305)
  14.  
  15. E/AndroidRuntime(1236):     at android.app.ListActivity.setListAdapter(ListActivity.java:260)
  16.  
  17. E/AndroidRuntime(1236):     at com.google.Newtest.List7.onCreate(List7.java:76)
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


and code at List7.java line 76 is the setListAdapter above. Option val is a text field picked up from the db.

Thx
Bala
Attachments
Picture 11.png
Picture 11.png (18.21 KiB) Viewed 84885 times
exilid1
Freshman
Freshman
 
Posts: 2
Joined: Tue Dec 18, 2007 11:54 am
Location: Bangalore

Is it possible to have a list inside list??

Postby rahul_mawkins » Wed Feb 27, 2008 4:29 am

Hi Guys
First this is fantastic site for android developers. Good job done
Secondly I have a question
Is it possible to have a list inside list?
What I mean is my first list shows a category. What I am looking for is that when user clicks on some item in that category the box should expand and shows all the subcategory inside the inner list
I have gone through the example of having iconified list but again thats good for showing buttons,textviews,Imageviews
I tried adding ListViews....no error but list doesnt show up
Here is my source code

Any advice given is highly appreciated



import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.ArrayAdapter;
import android.view.View.MeasureSpec;
import android.widget.TableRow;


/**
* A list view example where the
* data comes from a custom
* ListAdapter
*/
public class ViewCurrentShoppingList extends ListActivity
{

@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
// Use our own list adapter
TextView label=(TextView)findViewById(R.id.label);
setListAdapter(new SpeechListAdapter(this));
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
((SpeechListAdapter)getListAdapter()).toggle(position);
}

/**
* A sample ListAdapter that presents content
* from arrays of speeches and text.
*
*/
private class SpeechListAdapter extends BaseAdapter {
public SpeechListAdapter(Context context)
{
mContext = context;
}


/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return mTitles.length;
}

/**
* Since the data comes from an array, just returning
* the index is sufficent to get at the data. If we
* were using a more complex data structure, we
* would return whatever object represents one
* row in the list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}

/**
* Use the array index as a unique id.
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}

public String getDialogueText(int position)
{
StringBuffer sb=new StringBuffer();
for(int k=0;k<5;k++)
{
for(int i=0;i< mDialogue[position].length;i++)
{
sb.append(mDialogue[position][i]);
sb.append("+");
}
}
return sb.toString();
}

/**
* Make a SpeechView to hold each row.
* @see android.widget.ListAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
SpeechView sv;
if (convertView == null) {
sv = new SpeechView(mContext, mTitles[position], mDialogue[position], mExpanded[position]);
} else {
sv = (SpeechView)convertView;
sv.setTitle(mTitles[position]);
sv.setDialogue(mDialogue[position]);
sv.setExpanded(mExpanded[position]);
}

return sv;
}

public void toggle(int position) {
mExpanded[position] = !mExpanded[position];
notifyDataSetChanged();
}

/**
* Remember our context so we can use it when constructing views.
*/
private Context mContext;

/**
* Our data, part 1.
*/
private String[] mTitles =
{
"Grocery",
"Clothes",
"Fastfood",
"Current Location"
};


private String [][] mDialogue={
{"rahul","mawkin","amit","mawkin"},
{"rahul","mawkin","amit","mawkin"},
{"rahul","mawkin","amit","mawkin"},
{"rahul","mawkin","amit","mawkin"}
};

/**
* Our data, part 3.
*/
private boolean[] mExpanded =
{
false,
false,
false,
false
};
}

/**
* We will use a SpeechView to display each speech. It's just a LinearLayout
* with two text fields.
*
*/
private class SpeechView extends TableLayout {
public SpeechView(Context context, String title, String []dialogue, boolean expanded) {
super(context);

this.setOrientation(VERTICAL);

this.setVerticalScrollBarEnabled(true);

// Here we build the child views in code. They could also have
// been specified in an XML file.

TableRow tr=new TableRow(context);
mTitle = new TextView(context);
mTitle.setText(title);
tr.addView(mTitle,new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(tr,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


TableRow tr1=new TableRow(context);
bt=new Button(context);
bt.setText("hello");
bt.setVisibility(expanded ? VISIBLE : GONE);
tr1.addView(bt,new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));//,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(tr1,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

TableRow tr2=new TableRow(context);
mDialogue=new MyListView(context);
mDialogue.bringToFront();
ila=new InnerListAdapter(context,dialogue);
mDialogue.setAdapter(ila);
mDialogue.setVisibility(expanded ? VISIBLE : GONE);
tr2.addView(mDialogue.getRootView(),new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));//,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(tr2,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));




}
private InnerListAdapter ila;
/**
* Convenience method to set the title of a SpeechView
*/
public void setTitle(String title) {
mTitle.setText(title);
}

/**
* Convenience method to set the dialogue of a SpeechView
*/
public void setDialogue(String []words) {
ila.setMStrings(words);
}

/**
* Convenience method to expand or hide the dialogue
*/
public void setExpanded(boolean expanded) {
mDialogue.setVisibility(expanded ? VISIBLE : GONE);
bt.setVisibility(expanded ? VISIBLE : GONE);
}

private TextView mTitle;
private MyListView mDialogue;
private Button bt;
private Button bt1;

}

private class MyListView extends ListView
{
public MyListView(Context ct)
{
super(ct);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
}

}

public class InnerListAdapter extends BaseAdapter
{
private Context ctx;
private String[] mStrings={"A","B","C","D"};
public InnerListAdapter(Context ct,String [] mStrings)
{
ctx=ct;
//mStrings=mStrings;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mStrings.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(ctx);
} else {
tv = (TextView) convertView;
}
tv.setText(mStrings[position]);
return tv;

}

public void setMStrings(String[] strings) {
mStrings = strings;
this.notifyDataSetChanged();
}



}


}

The point is MyListView mDialogue(I know the attribute is wrongly name) doesnt shows up
The textview and button shows up only
The outer list shows up and fills the data with titles

Please help
rahul_mawkins
Developer
Developer
 
Posts: 34
Joined: Wed Feb 27, 2008 4:09 am

Postby manojo » Sat Apr 12, 2008 4:01 pm

Hi,

I would like to know how I could proceed to drag and drop elements in a ListActivity. I have followed the filebrowser tutorial here, and I have therefore made a ListAdapter which contains my own ListView.

What do I need to do ?

Thanks,
Manojo
manojo
Freshman
Freshman
 
Posts: 4
Joined: Fri Mar 21, 2008 12:33 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests