MainActivity.Java
package com.sattar.listviewexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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 MainActivity extends Activity {
//DataSource..........................................................
private String []osData={"Linux","Windos 7","Windos
8","Mac osx","Dos oS","Solaric","Mozila
oS","Vista","Windos Xp"};
//
//Adapter..............................................................
ArrayAdapter<String>adapter;
//View.............................................................
ListView listView;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.listView1);
adapter=new ArrayAdapter<String>(this,R.layout.list_item, R.id.textView1, osData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getApplicationContext(),
"item Index"+position+"item Name"+osData[position], Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate
the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Make Your
Choice:"
android:textColor="#000000"
android:background="#dddddd"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:dividerHeight="3dp"
android:divider="@android:color/darker_gray"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
List_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#0000ff"
android:background="#0fffff"
android:padding="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
Thanks.....................................
No comments:
Post a Comment