Tuesday 6 October 2015

ListActivity Demo Application

In the previous post, We have gone through some basic concepts of ListActivity. In this post we will develop a very simple application using ListActivity.


In this demo, I have created a ListActivity for a Scorecard of Indian Team. At the top of view, Scorecard is set as the header of the ListView and Total Score as the footer of the view. In between I have used an ArrayList of players and their score to display as each element of the ListView. Clicking on the item, A toast is displayed for that particular player.

If you pressed the back button a new player is added to the view before the footer view. I have used the physical back button to handle the addition of new item to the view, though it is advisable not to do so. You can handle this inside the button event. This button can be added inside your custom list activity layout.


In this example, default layout for the ListActivity is used. For each row, a custom layout, row_view.xml is used. A custom adapter is used to populate the player list inside the ListView. onListItemClick and onContentChanged methods are also used


MainActivity.java


package test.example.mylistactivity;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ListActivity {

private ListView list;
private ListAdapter adapter;
ArrayList<String> playerList;
ArrayList<String> runList;
TextView footer;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Not setting own view, but by default it is there
       //setContentView(R.layout.activity_main);
       
       // get id of default ListView associated with ListActivity
       list=getListView();
       // This is default adaptor
       //adapter=getListAdapter();
       
       playerList = new ArrayList<String>();
       playerList.add("Rohit Sharma");
       playerList.add("Sikhar Dhawan");
       playerList.add("Virat Kohali");
       playerList.add("Suresh Raina");
       playerList.add("A Rahane");
       playerList.add("Mahendra Singh Dhoni");
       playerList.add("Ravindra Jadeja");
       
       runList = new ArrayList<String>();
       runList.add("35");
       runList.add("47");
       runList.add("125");
       runList.add("54");
       runList.add("12");
       runList.add("11");
       runList.add("20");
       
       //set your own adaptor
       adapter = new MyListAdaptor(this);
       //setListAdapter(adapter);
       
       // Add header to ListView
       TextView header = new TextView(this);
       header.setTypeface(Typeface.SANS_SERIF);
       header.setTextColor(Color.BLUE);
       header.setBackgroundColor(Color.GRAY);
       header.setText("Scorecard");
       //list.addHeaderView(header);   
       list.addHeaderView(header,"Header Location", false);  
       
       // this must be after setting header view
       setListAdapter(adapter);
       
    // Add Footer to ListView
       footer = new TextView(this);
       footer.setTypeface(Typeface.SANS_SERIF);
       footer.setTextColor(Color.RED);
       footer.setBackgroundColor(Color.GRAY);
       footer.setText("Total               "+304);
       //list.addFooterView(footer);
       list.addFooterView(footer,"Footer Location",false);
       
       list.setSelectionAfterHeaderView();
   }
   
   // To handle click for a single row, override this protected method.
   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    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();
   
   }
   
   // It should not be done here....
   // Using back pressed button for simplicity only
   // not want to add any button in layout ...;)
   @Override
   public void onBackPressed() {
    playerList.add("R Ashwin");
    runList.add("2");
    footer.setText("Total               "+306);
    onContentChanged();
   }
   
   // This is custom adapter implementation for the list view
   class MyListAdaptor extends BaseAdapter{

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

@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_view, parent, false);
}
TextView nameTextView = (TextView)v.findViewById(R.id.name);
TextView runTextView = (TextView)v.findViewById(R.id.run);
nameTextView.setText(playerList.get(position));
runTextView.setText(runList.get(position));
return v;
}
   
   }

}


row_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="test.example.mylistactivity.MainActivity" >

   <TextView
       android:id="@+id/name"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
        />

   <TextView
       android:id="@+id/run"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        />

</LinearLayout>

No comments:

Post a Comment