check whether the item in spinner selected or not - java

I am trying to write a simple app for android. I want to display toast message if there is no item is selected in spinner. .I use two progress bar to do this.I have tried the code given below.
String selection =sp.getSelectedItem().toString();
System.out.println("selected item "+selection);
if(selection=="[selection]")
Toast.makeText(getActivity(), "gfdgdghfhf", Toast.LENGTH_SHORT).show();
return false;

use a string value as flag of selection
String status="not_selected";
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
MyData d = items[position];
status=d.getValue();
}
public void onNothingSelected(AdapterView<?> parent) {
status="not_selected";
}
});
if(status.equqls("not_selected"){
//show toast here
}

Related

Android Multiple Spinner

I'm sure I'm missing something simple, but I am having some logic issues with my code. I am working on a converter app. I will be using two spinners to select between to do conversions. Ex. inches to feet. I am using two simple methods to test before fleshing out all of the code. Right now if I select the value for SpinnerA in the app first, and then select the value for SpinnerB, it doesn't calculate. If I select SpinnerB first and then SpinnerA, it works. What am I missing?
spinnerA = (Spinner)getView().findViewById(R.id.spinnerA);
spinnerB = (Spinner)getView().findViewById(R.id.spinnerB);
adapterA = ArrayAdapter.createFromResource(getContext(),
R.array.conversions, android.R.layout.simple_spinner_item);
adapterA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterB = ArrayAdapter.createFromResource(getContext(),
R.array.conversions, android.R.layout.simple_spinner_item);
adapterB.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerA.setAdapter(adapterA);
spinnerB.setAdapter(adapterB);
spinnerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tempA = parent.getItemAtPosition(position).toString();
if (tempA.equals("Inches") && tempB.equals("Centimeters")){
textView.setText(String.valueOf(halfMyNum(100)));
}
else if (tempA.equals("Centimeters")){
if (tempB.equals("Inches")){
textView.setText(String.valueOf(doubleMyNum(12)));
}
}
else{
textView.setText("Please select a valid option");
}
//Toast.makeText(getContext(), parent.getItemAtPosition(position)+ " Selected"
//, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerB.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tempB = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
When you are selecting an item from spinnerA first, tempA is initialized and item listener for spinnerA is called but tempB is not yet initialized.
Then, when you select item from spinnerB, tempB is initialized and item listener for spinnerB is called. In your case, you only called the method in item listener method for spinnerA, so when you select item from spinnerB nothing actually executes. One possible solution is to call the desired method in item listener for spinnerB as well.

Set visibility TableLayout based item selected on Spinner

I want to make Spinner to set Visibility of table, i have 2 Array String "cuboid and cylinder". if i select Cuboid , cubeT table is visible and cyclinderT table is Invisible. and if i select Cylinder , cylinderT table is Visible and cubeT is Invisible.
Sample code welcome. Thank you for your time.
You can set an OnItemSelectedListener to your Spinner then using the int position argument to decide what action to take.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch (position) {
case Cuboid:
cubeT.setVisibility(View.VISIBLE);
cylinderT.setVisibility(View.GONE);
break;
....
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) { }
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String text = ((Spinner)spinner).getSelectedItem().toString();
if (Intrinsics.areEqual(text, "Cuboid")) {
//Your code here to set your "table" as cubeT if it's image in imageview
//if it's a "tableLayout" you may create 2 different layouts included and..:
setContentView(R.layout.your_cubeT_layout);
} else if (Intrinsics.areEqual(text, "Cylinder")) {
setContentView(R.layout.your_cyclinderT_layout);
}
} //when it comes to use different layouts on the same activity, generally suggestions made over fragments to make your code more dynamic but i don't know how to do that...
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I'm not using java since years so i might code bad... So no warranty!

Displaying the selected string from Android Spinner

