Tuesday 6 October 2015

Understand ListActivity in Android

What is ListActivity?


This is an Activity which has the default screen layout in which there is a ListView element occupying all the display space. The “id” of this ListView is “list”. A simple activity can works as a ListActivity also, but Android API provide a simple mechanism for activity that has only an array of item to display and handle.


Differences from a simple Activity


  • It only display a list of items.
  • Has implicit layout, no need to call setContentView method with any resource.
  • Implicit layout has only single element ListView with id=list


You can get the id of this default ListView inside the onCreate method by using following line of code for an example.


public class MainActivity extends ListActivity {

   private ListView list;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // No need to set own view, but by default it is there
       //setContentView(R.layout.activity_main);
       
       // get id of default ListView associated with ListActivity
       list=getListView();
     
   }


How to set own layout inside ListActivity?


  1. To set own layout, create a layout and MUST put a ListView element with id as follows: android:id="@android:id/list"
  2. Now set your own layout using setContentView method inside onCreate method.


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"    
   tools:context="test.example.mylistactivity.MainActivity" >

   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/no_data"
       android:text="@string/hello_world" />

    <!--
    Remember not put like this for ListView id
    android:id="@+id/listView1"
    -->
   
   <ListView      
       android:id="@android:id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       >
   </ListView>

</FrameLayout>


Create an Adaptor and bind the data to the view.
You can create a layout for a single row element inside the ListView and pass it to the adaptor. Adaptor will use it inside the getView method to inflate it. The adaptor can be inner class of your ListActivity main class or a separate top level class.


row_single_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"    
   tools:context="test.example.mylistactivity.MainActivity" >

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/name"
       android:text="@string/hello_world" />

</LinearLayout>


Note: you can put as many views as you want inside the above layout.


Now create the adaptor for ListView.


class MyListAdaptor extends BaseAdapter{

    LayoutInflater inflater;
   
    public MyListAdaptor(Context context){
    inflater=LayoutInflater.from(context);
    }
   
@Override
public int getCount() {
return players.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView!=null){
v=convertView;
}else{
v=inflater.inflate(R.layout.row_single_view, parent, false);
}
   TextView nameTextView = (TextView)v.findViewById(R.id.name
// bind (display) your data here…..

return v;
}
   
   }


Set the adapter to ListView

If you want to add header view to this ListView, you should do this before setting the adapter. You can set the footer view after setting the adapter.


public class MainActivity extends ListActivity {

private ListView list;
private ListAdapter adapter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       list=getListView();
       adapter = new MyListAdaptor(this);
               
       // Add header to ListView fist
       TextView header = new TextView(this);
       header.setTypeface(Typeface.SANS_SERIF);
       header.setTextColor(Color.BLUE);
       header.setText("This is my Header");
       list.addHeaderView(header);             
       
       // this must be after setting header view
       setListAdapter(adapter);
       
    // Add Footer to ListView
       TextView footer = new TextView(this);
       footer.setTypeface(Typeface.SANS_SERIF);
       footer.setTextColor(Color.RED);
       footer.setText("This is footer");
       list.addFooterView(footer);
   }

When to use onContentChanged method ?


Whenever the data list provided to the adapter is changed call this method method after adding the new item to the list. This action can be achieved inside the button click operation to verify this method.

public class MainActivity extends ListActivity {

private ListView list;
private ListAdapter adapter;
ArrayList<String> playerList;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
      
       list=getListView();        
       playerList = new ArrayList<String>();
       playerList.add("Rohit Sharma");
       playerList.add("Sikhar Dhawan");
       playerList.add("Virat Kohali");
       playerList.add("Suresh Raina");
       playerList.add("A Rahane");
      
       adapter = new MyListAdaptor(this);                       
       setListAdapter(adapter);        
       }
   
      @Override
   public void myButtonClicked(View v) {
    playerList.add("R Ashwin");    
    onContentChanged();
   }

Handling of Item clicked inside ListView


You have to override the protected method onListItemClick of ListActivity class to handle the click of single element for the ListView. This method you have to override inside the ListActivity class.

public class MainActivity extends ListActivity {

......

.....

@Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
   
    super.onListItemClick(l, v, position, id);
    TextView textView = (TextView) v.findViewById(R.id.name);
    String text = (String) textView.getText();
    Toast.makeText(this, "I am "+text, Toast.LENGTH_SHORT).show();    
   
   }
}

Data you are binding can be from an ArrayList or from content provider. In the next post, I will provide a simple demo application of ListActivity.

1 comment:

  1. Okay...

    What I'm going to tell you may sound a little weird, and maybe even kind of "out there"....

    WHAT if you could just click "Play" and listen to a short, "magical tone"...

    And miraculously bring MORE MONEY to your LIFE?

    What I'm talking about is hundreds... even thousands of dollars!

    Do you think it's too EASY? Think it couldn't possibly be REAL???

    Well, I'll be the one to tell you the news...

    Usually the largest blessings life has to offer are also the SIMPLEST!

    In fact, I'm going to PROVE it to you by letting you listen to a REAL "magical money-magnet tone" I developed...

    You just press "Play" and you will start having more money come into your life... it starts right away...

    CLICK here to play this mysterious "Miracle Wealth Building Sound Frequency" - as my gift to you!

    ReplyDelete