Change colour of selected items

时间:2017-03-22 18:53:01

标签: android listview spinner

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
}


}

0 个答案:

没有答案