Spinner & ListView
Spinners is a type of user interface that facilitate a smart selection of item from a set of items. It's also known as Drop-down. On Touching the spinner provide a drop-down menu of set of items, by default it shows a currently selected item.
ListView is a type of user interface and a view group that provide a view of list with scroll able items.The list items are auto inserted to the list using an Adapter. Adapter is bridge that coordinate between code of different nature.Here it insert content from a Java source to XML source. Java source may be an array or database query and XML are ListView and Spinner etc.Adapter will pick data items from Java source and place it as XML Spinner or ListView contents.
ListView is a type of user interface and a view group that provide a view of list with scroll able items.The list items are auto inserted to the list using an Adapter. Adapter is bridge that coordinate between code of different nature.Here it insert content from a Java source to XML source. Java source may be an array or database query and XML are ListView and Spinner etc.Adapter will pick data items from Java source and place it as XML Spinner or ListView contents.
File Name
|
Code
|
strings.xml
|
<resources>
<string name="app_name">Listing</string>
<string-array name="gender_type">
<item>Male</item>
<item>Female</item>
</string-array>
</resources>
|
XML Layout
activity_main.xml
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Spinner
android:id="@+id/gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/gender_type"/>
<Spinner
android:id="@+id/marital_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
|
Java Code
MainActivity.java
|
public class MainActivity extends AppCompatActivity {
Spinner maritalStatus;
String[] status={"Married","Single"};
ArrayAdapter adapter;
//////////////////////////
ListView listView;
String[] contacts={"Nabel","Humash","Huma"};
ArrayAdapter contactAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
maritalStatus=findViewById(R.id.marital_status);
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_dropdown_item_1line,status);
maritalStatus.setAdapter(adapter);
maritalStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,"Item Selected :"+status[i],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//////////////////////////////////
listView=findViewById(R.id.list);
contactAdapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,contacts);
listView.setAdapter(contactAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,"Item Selected :"+contacts[i],Toast.LENGTH_SHORT).show();
}
});
}
|
Comments
Post a Comment