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
}
Related
I use a spinner in my App to open the same Activity but with different values to download Json files and show them. The first problem was, that the spinner don´t work, when i don´t add a spinner value at the 0 position and query it with if-statement (if(position >0)) in the onItemSelected method before open a new activity. Its a solution but not a very good.
The next problem is, that the value of the spinner shows every time the 0 position of the spinner list. Its same when i make a Array to fill the spinner or different Strings. Open the same Activity with different json files works fine but the spinner is all the time on position 0 value.
public class FragmentResult extends Fragment implements
GestureOverlayView.OnGesturePerformedListener, AdapterView.OnItemSelectedListener {
private ArrayList<ResultModel> arrayListResult = new ArrayList<>();
public static int dayOfMatch = 34;
private GestureLibrary gestureLibrary;
Spinner spinnerDay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//matchDayCheck();
ServerResult serverResult = new ServerResult(getActivity().getApplicationContext(),
"https://www.dein-weg-in-die-cloud.de/tomcat7/RestSoccer/fussball/spieltag/" + dayOfMatch);
serverResult.execute();
String downResult = null;
try {
downResult = serverResult.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
JsonParser jsonParser = new JsonParser();
arrayListResult = jsonParser.parseJSONResult(downResult);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
GestureOverlayView gestureOverlayView = new GestureOverlayView(getActivity());
View view = inflater.inflate(R.layout.layout_result, container, false);
ListView result_list = (ListView)view.findViewById(R.id.result_list);
AdapterResult adapterResult = new AdapterResult(getActivity().getApplicationContext(), arrayListResult);
result_list.setAdapter(adapterResult);
gestureOverlayView.addView(view);
gestureOverlayView.setFadeEnabled(false);
gestureOverlayView.addOnGesturePerformedListener(this);
gestureOverlayView.setGestureColor(Color.TRANSPARENT);
gestureOverlayView.setUncertainGestureColor(Color.TRANSPARENT);
gestureOverlayView.setFadeOffset(0);
gestureOverlayView.setHapticFeedbackEnabled(true);
gestureLibrary = GestureLibraries.fromRawResource(getActivity().getApplicationContext(), R.raw.gestures); //!!
if (!gestureLibrary.load()) {
getActivity().finish();
}
// Spinner element
Spinner spinnerYear = (Spinner)view.findViewById(R.id.spinnerResultYear);
spinnerDay = (Spinner)view.findViewById(R.id.spinnerResultDay);
// Spinner click listener
spinnerYear.setOnItemSelectedListener(this);
spinnerDay.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> spinnerListYear = new ArrayList<String>();
spinnerListYear.add("2013/2014");
List<String> spinnerListDay = new ArrayList<String>();
spinnerListDay.add("");
for(int i = 1; i<=34; i++){
spinnerListDay.add(i + ".Spieltag");
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerYearAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, spinnerListYear);
ArrayAdapter<String> spinnerDayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, spinnerListDay);
// Drop down layout style - list view with radio button
spinnerYearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerYear.setAdapter(spinnerYearAdapter);
spinnerDay.setAdapter(spinnerDayAdapter);
/*result_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ResultModel resultModel = (ResultModel) parent.getItemAtPosition(position);
int matchId = resultModel.getMatchId();
Intent matchIntent = new Intent(getActivity(), MatchActivity.class);
matchIntent.putExtra("MatchId", matchId);
matchIntent.putExtra("Matchday", dayOfMatch);
startActivity(matchIntent);
}
});*/
return gestureOverlayView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
if(position >0) {
final Intent intent;
intent = new Intent(getActivity().getApplicationContext(), ResultActivity.class);
dayOfMatch = position;
intent.putExtra("dayOfMatch", dayOfMatch);
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
.
.
.
}
My target is to show part of selected spinner item string, as selected item. In Spinner options I want show full string, but when selected only first part of it. Example:
Selected: "Racing Car" > show: "Racing",
Selected: "Fast car" > show: "Fast"
Does that possible? My code:
private void defineRacingCarsArray(){
racingCarsArray = Arrays.asList(
"Racing Car",
"Fast Car"
);
}
private void defineRacingCarsCodeSpinner(){
racingCarsCodeSpinner = (Spinner) view.findViewById(R.id.cars_code_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < racingCarsArray.size(); ++i) {
adapter.add(racingCarsArray.get(i));
}
racingCarsCodeSpinner.setAdapter(adapter);
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
String car = racingCarsArray.get(position);
;
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this,
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// here you have to update
TextView selectedView = (TextView) view;
selectedView.setText(racingCarsArray.get(position).split(" ",2)[0]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The method onItemSelected doesn't work and I don't know why.
Below is my code.
Array adapter:
public class MyListAdapter extends ArrayAdapter {
Spinner spinner;
ListView listView;
/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
System.out.println("ciao1");
// spinner.setOnItemSelectedListener(this);
return row;
}
/* #Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) spinner.getSelectedItem();
System.out.println(selState);
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}*/
}
fragment:
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
// listView.setOnItemClickListener(this);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
listView.setSelection(position);
String selState = (String) listView.getSelectedItem();
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
On ListViews you use onItemClicked. OnItemSelected is for Spinners. It's one of those Android nuisances...
In your method of setting OnItemSelectedListener(this) within the ArrayAdapter, I believe the issue is that, because every spinner in the list references the same instance of MyListAdapter, your OnItemSelected method is not distinguishing which spinner within your list was actually selected from-- it just references the spinner object that was last set to the MyListAdapter.spinner reference, which should be the last one in the list.
To remedy this, don't use spinner.getSelectedItem(). Instead use ((Spinner) parent).getSelectedItem(), since the parent object will be the actual spinner selected from. And at that point, you should make the spinner variable local to the getView method.
In the method of calling listView.setOnItemSelectedListener(...) in the fragment, I expect you meant to have listView.setOnItemClickListener(...), but that will listen for a click on a row in your list, not a selection from a spinner.
To detect a selection from one of your spinners, make the first modification above. To detect a click on a row in your listView as well, change to listView.setOnItemClickListener. Without more context for your objective or the actual errors occurring, it's hard to tell if both or just the first is desired.
EDIT:
From your comments, I think you should have this in your MyListAdapter:
public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener {
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Spinner spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
parent.getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
return row;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) ((Spinner) parent).getSelectedItem();
System.out.println(selState);
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
And this in your fragment (no listeners):
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
listView.setAdapter(listAdapter);
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
}
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.