Home > OS >  Set background color of one item in list view if switch is turned on
Set background color of one item in list view if switch is turned on

Time:06-06

Hi I am completing a school project, and I need the background of a list item to be red with white text if the urgent switch is on. Currently, if I add an item with the urgent switch all of the background of the list view changes. I seen somewhere someone said to use an array, but when I tried that block of code it wouldn't work. Thanks in advance!

 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ListView
            android:id="@ id/listView"
            android:layout_width="match_parent"
            android:layout_height="576dp"
            android:layout_columnSpan="3"
            android:focusableInTouchMode="true"
            android:isScrollContainer="true">
    
        </ListView>
    
            <EditText
                android:id="@ id/editText"
                android:layout_width="209dp"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:hint="@string/typeHere"
                android:textSize="20dp" />
    
        <Switch
            android:id="@ id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="1"
            android:layout_weight="0"
            android:text="@string/urgent" />
    
        <Button
            android:id="@ id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_row="1"
            android:layout_column="2"
            android:layout_weight="0"
            android:text="@string/add" />
    
    
    </GridLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@ id/to_do_TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Testing"
        android:textSize="25dp" />

</LinearLayout>

public class MainActivity extends AppCompatActivity {
 Button buttonAdd;
 Boolean urgent = false;
 EditText editText;
 ListAdapter adapter;
 ListView myListView;
 Switch switch1;
 ArrayList<String> itemList = new ArrayList<String>( Arrays.asList("Clean Bathroom","Buy Apples","Buy Bananas" ));


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //declare variables
        buttonAdd = findViewById(R.id.buttonAdd);
        editText = findViewById(R.id.editText);
        switch1 = findViewById(R.id.switch1);

        ListView myListView = findViewById(R.id.listView);
        myListView.setAdapter(adapter = new ListAdapter());

      buttonAdd.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              itemList.add(editText.getText().toString());

              editText.setText("");
              //also tried setting value you here and using in getView
              if(switch1.isChecked()){
                  urgent = true;
              } else{
                  urgent = false;
              }adapter.notifyDataSetChanged();
          }
      });


    }

    // adapter class
    class ListAdapter extends BaseAdapter{

        @Override
        public int getCount() {

            return itemList.size();
        }

        @Override
        public Object getItem(int position) {
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View old, ViewGroup parent) {
            View newView =old;
            LayoutInflater inflater = getLayoutInflater();

            if(newView==null){
                newView = inflater.inflate(R.layout.item,parent,false);
            }
            TextView textview= newView.findViewById(R.id.to_do_TextView);
            textview.setText(getItem(position).toString());
            if(switch1.isChecked()){
            textview.setBackgroundColor(Color.RED);
                textview.setText(getItem(position).toString());
        }return newView;
        }
     //to do row class
    class ToDoItem {
       String name;
       Boolean urgent;


    }}}

CodePudding user response:

This happen because you use a global variable Boolean urgent to all items in list.

You need send to ListAdapter the information if is urgent with a String doing use of class ToDoItem like this:

itemList.add(new ToDoItem(itemList.add(editText.getText().toString()), switch1.isChecked()));

the full code would be:

public class MainActivity extends AppCompatActivity {
 Button buttonAdd;
 EditText editText;
 ListAdapter adapter;
 ListView myListView;
 Switch switch1;
 //Create list
 ArrayList<ToDoItem> itemList = new ArrayList<>();
//add data
 itemList.add(new ToDoItem("Clean Bathroom", false);
 itemList.add(new ToDoItem("Buy Apples", false);
 itemList.add(new ToDoItem("Buy Bananas", true);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //declare variables
        buttonAdd = findViewById(R.id.buttonAdd);
        editText = findViewById(R.id.editText);
        switch1 = findViewById(R.id.switch1);

        ListView myListView = findViewById(R.id.listView);
        myListView.setAdapter(adapter = new ListAdapter());

      buttonAdd.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              //Here set to list a item with String and boolean 
              itemList.add(new ToDoItem(itemList.add(editText.getText().toString()), switch1.isChecked()));

              editText.setText("");
              
              adapter.notifyDataSetChanged();
          }
      });


    }

    // adapter class
    class ListAdapter extends BaseAdapter{

        @Override
        public int getCount() {

            return itemList.size();
        }

        //Here get a item like ToDoItem
        @Override
        public ToDoItem getItem(int position) {
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View old, ViewGroup parent) {
            View newView =old;
            LayoutInflater inflater = getLayoutInflater();

            if(newView==null){
                newView = inflater.inflate(R.layout.item,parent,false);
            }
            TextView textview= newView.findViewById(R.id.to_do_TextView);
            //Here get a name
            textview.setText(getItem(position).name);
            if(getItem(position).urgent){
              textview.setBackgroundColor(Color.RED);
            }
            return newView;
        }
    }
  //Class that have the information on items
    class ToDoItem {
      String name;
      Boolean urgent;

        public ToDoItem(String name, Boolean urgent) {
            this.name = name;
            this.urgent = urgent;
        }
    }
}
  • Related