How to set selected country isd code in spinner - android

I don't know how to set the selected country isd code in
spinner.Below I have posted the code what I have been tried so far:
MainActivity.java:
public class MainActivity extends Activity implements OnItemSelectedListener {
ArrayList<String> arrCode;
ArrayList<String> arrCountry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
arrCode = new ArrayList<>();
arrCountry = new ArrayList<>();
arrCountry.add("US");
arrCountry.add("KZ");
arrCountry.add("EG");
arrCountry.add("ZA");
arrCountry.add("GR");
arrCode.add("1");
arrCode.add("7");
arrCode.add("20");
arrCode.add("27");
arrCode.add("30");
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrCountry);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_mobile_code.setAdapter(adapter_state);
sp_mobile_code.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
int spinnerValue1 = sp_mobile_code.getSelectedItemPosition();
String data = arrCode.get(spinnerValue1);
Log.e("data", "" + data);
sp_mobile_code.setPrompt(data);
/* sp_mobile_code.setSelection(position);*/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
My issue is : I can't set the isd code in same spinner.If I have to
set isd code in textview means it will be easy.But I have to set it
in the spinner
Edit: spinner list showing correctly the country list.on clicking the country list, I have to show the isd code

Use Custom ArrayAdapter.
private static class CustomSpinnerAdapter extends ArrayAdapter<String>
{
List<String> arrCodes;
public CustomSpinnerAdapter(Context context, int resource, List<String> items, List<String> arrCodes)
{
super(context, resource, items);
this.arrCodes = new ArrayList<>();
this.arrCodes = arrCodes;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView view = (TextView) super.getView(position, convertView, parent);
view.setText(arrCodes.get(position));
return view;
}
}
Check the position in the getView() and set text of your desire. Use this adapter for the Spinner

Related

How to create custom toast message for different item in a list in a custom adapter

So I have a program where I need to display a different toast depending on what item the user selects in a list. I created a custom adapter since I need to add pictures to the list and created an object that goes by the name Day that contains the day of the week, the image, and the custom text that needs to go in the toast. My problem is that I don't know how to create an onItemClickListener that will use my custom text for my toast.
This is my custom adapter
public class DayAdapter extends ArrayAdapter<Day> {
private Context mContext;
int mResource;
public DayAdapter(#NonNull Context context, int resource, #NonNull ArrayList<Day> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
String jour = getItem(position).getDay();
int image = getItem(position).getImage();
String message = getItem(position).getMessage();
Day journee = new Day(jour, image, message);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView tvJour = (TextView) convertView.findViewById(R.id.textViewJour);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.imageViewJour);
tvJour.setText(jour);
tvImage.setImageResource(image);
return convertView;
}
This is my MainActivity
public class MainActivity extends AppCompatActivity {
ListView liste;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
liste = (ListView) findViewById(R.id.dayList);
ArrayList<Day> semaine = new ArrayList<Day>();
Day lundi = new Day("Lundi", R.drawable.happyface, "Prog lundi");
Day mardi = new Day("Mardi", R.drawable.happyface, "Musique mardi");
Day mercredi = new Day("Mercredi", R.drawable.happyface, "Mercredi");
Day jeudi = new Day("Jeudi", R.drawable.beer, "Work jeudi");
Day vendredi = new Day("Vendredi", R.drawable.beer, "also work vendredi");
Day samedi = new Day("Samedi", R.drawable.malade, "also also work samedi");
Day dimanche = new Day("Dimanche", R.drawable.etude, "Love Dimanche");
semaine.add(lundi);
semaine.add(mardi);
semaine.add(mercredi);
semaine.add(jeudi);
semaine.add(vendredi);
semaine.add(samedi);
semaine.add(dimanche);
DayAdapter adapter = new DayAdapter (this, R.layout.row, semaine);
liste.setAdapter(adapter);
liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String messa = adapterView.getAdapter().getItem(i).toString();
Toast.makeText(MainActivity.this, messa, Toast.LENGTH_LONG).show();
}
});
}
}
Try making the Toast in your ArrayAdapter class, specifically in the getView method like
convertview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "Custom Toast", Toast.LENGTH_SHORT).show();
}
});
If you want, you can use an onTouchListener instead of the onClickListener.

Handle click for custom ArrayAdapter and ListView [duplicate]

