Simple List View Example
SOURCE CODE [ListActivity.java] is
SOURCE CODE [main.xml] is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"> <ListView android:id="@+id/list" android:layout_width="fill_pa
rent"
android:layout_height="fill_parent" android:drawSelectorOnTop="false" />
</LinearLayout>
package com.list;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class listActivity extends Activity implements OnItemClickListener{
private ListView lv;
private static final String[] items={"lorem", "ipsum", "dolor",
"sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis",
"etiam", "vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque", "augue", "purus"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lv=(ListView)findViewById(R.id.list);
lv.setOnItemClickListener(this);
try
{
lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,items));
}catch(Exception e)
{
e.toString();
}
}
@Override
public void onItemClick(AdapterView arg0, View v, int position, long arg3) {
Toast.makeText(this, "u clicked " + items[position] ,Toast.LENGTH_LONG).show();
}
output:
No comments:
Post a Comment