Pages

Saturday, September 8, 2012

Android Simple List Example


Define Your main.xml



<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="30dp"
    />


package sample.listex;

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

public class ListExample extends ListActivity {
 
String[] languages={"c","c++","java","php","asp","c#",
"c","c++","java","php","asp","c#",
"c","c++","java","php","asp","c#",
"c","c++","java","php","asp","c#"};





/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
       
        setListAdapter(new ArrayAdapter<String>(this,R.layout.main,languages));
        ListView list=getListView();
        list.setTextFilterEnabled(true);
 
    list.setOnItemClickListener(new OnItemClickListener(){
     
       
       
       

public void onItemClick(AdapterView<?> arg0, View v1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),((TextView)v1).getText() , Toast.LENGTH_SHORT).show();
}
       
   
    });
   
   
}
    }

No comments:

Post a Comment