MainActivity
package com.example.adapter;import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String>cnt=new ArrayList<String>();
cnt.add("India");
cnt.add("Pakistan");
Spinner spn=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String>adp=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,cnt);
spn.setAdapter(adp);
final List<String>india=new ArrayList<String>();
india.add("punjab");
india.add("HP");
india.add("MP");
final List<String>pak=new ArrayList<String>();
pak.add("p");
pak.add("pl");
pak.add("pp");
spn.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
if(position==0)
{
Spinner spn=(Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String>adp=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,india);
spn.setAdapter(adp);
}
else if(position==1)
{
Spinner spn=(Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String>adp1=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,pak);
spn.setAdapter(adp1);
}}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
Xml File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.adapter.MainActivity"
tools:ignore="MergeRootFrame" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.adapter.MainActivity"
tools:ignore="MergeRootFrame" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner1"
android:layout_marginTop="15dp" />
</RelativeLayout>
No comments:
Post a Comment