SOLVED HERE IS THE SOLUTION ANSWER http://www.congdegnu.es/2011/06/02/spinners-en-android-tres-formas-de-poblarlos/
I'm populating a spinner from my sqlite database like this:
Cursor CS = newDB.rawQuery("Select ID AS _id, Name from Schools",null);
CS.moveToFirst();
do{
Schools.add(CS.getString(CS.getColumnIndex("_id")));
} while(CS.moveToNext());
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,Schools);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
My problem is that I only add an id but how can I add a value to that id so when the value get select I get the id ?
I think that "schools" is an array containing you own class "school".
You could add a propertie which contains the information.
Or you could use a 2 dimensional array.
Than you have to set up your arrayadapter to get the information (getter Method).
Hope it helps
Try Cursor Adapter
or
Take a global variable
Hashmap schoolmap=new Hashmap();
While inserting data to the list also add to Hashmap like this
CS.moveToFirst();
do{
Schools.add(CS.getString(CS.getColumnIndex("_id")));
schoolmap.put(CS.getString(CS.getColumnIndex("_id")),CS.getColumnIndex("_id"))
} while(CS.moveToNext());
onSpinner item click get the value like this
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String value=((TextView)view).getText();
int id=Integer.parseInt(schoolmap.get(value));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Take a look at using a SimpleCursorAdapter for your spinner instead. Although this has been deprecated since API-11, so you may also want to think about using a LoaderManager with a CursorLoader.
An explanation on how to transition to a LoaderManager and CursorLoader can be found here:
How to transition from managedQuery to LoaderManager/CursorLoader?
I would recommend you start by using the SimpleCursorAdapter, then if confortable enough, move on to the other method.
1) Create an ArrayList<School>
2) Add all your Schools to the Arraylist
3) Create a custom adapter like this:
public class SchoolAdapter extends ArrayAdapter<School>{
public SchoolAdapter(Context ctx, List<School> schools){
super(ctx, 0, schools);
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
TextView tv;
if (convertView == null){
tv = new TextView(getContext());
} else {
tv = (TextView) convertView;
}
tv.setTextSize(16);
final School school = getItem(position);
tv.setText(school.toString());
return tv;
}
}
4) Use this in your activity:
final SchoolAdapter adapter = new SchoolAdapter(this, schools);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
School school = adapter.getItem(pos);
// Do whatever you want with your school
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
i create in a two way data binding spinner in Android, and added value into with a result of a REST GET.
I need now to add the first value of the spinner as an empty string that when it will be selected from the spinner, the value will be null.
In the fragment class i wrote this code:
//spinner
appCompatSpinner = binding.spinner;
spinnerAdapter = new SpinnerAdapter(getActivity(), viewModel.usersDetailsList );
appCompatSpinner.setAdapter(spinnerActorAdapter);
appCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
viewModel.onSelected(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mAdapter.notifyDataSetChanged();
Can someone please help me to achieve that?
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.
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!
I would like to have my application populate an empty textview with the value that a user selects from a spinner. Currently it seems to work, but it only populates the textview with what I'm guessing is the entire cursor: (android.database.sqlite.SQLiteCursor#4120dd08).
Can someone help me figure out what the issue is? I can post more code if necessary.
// Set spinner listener to populate the description field onclick.
mContext = this;
commonDescSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
String stringItem = parent.getItemAtPosition(position).toString();
mDescriptionText.setText(stringItem);
}
public void onNothingSelected(AdapterView<?> parent) {}
});
Try this:
// Set spinner listener to populate the description field onclick.
mContext = this;
commonDescSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
TextView objTextView = (TextView) commonDescSpin.getSelectedView();
String stringItem = objTextView.getText().toString();
mDescriptionText.setText(stringItem);
}
public void onNothingSelected(AdapterView<?> parent) {}
});
The method toString() usually has other implementations relating to identifying object instances. If you know that the item in the spinner is without a doubt a string, you could consider performing an explicit cast:
String stringItem = (String)(spinner1.getItemAtPosition(2));
textViewStatus.setText(stringItem);
I have created a spinner and the items of spinner comes from database. However, When I use
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
typeOFBCard = contactSpinner.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
When I call this listener and try to pick the chosen string of the spinner i get a reference of the sglite something like:
android.database.sqlite.SQLiteCursor#40535568
This is the return value of typeOfBCard.
However, on the spinner I can see normal string like "Work".
Here is how I initialized the spinner :
contactSpinner = (Spinner) findViewById(R.id.contactSpinner);
mobileText =(EditText) findViewById(R.id.mobileText);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
cursor = mDbHelper.fetchAllBusinessCards();
startManagingCursor(cursor);
context =this;
contactSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
How ever on the spinner I can see normal string like "Work"
That is because you configured an Adapter on the Spinner, and the Adapter is pulling data out of the Cursor to display.
How to get spinner string value on android?
There is no "spinner string value". Spinners don't have strings. They have views. Those views might be instances of TextView, or they might be instances of ImageView, or they might be instances of a LinearLayout holding onto a TextView and an ImageView, or...
If you want to get data out of the Cursor, call getString() on the Cursor.
Every row in a spinner is a view but it's also a value/object from your source.
Try
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Parent == where the click happened.
typeOFBCard = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}