Search code examples
javaandroidobjectarraylistandroid-arrayadapter

How to populate an ArrayList from EditText and a button / Initializing an object


Hello how can I populate my ArrayList with an EditText and a button, the problem is that I don't know how from EditText I can initialize an object and then add It to my ArrayList

Thank you

enter image description here

//Main activity

  public class MainActivity extends AppCompatActivity {

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

        ListView view = (ListView)findViewById(R.id.listview);
        EditText edit = (EditText)findViewById(R.id.editText);
        Button button = (Button)findViewById(R.id.button);

        Todo eat = new Todo("Eat ");
        Todo sleep = new Todo("Sleep");

        ArrayList<Todo> todolist = new ArrayList<>();
        todolist.add(eat);
        todolist.add(sleep);

        Todoadapter adapter = new Todoadapter(this,R.layout.custom_adapter_layout,todolist);
        view.setAdapter(adapter);
    }
}

// Object

public class Todo {
    private String todo;
    public Todo(String todo) {
        this.todo = todo;
    }
    public String getTodo() {
        return todo;
    }
    public void setTodo(String todo) {
        this.todo = todo;
    }
}

//ArrayAdapter

public class Todoadapter extends ArrayAdapter<Todo> {

private Context mcontext;
int mresource;
private List<Todo> objects = new ArrayList<>();

public Todoadapter(@NonNull Context context, int resource, @NonNull ArrayList<Todo> objects) {
    super(context, resource, objects);
    this.mresource = resource;
    this.mcontext = context;
    this.objects = objects;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    String todo = getItem(position).getTodo();
    LayoutInflater inflater = LayoutInflater.from(mcontext);
    convertView = inflater.inflate(mresource, parent, false);
    final CheckBox box = (CheckBox) convertView.findViewById(R.id.checkBox2);
    box.setText(todo);
    box.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(box.isChecked()){
                //Delay to see animation
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        objects.remove(getItem(position)); //Your object list where you have the itens
                        notifyDataSetChanged(); //If you are using a recycler view.
                    }
                },300); //adding 0.3 sec delay
            }
        }
    });
    return convertView;

}

}

enter image description here


Solution

  • First add listener for button either in XML or via java

    activity_main

    <Button ...
      android:id="@+id/button"
      android:onClick="addTodo"/>
    

    In MainActivity.java

    1.) Make adapter and list instance global to class

    2.) Create addTodo method (keep it public and add View parameter)

    3.) After click, fetch data from edittext, create a Todo instance and add it to list

    4.) Notify the adapter to reflect the changes

    public class MainActivity extends AppCompatActivity {
        // 1
        private ArrayList<Todo> todolist;
        private Todoadapter adapter;
        private EditText edit;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ListView view = (ListView)findViewById(R.id.listview);
            edit = (EditText)findViewById(R.id.editText);
            Button button = (Button)findViewById(R.id.button);
    
            Todo eat = new Todo("Eat ");
            Todo sleep = new Todo("Sleep");
    
            todolist = new ArrayList<>();
            todolist.add(eat);
            todolist.add(sleep);
    
            adapter = new Todoadapter(this,R.layout.custom_adapter_layout,todolist);
            view.setAdapter(adapter);
        }
        // 2
        public void addTodo(View view){
            // 3
            todolist.add(new Todo(edit.getText().toString()));
            // 4
            adapter.notifyDataSetChanged();
        }
    }