ListView > if I don't wan use xml, but programatic

Tutorials with advanced 'difficulty' and more Lines of Code.

ListView > if I don't wan use xml, but programatic

Postby sit06603 » Fri Aug 14, 2009 11:19 am

hi all,

can any one show me a simple example how can I create ListView using code, not xml...

and add item into list and remove item in the list anyway I wan???



wesley.
sit06603
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu May 15, 2008 11:22 am

Top

Postby gecu_121 » Wed Aug 19, 2009 7:46 pm

hello,
use the following code to get your work done...


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.pack.listactivity;
  2.  
  3.  
  4.  
  5. import android.app.ListActivity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.util.Log;
  10.  
  11. import android.view.View;
  12.  
  13. import android.widget.ArrayAdapter;
  14.  
  15. import android.widget.ListView;
  16.  
  17.  
  18.  
  19. public class MyList extends ListActivity{
  20.  
  21.         private String[] itemNames = {"Item1", "Item2", "Item3", "Item4", "Item5"};
  22.  
  23.     /** Called when the activity is first created. */
  24.  
  25.     @Override
  26.  
  27.     public void onCreate(Bundle savedInstanceState) {
  28.  
  29.         super.onCreate(savedInstanceState);
  30.  
  31.         setListAdapter(new ArrayAdapter<String>(this,
  32.  
  33.                 android.R.layout.simple_list_item_1, itemNames));
  34.  
  35.         getListView().setTextFilterEnabled(true);
  36.  
  37.     }
  38.  
  39.         @Override
  40.  
  41.         protected void onListItemClick(ListView l, View v, int position, long id) {
  42.  
  43.                 // TODO Auto-generated method stub
  44.  
  45.                 super.onListItemClick(l, v, position, id);
  46.  
  47.                 Log.v("Done", "Item clicked is " + position);
  48.  
  49.         }
  50.  
  51.  
  52.  
  53. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


in onListItemClick, you can do what you wanted.
gecu_121
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Jul 30, 2009 2:39 pm

local address

Postby android2 » Thu Oct 22, 2009 8:07 pm

You can switch from a local address type c: users etc.
gecu_121 wrote:hello,
use the following code to get your work done...


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.pack.listactivity;
  2.  
  3. import android.app.ListActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9.  
  10. public class MyList extends ListActivity{
  11.         private String[] itemNames = {"Item1", "Item2", "Item3", "Item4", "Item5"};
  12.     /** Called when the activity is first created. */
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setListAdapter(new ArrayAdapter<String>(this,
  17.                 android.R.layout.simple_list_item_1, itemNames));
  18.         getListView().setTextFilterEnabled(true);
  19.     }
  20.         @Override
  21.         protected void onListItemClick(ListView l, View v, int position, long id) {
  22.                 // TODO Auto-generated method stub
  23.                 super.onListItemClick(l, v, position, id);
  24.                 Log.v("Done", "Item clicked is " + position);
  25.         }
  26.  
  27. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


in onListItemClick, you can do what you wanted.
android2
Developer
Developer
 
Posts: 48
Joined: Sun Oct 04, 2009 8:34 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests