单击按钮时将用户输入添加到ListView中

时间:2013-09-06 14:57:03

标签: android listview dynamic

我正在努力制作待办事项列表。我有一个EditText,Button和ListView。单击按钮,我想添加,我在EditText中键入到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;
}   
}

代码不起作用,是android开发新手,只是尝试创建一个简单的待办事项列表。谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

您需要找到列表视图,然后设置适配器:

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

答案 1 :(得分:2)

这应该这样做。

@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
    }
}