How to pass value of spinner in doInBackground - android

I want to pass value from spinner to doInBackground in AsyncTask. I use this code:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, myArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(0);
OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Integer item = (Integer)parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
spinner.setOnItemSelectedListener(itemSelectedListener);
public void onClick(View view) {
SomeTask = new SomeTask();
SomeTask.execute();
class SomeTask extends AsyncTask<Void, Integer, Void> {
#Override
protected Void doInBackground (Void...params){
//do something with variable item
}
}
//do all next things
And I don't know how to pass variable item to doInBackground. Help me please and thank you.

Related

how to change imageview view based on spinner selection in android studio?

I am trying to display image in image view, based on spinner selection. If anyone know how to solve this please help.
public class activity_tables extends AppCompatActivity {
Spinner s1;
String[] tbls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tables);
s1 = (Spinner)findViewById(R.id.spinner);
tbls = getResources().getStringArray(R.array.tables);
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,tbls);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
}
Set onItemSelectedListener for your spinner like this
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(position==0)
{
qImageView.setImageResource(R.drawable.whatever);}
else if(position ==1)
{
qImageView.setImageResource(R.drawable.whatever);}
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

set one spinner to empty value when other one is clicked

I have two spinners, the first one shows the transport lines and the second one shows the stations of the line selected on the first spinner, the problem is that I want to set to empty value the second spinner when I click on the first one, but I can't do it.
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
I've tried a few options on the onItemClick() method of the first spinner but it's not working propertly:
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
protected void onPreExecute() {
// I'm trying all the options in this method
}
#Override
protected String[] doInBackground(String... strings) {
}
#Override
protected void onPostExecute(String[] result) {
}
}
});
Option 1:
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
Option 2:
spinner2.setAdapter(null);
Option 3:
ArrayList<String> list = new ArrayList<>();
list.add("");
spinner2.setSelection(list.size()-1);
Option 4:
arrayAdapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2);
spinner2.setSelection(arrayAdapter2.getCount());
spinner2.setOnItemSelectedListener(this);
arrayAdapter2.notifyDataSetChanged();
To sum up, there is a way to change the value of a spinner in function of the value of other one? Or just a way to change the value of a spinner to empty value?
Any help will be apreciated!
Use setOnItemSelectedListener instead of OnItemClickListener like:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos==0) // you can change condition as per your requirement
{
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
The complete code:
public class FillTransportPlaceActivity extends AppCompatActivity {
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
private ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_transport_place);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner = findViewById(R.id.listLines);
spinner.setAdapter(arrayAdapter);
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner2 = findViewById(R.id.listStations);
spinner2.setAdapter(arrayAdapter2);
SharedPreferences sp = getApplicationContext().getSharedPreferences("transportButton", 0);
boolean metro = sp.getBoolean("metro", false);
boolean bus = sp.getBoolean("bus", false);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).toString() != null) {
String choice = parent.getItemAtPosition(position).toString();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
new stationsDB().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, choice);
else
new stationsDB().execute(choice);
}
}
});
}
private class stationsDB extends AsyncTask<String, String[], String[]> {
#Override
protected String[] doInBackground(String... strings) {
return DB_transportPlace.getStations(strings[0]);
}
#Override
protected void onPostExecute(String[] result) {
updateAdapter2(result);
}
}
public void updateAdapter(String[] result) {
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner.setAdapter(arrayAdapter);
}
public void updateAdapter2(String[] result) {
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner2.setAdapter(arrayAdapter2);
}
As I said I've tried with setOnItemSelectedListener instead of OnItemClickListener and it didn't work, only execute one time in the onCreate method, I just want to change the value of spinner2 to empty when I change the value of spinner1.

Class must either be declared abstract or implement abstract method onItemSelected in OnItemSelectedListener

I am implementing spinner in Android studio. I have the below code. I am getting error MainActivity must either be declared abstract or implement abstract method onItemSelected(AdapterView<?>, View, int, long) in OnItemSelectedListener. But As you can see below, I have implemented onItemSelected(AdapterView<?>, View, int, long).
Also, when I run the code, I see error MainActivity is not abstract and does not override abstract method onNothingSelected(AdapterView<?>) in OnItemSelectedListener
What am I missing?
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getApplicationContext();
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner_supplychainEntities);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_options, android.R.layout.simple_spinner_item);
spinner.setOnItemSelectedListener(MainActivity.this);
spinner.setAdapter(adapter);
}
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//stuff here to handle item selection
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i("GTOUTOUT", "Nothing Selected");
}
});
}
You should choose only one way to implement OnItemSelectedListener, actually you have implemented both of the way of OnItemSelectedListener and not override the onItemSelected and onNothingSelected methods
//Either remove this
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//stuff here to handle item selection
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i("GTOUTOUT", "Nothing Selected");
}
});
OR REMOVE this
implements OnItemSelectedListener //from class statement
spinner.setOnItemSelectedListener(MainActivity.this);
You are doing it twice, you have to keep just one. Also, move your code inside your onCreate method:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getApplicationContext();
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner_supplychainEntities);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_options, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//stuff here to handle item selection
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i("GTOUTOUT", "Nothing Selected");
}
});
}
}
You need to implement the abstract functions defined in OnItemSelectedListener to be able to instantiate your activity class. Try this
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getApplicationContext();
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner_supplychainEntities);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_options, android.R.layout.simple_spinner_item);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//stuff here to handle item selection
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i("GTOUTOUT", "Nothing Selected");
}
}

How Can i Load ListView Dynamically Depends on the Value from Spinner

I have a Spinner,Edit Text, Button and a List View in a View. I want to Load my ListView from database Depends on the value from spinner. and i'm doing add operation on List View also.
My Doubt is that i wanted to know how can i load my list view depends on the data from spinner?
public class myClass extends ListActivity implements OnClickListener {
DatabaseFAM db;
Spinner No;
String Selected;
EditText user;
Button btnAdd;
String strselected;
ArrayList<String> list =new ArrayList<String>();
ArrayAdapter<String> adapterLV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myews);
db = new DatabaseFAM(getApplicationContext());
initVar();
}
private void initAudit2Var() {
No = (Spinner) findViewById(R.id.station);
user = (EditText) findViewById(R.id.er);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
adapterLV = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapterLV);
final DatabaseFAM db = new DatabaseFAM(getApplicationContext());
List<String> station = db.getAllstation();
ArrayAdapter<String> stationAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
station);
stationAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
No.setAdapter(stationAdapter);
No.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Selected = arg0.getItemAtPosition(
arg2).toString();
list.clear();
list=db.getTag(Selected);
adapterLV.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
you should set Adapter of ListView at the Item selection of the Spinner....
mSpinnerLangAttrValue.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
** Add ListAdapter Here **
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
add onItemSelected event to your spinner.
Depending upon the value of the seletion modify your listView with the required data
use Datasetchanged event on the listview
sp1 = (Spinner)findViewById(R.id.spinner1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// Load your list here
}}

Create Spinner Programmatically

I have to create a Spinner which is not in my Layout.
so I want to create it Programmatic I write the following code in My Activity class
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
so My question is how I can access the spinner selected item in the activity
Your only way to access it outside your listener is making a variable that will hold the position, so you would use it later.
Something like this:
public class YourActivity extends Activity {
private int curPos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
curPos = position;
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
}
In your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
The position helps you to retrieve your selected item. Which, in your case, it should be:
option[position]

Categories

Resources