1

我正在尝试制作一个待办事项列表。我有一个 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开发的新手,只是试图创建一个简单的待办事项列表。谢谢你的帮助!

4

2 回答 2

2

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

  lv=(ListView)findViewById(android.R.id.yourlistview);
  lv.setAdapter(adapter);
于 2013-09-06T15:08:11.803 回答
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
    }
}
于 2013-09-06T15:11:46.677 回答