This question already has answers here:
setOnItemClickListener on custom ListView
(6 answers)
Closed 6 years ago.
I have an ArrayList that has some String variables and I have a custom ArrayAdapter that I populate from the ArrayList. Now when that list in displayed and an item in the list is clicked, I want the program to do something but I don't know how to do it. I looked at a few examples but I didn't understand where exactly to put the code. So here is my code, can you tell me what to do and where to do it?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateList(listOfItems);
}
public void updateList(ArrayList<String> possibleWords){
listAdapter = new CustomListAdapter(this, R.layout.custom_list,possibleWords);
android.R.layout.simple_list_item_1, possibleWords);
final ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(listAdapter);
}
CustomListAdapter.java Class
public class CustomListAdapter extends ArrayAdapter {
private Context mContext;
private int id;
private List<String> items ;
public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}
#Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
return mView;
}
}
Here is a sample image on what the output looks like
So when "act" or "cat" are clicked, I want to do something but I don't know how to handle the click and where to put the code on what I want to do.
Put the below code on your main activity
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
//do your work here
}
});
Please just add click listener in adapter like as follows
mView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};

how to make listView redirect to website onClick?

I've been trying to make listView redirect user onClick to URL's when clicking on different elements.
Example:
clicking on "apple" would open "stackoverflow.com",
but clicking on tomato would open "google.com" etc.
Could anyone give me some advice how can I accomplish this, because after 2 days of trying and searching all I've got is a headache..
displayMainMenu.java
public class DisplayMainMenu extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_main_menu);
final String[] food = new String[] {"Apple", "Tomato", "Potato"};
ListAdapter adapter = new MyAdapter(this, food);
ListView listView = (ListView) findViewById(R.id.mainMenu);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String>
{
public MyAdapter(Context context, String[] values)
{
super(context, R.layout.entry, values);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.entry, parent, false);
String text = getItem(position);
TextView textView = (TextView) view.findViewById(R.id.listTextView1);
textView.setText(text);
return view;
}
}
}
You should make a new class that holds the shown string value (Apple, Tomato, Potato, etc) and also holds the URL that you want to link to.
Then make the ArrayAdapter use that class. The getView function you have already should suffice (when its updated to use the new class).
Then in your activity, use 'setOnItemClickListener' to set a new listener.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyItem clickedItem = () parent.getItemAtPosition(position);
<< Insert code to open the link with the URL you can get from 'clickedItem' >>
}
});
That should do it.

Getting Spinner changes in listView

