postheadericon Simple ListView Example in Android





ListViewMainActivity.java

package com.example.listviewdemo;

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.TextView;
import android.widget.Toast;

public class ListViewMainActivity extends Activity {

ListView lv;
TextView Selection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_main);
lv=(ListView) findViewById(R.id.list);
Selection=(TextView) findViewById(R.id.textView1);
String[] items={"Gujarati", "Hindi","English"};

ArrayAdapter<String> aa= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items);
lv.setAdapter(aa);

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
   Selection.setText(lv.getItemAtPosition(arg2).toString());
}
} );

}



}



activity_list_view_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListViewMainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="73dp" >
    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Select Iteam "
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.