I am trying to store the spinner data into SQLite database - java

I am trying to store the spinner data into SQLite database but in SQLite database cell android.widget.Spinner#411cf168 vale come
i.e value not fetch from spinner list. Please anyone help me i think my problem is in this line
ad1.insertquery(DatabaseDetail.getPAYMENT(), new String[]{ed_name.getText().toString(),ed_amount.getText().toString(),spin1.toString(),card_number.getText().toString(),ex_date.getText().toString()});
public class Payment_confirmation extends Activity implements OnItemSelectedListener
{
EditText ed_name,ed_amount,card_number,ex_date;
Button payment;
Spinner spin;
String[] accounttype = { "SBI MASTER CARD","SBI MAESTRO CARD","HDFC DEBIT/CREDIT CARD","BOB CREDIT CARD","INDIAN BANK CREDIT CARD"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.payment_confirmation);
ed_name=(EditText)findViewById(R.id.name);
ed_amount=(EditText)findViewById(R.id.amount);
card_number=(EditText)findViewById(R.id.crdnumber);
ex_date=(EditText)findViewById(R.id.date);
payment=(Button)findViewById(R.id.button2);
back = (Button)findViewById(R.id.button1);
spin=(Spinner)findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item, accounttype);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
payment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name=ed_name.getText().toString();
String amount=ed_amount.getText().toString();
String card=card_number.getText().toString();
String date=ex_date.getText().toString(); String spin1= spin.toString();
AdapterClass ad1 = new AdapterClass(getApplicationContext(), DatabaseDetail.PAYMENT);
ad1.Open();
ad1.insertquery(DatabaseDetail.getPAYMENT(), new String[]{ed_name.getText().toString(),ed_amount.getText().toString(),spin1.toString(),card_number.getText().toString(),ex_date.getText().toString()});
Cursor lCursor1 = ad1.fetchRecords(new String[]{"CUS_NAME","AMOUNT","CARD_NAME","CARD_NUMBER","EX_DATE"},null);
startManagingCursor(lCursor1);
lCursor1.moveToFirst();
if(lCursor1.getCount()!=0)
{
Intent i = new Intent(getApplicationContext(),Thankpayment.class);
startActivity(i);
}
ad1.close();
finish();
}
});
}
}

Try this way:
Spinner spinner1 = (Spinner) findViewById(R.id.spin1);
ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item, accounttype);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Object item = parent.getItemAtPosition(pos);
String spin_value = item.toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Replace
spin1.toString()
--- As it will return the spinner object reference as string like you are getting android.widget.Spinner#411cf168
With
spin1.getSelectedItem().toString() -- As it will return the spinner object's content i.e selected item as string..

When you do this:
public class SpinnerActivity extends Activity implements OnItemSelectedListener
You dont really have to write
spin.setAdapter(aa);
Just override the following methods in your class
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
}

Related

How to display ImageView when I'm choose spinner item

private String[] genre = { "Action", "Adventure", "Comedy", "Biography", "Drama", "Family", "Horror", "Romance" };
private Integer images[] = { R.drawable.img_action, R.drawable.img_adventure, R.drawable.img_biografy, R.drawable.img_comedy, R.drawable.img_drama, R.drawable.img_family, R.drawable.img_horror, R.drawable.img_romantis };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setInitialImage();
spinner = (Spinner) findViewById(R.id.label_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( // make array adapter
this, android.R.layout.simple_spinner_item, genre
);
spinner.setAdapter(adapter); // connect adapter with spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { // get item from spinner
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
int index = spinner.getSelectedItemPosition();
if () {
currImage = images[0];
Toast.makeText(getBaseContext(), "Anda memilih genre film "+ genre[index], Toast.LENGTH_SHORT)
.show();
}
setCurrentImage(); // called method
}
});
}
how, when I click one of the spinner items, then the image of the movie genre will appear.
what should I type in the if statement?
anyone can help me solve this problem?
Modify your onItemSelectedMethod to below
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
currImage = images[position];
Toast.makeText(getBaseContext(), "Anda memilih genre film "+ genre[index], Toast.LENGTH_SHORT)
.show();
setCurrentImage(); // called method
}

sqlite database updating Gridview when searching with a searchview

