I'm trying to add a String (To do activity) and a boolean (toggle is urgent, untoggled is not urgent). I did this by creating a class called ToDos and having String and Boolean within the class itself.
However, I'm getting errors saying that the String should be a ToDos (the name of the class) and I have no idea how to circumvent this.
MainActivity.java
package com.example.androidlabs;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button button1;
EditText editText;
ListView listView;
ListAdapter adapter;
Switch switch1;
ArrayList<ToDos> taskList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskList.add(new ToDos("Mow the lawn",false));
taskList.add(new ToDos("Do groceries", false));
taskList.add(new ToDos("Change oil", true));
//variables
button1 = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.edittext1);
switch1 = findViewById(R.id.switch1);
listView = (ListView) findViewById(R.id.TheListView);
listView.setAdapter(adapter = new ListAdapter());
//onClick
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
taskList.add(new ToDos(taskList.add(editText.getText().toString()), switch1.isChecked()));
editText.setText("");
adapter.notifyDataSetChanged();
}
});
}
//adapter
class ListAdapter extends BaseAdapter{
@Override
public int getCount() {
return taskList.size();
}
@Override
public ToDos getItem(int position) {
return taskList.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.row_layout,parent,false);
}
TextView textview= newView.findViewById(R.id.textgoeshere);
textview.setText(getItem(position).text);
if(getItem(position).IsUrgent){
textview.setBackgroundColor(Color.RED);
}
return newView;
}
}
public class ToDos {
public String text;
public Boolean IsUrgent;
public ToDos(String text, Boolean IsUrgent) {
this.text = text;
this.IsUrgent = IsUrgent;
}
}
}
The problem is specifically with this line :
taskList.add(new ToDos(taskList.add(editText.getText().toString()),switch1.isChecked()));
However I've tried everything I can think of and I'm stumped. Anyone have any ideas?
CodePudding user response:
Your syntax is incorrect as you have taskList.add
twice
try
taskList.add(new ToDos(editText.getText().toString(),switch1.isChecked());