新增用户输入按钮点击一个ListView

新增用户输入按钮点击一个ListView

本文介绍了新增用户输入按钮点击一个ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个待办事项清单。我有一个的EditText,按钮和ListView。按钮点击我要补充,我输入到的EditText成一个ListView。

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

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

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

在code不起作用,新到Android发展,只是想创建一个简单的任务列表。感谢您的帮助!

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

推荐答案

这应该这样做。

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

这篇关于新增用户输入按钮点击一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:34