I have a ListView with a CustomAdapter that has up to 50 rows( only 11 on the screen at once). Each row contains a spinner and two EditTexts. I pass 3 sets of data to the adapter for each of the three columns. When a spinner item is selected or text changed I want to amend the relevant data set inside the adapter so that it can be retrieved by the calling activity.
I can get a OnItemSelectedListener() registered against each spinner, however, I can't find a way to know which row the spinner was on. Because of that I can't update the data set.
This is the adapter.
SQLiteDatabase db;
Activity mActivity;
int [] mCategories;
String [] mComments;
String [] allCategories;
int [] mAmounts;
String [] spinnerValues;
TransCatListAdapter(Activity activity, int[] categories, String[] comments, int[] amounts){
super (activity, R.layout.transcat_row, comments);
mActivity = activity;
mCategories = categories;
mComments = comments;
mAmounts = amounts;
db = DatabaseHelper.getInstance(activity);
}
public View getView(int pos, View convertView, ViewGroup parent) {
View row = convertView;
if (row==null) {
LayoutInflater inflater=mActivity.getLayoutInflater();
row = inflater.inflate(R.layout.transcat_row, null);
}
Spinner SPNCategory = (Spinner) row.findViewById(R.id.trncatrow_category);
EditText ETComment = (EditText) row.findViewById(R.id.trncatrow_comment);
EditText ETAmount = (EditText) row.findViewById(R.id.trncatrow_amount);
SPNCategory.setAdapter(new CategorySpinnerAdapter(mActivity, R.layout.categoryspinnerstyle, DatabaseMethods.getCategories(db)));
ETComment.setText(mComments[pos]);
ETAmount.setText(Utils.formatAsMoneyString(mAmounts[pos]));
return (row);
}
So if I understand correctly, you want to be able to register an OnItemSelectedListener to your spinners but you want to be able to identify WHICH spinner it was right? Try this
getView(int pos, View convertView, ViewGroup parent) {
...
spinner SPNCategory = (Spinner) row.findViewById(R.id.trncatrow_category);
spinner.setOnItemSelectedListener(new YourSpinnerListener(pos);
...
private class YourSpinnerListener implements OnItemSelectedListener {
private int mSpinnerPosition;
public YourSpinnerListener(int spinnerPosition) {
mSpinnerPosition = spinnerPosition;
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
This class that implements the OnItemSelectedListener now has a reference to the position of the spinner.
Have fun!

How to control values and text in spinner

I want to build android app with spinner , I would like to appear in spinner some text , and when selected, it appears in TextView other text than the value of which is a selected.
for example , i want to appear in spinner website name (like= stackoverflow) and when select appear in TextView the website URL (like=https://stackoverflow.com/)
can anyone help me?
thanks in advance
First, you create yourself an Object:
public class LanguageObject {
private String title;
private String url;
public LanguageObject(String title, String url) {
this.title = title;
this.url = url;
}
public String getTitle() {
return title;
}
public Integer getUrl() {
return url;
}
}
Then, in your Activity/Fragment, you create an ArrayList with the objects you need:
ArrayList<SpinnerObject> spinnerObjectList = new ArrayList<SpinnerObject>();
spinnerObjectList.add(new SpinnerObject("Google", "www.google.com"));
spinnerObjectList.add(new SpinnerObject("StackOverflow", "www.stackoverflow.com"));
In addition to that, create a custom adapter that will handle your data:
public class SpinnerAdapter extends ArrayAdapter<SpinnerObject> {
public SpinnerAdapter (Context context, int textViewResourceId, ArrayList<SpinnerObject> spinnerObjects) {
super(context, textViewResourceId, spinnerObjects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
SpinnerObject spinnerObject = getItem(position);
if(view == null) {
LayoutInflater layoutInflater;
layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(android.R.layout.select_dialog_item, null);
if (view != null) {
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(spinnerObject.getTitle());
}
}
return view;
}
Now you can use this adapter on your Spinner, and get the url when one of the items was clicked.
You can add text to spinner like this:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> texts = new ArrayList<String>();
texts.add("Google");
texts.add("Facebook");
List<String> urls = new ArrayList<String>();
urls.add("www.google.com");
urls.add("www.facebook.com");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, texts);
spinner.setAdapter(adapter);
then you need to listen for item selected on spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String url = urls.get(i);
// do with url whatever you need
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
For adding text in spinner you can use following code in onCreate()
Spinner locale;
ArrayList<String> lList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locale =(Spinner)findViewById(R.id.Cmblocale);
// bind data to Spinner(ComboBox)
lList =new ArrayList<String>();
lList.add("English");
lList.add("Canada");
lList.add("Chinese");
lList.add("French");
lList.add("Germany");
lList.add("Japan");
ArrayAdapter<String> cAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,lList);
locale.setAdapter(cAdapter);
}
For Retrieving value use following code inside your setOnItemSelectedListener on spinner
if(locale.getSelectedItem().equals("English"))
{
txtText.setText(English);
}else if(locale.getSelectedItem().equals("Chinese"))
{
txtText.setText(Chinese);
}else if(locale.getSelectedItem().equals("Canada"))
{
txtText.setText(Canada);
}else if(locale.getSelectedItem().equals("French"))
{
txtText.setText(French);
}else if(locale.getSelectedItem().equals("Germany"))
{
txtText.setText(Germany);
}else if(locale.getSelectedItem().equals("Japan"))
{
txtText.setText(Japan);
}
Spinner spinner = (Spinner) findViewById(R.id.spinner); //create a spinner view in XML
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.your_array, android.R.layout.simple_spinner_item); // create adapter which has second parameter as the array which contains the values you want to show as entries of spinner
spinner.setAdapter(adapter); // set the adapter to your spinner and your spinner will show the values from array that you set in second parameter of adapter
To listen to the click on the options of the spinner
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
Now to show the appropriate link of the website for selected option from spinner you can have a map which has keys as same as the spinner values and values as websites as you want....
HashMap<String, String> nameWebsiteMap = new HashMap<String, String>();
nameWebsite.put("stack overflow","http://stackoverflow.com/"); // for example
So in onItemSelected you can simply do this
selectedSiteName = nameWebsite.get(parent.getItemAtPosition(pos).toString());
textView.setText(selectedSiteName);

Categories

Resources