Display one array value according to other array - android

I have created to arrays.One is for all the states(Full Form) in USA and other is the all short form of the states in USA.I have dispayed the states(Full form) in a spinner.Now what i have to do is display the state in short form according to which the user selects in the spinner.How can i achieve it.

You should use mapping i.e in both the arrays the elements should be mapped. Let me give you an example.
String[] fullForm = { Alabama, California, Florida, Illinois};
String[] shortForm = {AL, CA, FL, IL};
ArrayAdapter<String> ad = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,fullForm );
spinner.setAdapter(ad);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), " You selected " + fullForm[position] + "\n Short form is : " + shortForm[position], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

// try this way,hope this will help you...
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Spinner
android:id="#+id/spnState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
**MainActivity.java**
public class MainActivity extends Activity {
Spinner spnState;
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
spnState = (Spinner) findViewById(R.id.spnState);
String[] stateFullNameArray = new String[]{"statefullname1","statefullname2","statefullname3","statefullname4","statefullname5"};
final String[] stateShortNameArray = new String[]{"stateshortname1","stateshortname2","stateshortname3","stateshortname4","stateshortname5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,stateFullNameArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(adapter);
spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,stateShortNameArray[position],Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

Related

Display EditText items on Spinner selection

I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}

How To Add Search Functionality in ListView

how do i add Search Functionality in my android app?
i have a listview in which i want to add search functionality , i have the code but i dont know how do i integrate it properly with my app
here is the code -
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
and here is my java file
package com.Example.app;
import android.app.Activity;
import android.content.Intent;
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;
public class MainActivity extends Activity {
private ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",
"item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1", "item 2", "item3", "item 4"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in
// list.
list1.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, array));
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),Baba.class);
startActivity(myIntent);
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true">
<EditText
android:id="#+id/inputSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:inputType="textVisiblePassword">
</EditText>
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="5dp"
>
</ListView>
</LinearLayout>
Try the below
EditText inputSearch;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
list1 = (ListView) findViewById(R.id.ListView01);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),Baba.class);
startActivity(myIntent);
}
}
});
}
Initialize editText inputSearch in onCreate.
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
Initialize adapter
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
Set the adapter op listview
list1.setAdapter(adapter);
This is where the search happens
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs); // filter based on input
}
Since you are using android's array adapter, you can use its getFilter() method too. So you just have to change minor things in your code.
Instead of setting array adapter like this
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
Create a private variable outside of the methods
private ArrayAdapter adapter;
Then initialize and set it
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array);
list1.setAdapter(adapter);
Now text changed method can call MainActivity.this.adapter.getFilter().filter(cs); without any problem

android Spinner followed by ListView - onItemSelected not called

I have had a good search around and can't seem to find an answer. The method onItemSelected is not being called. Here's the code featured in my activity's onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_trips);
try {
// if network present.. TODO
// Load Devices
deviceList = new ArrayList<Device>();
DataAsyncTask loadDevices = new DataAsyncTask(0);
loadDevices.execute(someUrl);
List<Device> deviceListNames = deviceList;
final ListView tripListView = (ListView)findViewById(R.id.trip_list);
Spinner deviceSpinner = (Spinner) findViewById(R.id.select_device_spinner);
ArrayAdapter<Device> deviceAdapter = new ArrayAdapter<Device>(this, android.R.layout.simple_spinner_dropdown_item, deviceListNames);
deviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deviceSpinner.setAdapter(deviceAdapter);
OnItemSelectedListener spinnerListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int i, long id) {
Log.d(TAG, "select is: " + parent.getSelectedItem().toString());
DataAsyncTask loadTrips = new DataAsyncTask(1);
loadTrips.execute(someUrl);
ArrayAdapter<Trip> tripAdapter = new ArrayAdapter<Trip>(getApplication(),
android.R.layout.simple_list_item_1, tripList);
tripListView.setAdapter(tripAdapter);
tripListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int i, long id) {
Log.d(TAG, parent.getSelectedItem().toString());
Intent intent = new Intent(getApplication(), TripSummary.class);
//put value of tripId to intent TODO
startActivity(intent);
}
});
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO
Log.d(TAG, "nothing selected");
}
};
deviceSpinner.setOnItemSelectedListener(spinnerListener);
} catch (Exception e){
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
Here's the activity's layout 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=".ViewTrips" >
<Spinner
android:id="#+id/select_device_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_device"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:drawSelectorOnTop = "true" />
<ListView
android:id="#+id/trip_list"
android:layout_below="#id/select_device_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/trips"
android:clickable="true" />
</RelativeLayout>
It's values are populated and displaying correctly in the Spinner. I am just having an issue with the onItemSelected method in the OnItemSelectedListener() which is set to deviceSpinner - this method is then supposed to populate the ListView, where once clicked takes the user to a new activity. Can anyone explain what I am doing wrong?
I have also tried implementing OnItemSelectedListener for the activity, which doesn't work either.
Note: There's no errors to report in LogCat,
Log.d(TAG, "select is: " + parent.getSelectedItem().toString());
is not printing.
Thanks

Adding OnItemSelectedListener to Spinner

I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!
final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.

Android Spinner getting selectedItem Value?

I have setup Spinner with the layout below, and I wanted to get the value of the item selected, not the displayed text. Where does one pull out the value? Also will this work for pulling out values the layout below? Or do I need some other way to setup (value, displayText) pair? So I guess I need to know how to set it up so it has a value and also how in onItemSelected I would pull the value?
<?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" android:paddingLeft="12dip">
<Spinner
android:id="#+id/viewSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/some_values"
android:prompt="#array/some_names"/>
</LinearLayout>
You resolve the Spinner instance in your activity class with findViewById and set an OnItemSelectedListener on it. Since you are populating the Spinner with some kind of Adapter, use the items that you constructed the Adapter with and the position of the selected item as reported back in the third parameter of OnItemSelectedListener's onItemSelected method
http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onItemSelected%28android.widget.AdapterView%3C?%3E,%20android.view.View,%20int,%20long%29
I hope it will helpful to you.
Try this Code..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<String> list=new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
list.add("Item 5");
final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
final Spinner sp2= (Spinner) findViewById(R.id.spinner2);
final Spinner sp3= (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
ArrayAdapter<String> adp2=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,str);
adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adp2);
sp2.setSelection(adp2.getPosition("Report 3"));
ArrayAdapter<CharSequence> adp3=ArrayAdapter.createFromResource(this,
R.array.str2, android.R.layout.simple_spinner_item);
adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adp3);
sp1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
String str1=sp1.getSelectedItem().toString();
//Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long id) {
// TODO Auto-generated method stub
int selected_item_position = arg2;
String selected_item=sp2.getSelectedItem().toString();
Toast.makeText(getBaseContext(), ""+selected_item_position, Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), selected_item, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp3.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
String str1=sp1.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}

Categories

Resources