String chooseTime[] = {"7.00 - 9.00", "9.00 - 11.00", "11.00 - 13.00", "13.00 - 15.00", "15.00 - 17.00", "17.00 - 18.30" };
spTimeConsegna = (Spinner)
rootView.findViewById(R.id.timeChooseConsegna);
adapterChooseTimeConsegna = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, chooseTime);
adapterChooseTimeConsegna.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeConsegna.setAdapter(adapterChooseTimeConsegna);
chooseTimeConsegna = chooseTime[spTimeConsegna.getSelectedItemPosition()];
I want to save the selected value of the spinner in the chooseTimeConsegna String, but it always saves the first value of the chooseTime string array indifferently what is choosen in the spinner. Why?
I think you are doing this in onCreate method, so it's only called when that activity is started, not every time you select things on spinner. Try something like this:
yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String chooseTime[] = {"7.00 - 9.00", "9.00 - 11.00", "11.00 - 13.00", "13.00 - 15.00", "15.00 - 17.00", "17.00 - 18.30" };
chooseTimeConsegna = chooseTime[position];
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
This method is gonna be called every time you select something on your spinner, and argument position is the position of the selected item in the spinner.
Replace:
chooseTimeConsegna = chooseTime[spTimeConsegna.getSelectedItemPosition()];
With:
spTimeConsegna.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
chooseTimeConsegna = chooseTime[position];
}
});
You should be good to go.

I have a list view.how i will save list view total number of click to another activity text view?

I have a list view, how can I save a list view total number of clicks to another activity text view?
I have custom list view, it has money TextView and a name TextView. When I clicked on list view total number of clicks save on another activity text view.
example like..
list view item=5
1st item clicked->another activity text view have one value.
2nd item clicked->another activity text view have two value.(prevoius and new value)
Use can use SharedPreferences for this...
int count=0;
SharedPreferences pref=getSharedPreferences("share",1);
Editor edit=pref.edit();
if(pref.contains("count"))
{
count=pref.getInt("count", 0);
}
//Do this code in OnItemClickListener()..
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
count++;
edit.putInt("count", count);
edit.commit();
Intent intent=new Intent(getActivity(),PersonalChat.class);
startActivity(intent);
}
});
You can have a countClicks int in your activity and pass this int in the intent when you create the new Activity.
Catch the click like this :
int countClick = 0;
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
countClicks ++;
}
});

Passing an _id from one activity to another when ListView item is clicked

I have a problem. In my activity i have a ListView whose values are compiled from a Database. The database returns three columns: _Id, AccountName and Comments. What i want is that the when the user clicks on one of the items in the listView, that Item's position will be extracted and then that position will be compared with the postion in the ArrayList which has the data and then when the specific field and row is found out, the _Id field will be taken out and passed to another activity in the form of a String.
You may not have understood much; this might help:
public void search(View view){
final ListView vLst_IDCommName=(ListView)findViewById(R.id.lst_IDCommName);
EditText vTxt_searchTxt=(EditText)findViewById(R.id.search_txt);
String srch_str= vTxt_searchTxt.getText().toString();
if(srch_str.length()>0){
//The DataSource for the LstView
List<Comment> values = datasource.getContacts(srch_str);
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
vLst_IDCommName.setAdapter(adapter);
}
else{
Toast.makeText(getBaseContext(), "Please type in a value to proceed!", Toast.LENGTH_LONG).show();
}
vLst_IDCommName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Here I want to create a function that will take the position of the item clicked
//and then searched for that position in the ArrayList and then return the _Id of
//the account whose value was clicked.
}});
}
This code I used to get data:
public List<Comment> getContacts(String search_str) {
List<Comment> comments = new ArrayList<Comment>();
String srch_str=(String) search_str;
Cursor cur_comments= database.rawQuery("Select * from comments where acc_name like('%"+srch_str+"%')"+" "+ "or comment like('%"+srch_str+"%')", null);
cur_comments.moveToFirst();
while (!cur_comments.isAfterLast()) {
Comment comment = cursorToComment(cur_comments);
comments.add(comment); cur_comments.moveToNext();
}
cur_comments.close();
return comments;
}
Any feedback would be appreciated.
you can try
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String currentId=yourArrayList.get(position).toString();
}
vLst_IDCommName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Object s=parent.getItemAtPosition(position);
String j=s.toString();
}});

Categories

Resources