Change colour of selected items - android

I made a listview and a spinner with 3 options. I want that spinner to connect with Listview on other intent.
The spinner is on a Edit page so whenever I click on 1 item on the list I get to the edit page.
I want the first option on the spinner to make the selected item on the list green the second will make it yellow and then third will make it red. How do I do it?
public class EditList extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_list);
final Button button=(Button)findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(EditList.this,Vans.class);
startActivity(intent);
}
});
// Spinner element
spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(EditList.this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("רכב עובד");
categories.add("יש תקלה");
categories.add("רכב במוסך");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String work = parent.getItemAtPosition(position).toString();
if (position==0){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.GREEN);
}
Vans.listView.setBackgroundColor(position);
spinner.setSelection(position);
String takala = parent.getItemAtPosition(position).toString();
if (position==1){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.YELLOW);
}
String garage = parent.getItemAtPosition(position).toString();
if (position==2){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.RED);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Related

Android - Two spinners in one Activity - Saving Data is not working

my problem is the following. I have 2 Spinners in one activity. I want that both spinner items should be saved in the DB. But there is a slight difference between both spinners:
Spinner 1: Is populated by data from other db-table - when it is the only spinner in the mentioned activity, it works.
Spinner 2: Has default values. I am not sure if added it correctly to my activity. Anyway, after i added it to my saving the data is not working anymore.
Please help me what am I doing wrong??
My Activity Code:
public class activity_transport extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
DatabaseHelper myDb;
Button btn_save;
Spinner ChooseProject,ChooseTransport;
EditText Entfernung,Price,MWST;
String selectedspinner, selectedTransport;
ImageView imageView2;
private static int PICK_IMAGE = 100;
Uri imageUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
myDb = new DatabaseHelper(this);
ChooseProject = (Spinner) findViewById(R.id.ChooseProject);
ChooseTransport = (Spinner) findViewById(R.id.ChooseTransport);
Entfernung = (EditText) findViewById(R.id.Entfernung);
Price = (EditText) findViewById(R.id.Preis);
MWST = (EditText) findViewById(R.id.MwSt);
btn_save=(Button) findViewById(R.id.btn_save);
//bildanzeigebereich im layout
imageView2=(ImageView) findViewById(R.id.imageView2);
loadProjectSpinnerData();
//loadTransportSpinnerData();
SaveData();
//+++++++++++++++Function to load the spinner data from SQLite database++++++++++//
private void loadProjectSpinnerData() {
// database handler
DatabaseHelper db = new DatabaseHelper (getApplicationContext());
// Spinner Drop down elements
List<String> projects = db.getAllProjects();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseProject.setPrompt("Projekt auswählen");
// attaching data adapter to spinner
ChooseProject.setAdapter(dataAdapter);
//Choose Transport Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.ChooseProject)
{
selectedspinner = (String) ChooseProject.getSelectedItem();
}
else if(spinner.getId() == R.id.ChooseTransport)
{
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*private void loadTransportSpinnerData() {
// Spinner method to read the on selected value
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Creating adapter for spinner
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
//++++++++++++Save Data++++++//
public void SaveData(){
btn_save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.createTransport(
selectedspinner,
selectedTransport,
Integer.valueOf(Price.getText().toString()),
Integer.valueOf(MWST.getText().toString()),
Integer.valueOf(Entfernung.getText().toString()),
imageViewToByte(imageView2)
);
if(isInserted=true)
Toast.makeText(activity_transport.this, "Daten gespeichert", Toast.LENGTH_LONG).show();
else
Toast.makeText(activity_transport.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show();
}
}
);
}
}
EDIT:
I tried to run the app by setting a few breakpoints.
When trying to run this line selectedTransport = (String) ChooseTransport.getSelectedItem(); the debugger posts Cannot find local variable 'ChooseTransport'
I solved the problem:
I implemented an own method for ChooseTransport- Spinner and now it works. I also figured out that the order how I add the data to the DB was not right. I changed this too.
See here my method for my second spinner:
private void loadTransportSpinnerData(){
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.transport, android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
ChooseTransport.setAdapter(spinnerArrayAdapter);
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}

