1

I am trying to make a TO DO LIST. I have a EditText, Button, and ListView. On button click I want to add, what I typed into the EditText into a ListView.

main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
    android:id="@+id/editText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter task"
    android:textSize="24dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<Button
    android:id="@+id/addTaskBtn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add Task"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true" />
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="        Task"
    android:id="@+id/header"
    android:layout_below="@+id/addTaskBtn"
    android:background="#5e5e5e"
    android:textColor="#FFFFFF"
    android:textSize="14dp"/>
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    android:layout_below="@+id/header"
    android:layout_centerHorizontal="true" />

Main_ToDoList.java

package com.example.todolist;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;

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

    btnAdd = (Button)findViewById(R.id.addTaskBtn);
    btnAdd.setOnClickListener(this);
    et = (EditText)findViewById(R.id.editText);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
    lv.setAdapter(adapter);
}
public void onClick(View v)
{
    String input = et.getText().toString();
    if(input.length() > 0)
    {
        list.add(input);
        adapter.notifyDataSetChanged();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main__to_do_list, menu);
    return true;
}   
}

The code doesn't work, new to android developing, and just trying to create a simple To Do List. Thanks for your help!

2
  • 1
    why don't you simply do adapter.add(input)? Commented Sep 6, 2013 at 15:04
  • 1
    also, lv is not initialized. there should be a findViewById for that. Commented Sep 6, 2013 at 15:04

2 Answers 2

2

This should do it.

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

    btnAdd = (Button)findViewById(R.id.addTaskBtn);
    btnAdd.setOnClickListener(this);
    et = (EditText)findViewById(R.id.editText);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

    // set the lv variable to your list in the xml
    lv=(ListView)findViewById(R.id.list);  
    lv.setAdapter(adapter);
}
public void onClick(View v)
{
    String input = et.getText().toString();
    if(input.length() > 0)
    {
        // add string to the adapter, not the listview
        adapter.add(input);
        // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, thanks. One more question, how would I go about implementing a delete from the listview? So user input added, and then when I click the item maybe a "ask to delete" appears.
create another button, set it to a new view.onclicklistener to get the text, and then call adapter.remove(input); more info
Would I want to create a onItemClickListener()? So when I click the specific item in the listview it will then get deleted?
Last question: I have the insert and delete working and I added a alertDialog, now I want to save the data. What would the best way to go about doing this?
2

you need to find your listview and then set the adapter:

  lv=(ListView)findViewById(android.R.id.yourlistview);
  lv.setAdapter(adapter);

1 Comment

Thank you. I had it the first time, then removed it b/c it didn't work how I had it initially setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.