i was able to create a database and fill it with some foods !! and display them in a gridview take a look screenshot of my app
i want to know how i can make the search view work so that when i search for a food the gridview gets updated too !!!
the second problem is : i implemented OnItemClickListener for each subitem in the gridview
public class FoodList extends AppCompatActivity {
GridView gridView;
static ArrayList<Food> list;
FoodListAdapter adapter = null;
SearchView sv;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food_list_activity);
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new FoodListAdapter(this, R.layout.food_items, list);
gridView.setAdapter(adapter);
// get all data from sqlite
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD ");
list.clear();
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
String price = cursor.getString(2);
byte[] image = cursor.getBlob(3);
list.add(new Food(name, price, image, id));
}
adapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Food food = list.get(position);
Intent i = new Intent(FoodList.this,Details.class);
i.putExtra("position", position);
Bundle bundle = new Bundle();
startActivity(i);
}
});
}
}
but this wont work when i add the searchview !! because the positions of the subitems will change and it wont return the right position when
Food food = list.get(position);
here is the activity that i am starting when subitems are clicked
public class Details extends AppCompatActivity {
int position;
Button b;
EditText edi;
ImageView im;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
b=(Button)findViewById(R.id.hotelbu);
edi=(EditText)findViewById(R.id.hoteled);
im = (ImageView)findViewById(R.id.hotelim);
text=(TextView)findViewById(R.id.hoteltx);
if(getIntent().getExtras() != null) {
position = getIntent().getExtras().getInt("position", 0);
}
switch(position){
case 9999:
break;
default:
text.setText(FoodList.list.get(position).getName());
edi.setText(FoodList.list.get(position).getPrice());
break;
}
}
}
i hope you guys understand what i am looking for , sorry for my bad english , any help is appreciated Thank you
You must have to use AutoCompleteTextView instead of simple EditText. AutoCompleteTextView is an editable text view that shows a list of suggestions in a drop down list automatically when user is typing, from which the user can choose an item to replace the content of the edit box with.
auto_complete_textview = (AutoCompleteTextView)findViewById(R.id.auto_complete_textview);
auto_complete_textview.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
adapter.getFilter().filter(charSequence.toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Now create an adapter class that extends BaseAdapter and implements Filterable interface, override all methods of BaseAdapter and getFilter() method of Filterable interface and do all stuff what you want.
You can see the example of AutoCompleteTextView on this link
[http://www.javatpoint.com/android-autocompletetextview-example]
This is the xml code to create AutoCompleteTextView
<AutoCompleteTextView
android:id="#+id/auto_complete_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"/>

How to get String from setOnItemSelectedListener method?

I'm new to Android and I'm trying to get a String from spinner. I've made some research but I couldn't find anything useful. Without trying to get String the code is working properly. This is the code which is working:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I tried to use a getter method as follows where I didn't get any errors:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I'm trying to set this String to a text view using:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivityClassName AnObject = new MainActivityClassName();
text.setText(AnObject.getSpinnerString());
Where I get an error saying "cannot resolve method getSpin()". I do realise I'm not passing any parameters to the method but I don't know how I can do it here. I appreciate any help or any other advice to solve the problem in a different way. Thanks in advance.
This is the full code if that helps:
public class MainActivity extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = (TextView) findViewById(R.id.textView);
// spinner 1
final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
.createFromResource(this, R.array.lang,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner1.setAdapter(staticAdapter1);
// spinner 2
final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);
staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String spinnerLanguage = staticSpinner1.getSelectedItem().toString();
switch (spinnerLanguage) {
case "Afrikaans":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
.createFromResource(getBaseContext(), R.array.af,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterAF
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterAF);
break;
case "French":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.fr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterFR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterFR);
break;
case "English":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
.createFromResource(getBaseContext(), R.array.en,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterEN
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterEN);
break;
case "Turkish":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.tr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterTR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterTR);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivity AnObject = new MainActivity();
text.setText(AnObject.getSpinnerString());
}
}
You can do this:
String selectedText = null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedText = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Now you can use the selectedText variable to pass on to whatever method you want. But, keep in mind, unless you select something in the spinner, it's value would be null.
If you really have to use it outer, you can do this :
public void setTextView(String text){
text.setText(text);
}
and call this method inside your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextView(staticSpinner2.getSelectedItem().toString());
}
Having read that piece of code, why don't you extract "spinnerLanguage2" to your main class and then access it from there? Assigning the value inside the OnItemSelected method.
EDIT:
My previous answer is wrong. Try to implement an interface in order to communicate with the other class. Thus, calling a method of that interface you can pass the value. I give you a link with this (It explains it for fragments, but yo can communicate your classes too):
http://developer.android.com/intl/es/training/basics/fragments/communicating.html

