在Android中动态编辑ListView

时间:2014-05-07 09:38:14

标签: android android-listview android-edittext

假设我已经使用ListView创建了一个列表。该列表由颜色名称组成。 现在我拿一个EditText文本字段和一个Button小部件。我输入文字(颜色名称)并按下按钮。输入的颜色名称应添加到列表中。任何人都可以帮我解释代码吗?

2 个答案:

答案 0 :(得分:3)

MainActivity类

package com.example.asd;

import android.os.Bundle;
import android.app.Activity;
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;

public class MainActivity extends Activity
{

private ListView list;
private EditText et;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapter= new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
    adapter.add("white");
    adapter.add("black");
    adapter.add("read");
    list=(ListView)findViewById(R.id.listView1);
    list.setAdapter(adapter);

    et=(EditText)findViewById(R.id.editText1);

    Button b=(Button)findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
                adapter.add(et.getText().toString());           
        }
    });


}



}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="25dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:text="Button" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:cacheColorHint="#00000000"


    android:layout_below="@+id/button1" >
</ListView>

</RelativeLayout>

答案 1 :(得分:0)

我认为你正在寻找这个......

public class Colors extends Activity {

private EditText et1;
private Button but1;
private ListView list;
private ArrayList<String> colornames=new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1  = (EditText)findViewById(R.id.et1);
    but1 = (Button)findViewById(R.id.but1);
    list = (ListView)findViewById(R.id.list1);

    but1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            colornames.add(et1.getText().toString());
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(Colors.this, android.R.layout.simple_list_item_1,colornames);
            list.setAdapter(adapter); 
        }
    });
}

输出看起来像这样

enter image description here