Sample code for ListActivity

Sample code for ListActivity
package  www.puyufanyi.com;

import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.List;
import  java.util.Map;

import  www.puyufanyi.com.test.Word;

import  android.app.ListActivity;
import  android.os.Bundle;
import  android.view.View;
import  android.widget.AdapterView;
import  android.widget.AdapterView.OnItemClickListener;
import  android.widget.ListView;
import  android.widget.SimpleAdapter;
import  android.widget.Toast;
import  android.widget.TextView;

public   class  UIActivity  extends  ListActivity {

    
private  ArrayList < Word >  words  =   new  ArrayList < Word > ();

    @Override
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Word word1 
=   new  Word( " index1 " " result1 " );
        Word word2 
=   new  Word( " index2 " " result2 " );
        Word word3 
=   new  Word( " index3 " " result3 " );
        Word word4 
=   new  Word( " index4 " " result4 " );
        Word word5 
=   new  Word( " index5 " " result5 " );
        
this .words.add(word1);
        
this .words.add(word2);
        
this .words.add(word3);
        
this .words.add(word4);
        
this .words.add(word5);

        List
< Map < String, Object >>  listItems  =   new  ArrayList < Map < String, Object >> ();

        
for  ( int  i  =   0 ; i  <  words.size(); i ++ ) {
            Map
< String, Object >  listItem  =   new  HashMap < String, Object > ();
            listItem.put(
" Index " , words.get(i).getIndex());
            listItem.put(
" Result " , words.get(i).getResult());
            listItems.add(listItem);
        }
        SimpleAdapter simpleAdapter 
=   new  SimpleAdapter(
                
this
                listItems, 
                R.layout.main,
                
new  String[]{ " Index " , " Result " },
                
new   int []{R.id.index,R.id.result}
        );
                setListAdapter(simpleAdapter);
                ListView lv 
=  getListView(); 
                lv.setTextFilterEnabled(
true );
                lv.setOnItemClickListener(
new  OnItemClickListener() {
                    
public   void  onItemClick(AdapterView <?>  parent, View view, int  position,  long  id) {
                         String  indexValue 
=  ((TextView)view.findViewById(R.id.index)).getText().toString();
                         Toast.makeText(getApplicationContext(), indexValue, Toast.LENGTH_SHORT).show();    
                    }  
                });
                
     }
}

你可能感兴趣的:(Sample code for ListActivity)