How to select spinning values from listview?

I'm facing some issue with getting spinning values from list view.. in list view i have textview and related to spinner to it..
this is my Result.java
public class Result extends ListActivity{
SpinnerWrapper wrapper=new SpinnerWrapper();
Spinner sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
MyAdapter adap=new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules));
adap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setListAdapter(adap);
Button b=(Button) findViewById(R.id.button12);
//final Spinner sp=(Spinner) findViewById(R.id.spinner1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(Result.this,Cal.class);
for(int i=0;i<15;i++){
//String text= sp1.getSelectedItem().toString();
//Log.d("This", text);
}
//Spinner sp1=og.getSp();
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(wrapper);
intent.putExtra("values", sp.getSelectedItem().toString());
//intent.putExtra("values", sp.setOnItemSelectedListener(wrapper));
startActivity(intent);
}
});
}
public void getObject(Spinner sp){
this.sp=sp;
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.item_layout, parent,false);
String[] items=getResources().getStringArray(R.array.modules);
TextView tv=(TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
Spinner sp=(Spinner)row.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
getObject(sp);
//og.setSp(sp);
//sp.setOnItemSelectedListener(wrapper);
//int dd=row.getId();
return row;
}
}
}
My Spinner Wrapper class is looks like this..
public class SpinnerWrapper implements OnItemSelectedListener {
public SpinnerWrapper() {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String text=parent.getSelectedItem().toString();
int intee=parent.getSelectedItemPosition();
String pos=Integer.toString(intee);
String tex=parent.getItemAtPosition(position).toString();
Log.d("Selected", text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I want to get spinning value of each spinner ..how can i do that??
//sp.setOnItemSelectedListener(wrapper);
I see your code is commented. so the OnItemSelectedListener is not called.
You call different listener for different spinner.
I don't see why you can't get the value by
String tex=parent.getItemAtPosition(position).toString();
Log.d("You have selected", tex);
You can create an ArrayList<String> and add the selected value to that ArrayList. It will be something like this.
static ArrayList<String> sth = new ArrayList<String>();
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(
parent.getContext(),
"OnItemSelectedListener : "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
for (int i = 0; i < parent.getItemIdAtPosition(pos); i++) {
sth.add(parent.getItemAtPosition(pos).toString());
}
Log.w(getClass().getSimpleName(), "sth size " + sth.size());
}
You can pass that ArrayList around since the selected values are stored.

how to retrieve the selected value from spinner and use it in another class? - Android

as demonstrated here by jalopaba, I already created a new class: How do you get the selected value of a Spinner?
public class MyItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
and registering this to the spinner in the original class:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
However, I still can't use that selected string yet to fill in my code in the same class:
textView.setText(selected);
I'm new to this Android anyway, so this question may be too dummy to some of you
Add the setText code in the onItemSelected:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
yourTextView.setText(parent.getSelectedItem().toString);
}
Try this code,I hope It's help you.
final CharSequence[] array_min = {"No
Min","100","200","300","400", "500","600","700","800","900","1000",
"1100","1200","1300","1400","1500","1600","1700","1800","1900","2000","2500","3000","3500" };
Spinner s = (Spinner) layout.findViewById(R.id.viewSpin);
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_min);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3)
{
selItem = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use global variable..make following changes in your code
String selected="";
public class MyItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
textView.setText(selected);
After register spinner ,you can get selected item from getSelectedItem() method on any action such ocClick()
regType = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.regType, android.R.layout.simple_spinner_dropdown_item);
regType.setAdapter(adapter);
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSave:
intent.putExtra("regtype",regType.getSelectedItem().toString());
startActivity(intent);
break;}}

Categories

Resources