Opening different layouts based on values selected in two spinners

I am new to android,and in this code i have two spinners and based on the values selected,different layouts should open accordingly. Im stuck with what logic should i use to open different layouts, inside the OnItemSelected for the spinner.Say i have 5 items in spinner and i have 5 different layouts for each item.So how should i do??
Any help or ideas would be much appreciated. Thanks in advance.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(i);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner1 = (Spinner)parent;
Spinner spinner2 = (Spinner)parent;
if(spinner1.equals(position))
{
}
if(spinner2.getId() == R.id.spinner2)
{
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
Try below.
check the onClickListener of button which will decide which activity to open.
Spinner spinner1;
Spinner spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
if(spinner1_selected_item.equals("Maruti"))
{
if(spinner2_selected_item.equals("1Lakh-5Lakh"))
i=new Intent(SecondActivity.this,ThirdActivity.class);
if(spinner2_selected_item.equals("5Lakh-10Lakh"))
i=new Intent(SecondActivity.this,FourthActivity.class);
if(spinner2_selected_item.equals("10Lakh-20Lakh"))
i=new Intent(SecondActivity.this,FifthActivity.class);
}
startActivity(i);
}
});
}
String spinner1_selected_item;
String spinner2_selected_item;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinner1.getId() == R.id.spinner1)
{
spinner1_selected_item = parent.getItemAtPosition(position).toString();
}
if(spinner2.getId() == R.id.spinner2)
{
spinner2_selected_item = parent.getItemAtPosition(position).toString();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}

I want to display selected item with some title in Spinner,Like item is:item6

I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

Spinner data accept is mulfunctioning

I have a spinner which displays both options, but when I accept the female option, it still takes the answer as a male, any suggestions?
List<String> SpinnerArray = new ArrayList<String>();
SpinnerArray.add("Male");
SpinnerArray.add("Female");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner Items = (Spinner) findViewById(R.id.genderSpinner);
Items.setAdapter(adapter);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
String selected = Items.getSelectedItem().toString();
if (selected.equals("Male")) {
inputGen = "Male";
}
if (selected.equals("Female")){
inputGen = "Female";
}
Following the official guide:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
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
}
You can try a switch of position and then try your same conditions.

Android - Spinner not able to select items

I have a problem with my spinner. I add the items on the spinner dinamically, from a edittext when a button it's pressed. The items are correctly shown when the spinner is pressed, but no selection can be done and there's no display on the spinner.
Here's the code:
final ArrayList<String> players = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_dropdown_item, players);
final Spinner spinnerplayers = (Spinner)findViewById(R.id.spinnerplayers);
final ImageButton addbutton = (ImageButton)findViewById(R.id.addbutton);
final EditText editname = (EditText)findViewById(R.id.editname);
final EditText editnum = (EditText)findViewById(R.id.editnum);
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
players.add(editname.getText().toString()+ " " + editnum.getText().toString());
editname.setText("");
editnum.setText("");
}
});
spinnerplayers.setAdapter(adapter);
final ImageButton removebutton = (ImageButton)findViewById(R.id.removebutton);
removebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//players.remove(spinnerplayers.getSelectedItem().toString());
}
});
If it's needed, the spinner xml is:
<Spinner
android:id="#+id/spinnerplayers"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
Any idea about where's the problem?
Thanks in advance.
Have you tried to specify:
adapter.setNotifyOnChange(true);
to refresh adapter showing each add/remove you make ?
The reason is you set it with android.R.layout.simple_spinner_item.
You can write your own Adapter class:
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
spinner.setAdapter(new MyCustomAdapter(CLASS.this, R.layout.spinner_layout, players));
Then it will look like the way you design in spinner_layout
You have to make a custom adapter for this if you mean the clicklistenrs on your button are not working. You will apply listeners in getView method of the adapter.
Add you have to add OnItemSelectedListener.
YOUR_SPINNER.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The position gives you your item.

Categories

Resources