如何使用适配器在按钮单击上添加ListView项目

时间:2014-03-03 10:56:31

标签: android android-listview

如何获取在EditText中输入的数据,并在该窗口中单击“提交”,应将其添加到以前的活动列表视图项目中? 我需要做的是:

  1. 创建EditText并提交按钮
  2. 在同一个活动中创建列表视图
  3. 点击提交按钮,它应该在列表视图中显示。
  4. 我在这里看到了类似的问题:add items to listview dynamically android

    但我无法理解答案。有人请解释如何做到这一点。

3 个答案:

答案 0 :(得分:10)

您只需执行以下操作: 像这样准备你的xml:

<?xml version="1.0" encoding="utf-8"?>
<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" >

  <EditText
     android:id="@+id/editText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_toLeftOf="@+id/addItem"
     android:hint="Add a new item to List View" />

  <Button
     android:id="@+id/addItem"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:text="Add" /> 

  <ListView
     android:id="@+id/listView"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/editText" >
  </ListView>

</RelativeLayout>

活动如下所示:

public class MainActivity extends Activity {
    EditText editText;
    Button addButton;
    ListView listView;
    ArrayList<String> listItems;
    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        addButton = (Button) findViewById(R.id.addItem);
        listView = (ListView) findViewById(R.id.listView);
        listItems = new ArrayList<String>();
        listItems.add("First Item - added on Activity Create");
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems);
        listView.setAdapter(adapter);
        addButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                listItems.add(editText.getText().toString());
                adapter.notifyDataSetChanged();
            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
}

答案 1 :(得分:3)

创建String Arraylist并初始化

    ArrayList<String> str1 = new ArrayList<String>();

在其上添加一些值

     str1.add("First Row");
     str1.add("Second Row");
     str1.add("Third Row");
     str1.add("Fourth Row");
     str1.add("Fifth Row");

然后将Adapter设为ListView

adapter=new ListAdapter(this,str1);
list.setAdapter(adapter);

然后将您的EditText文本添加到str1中,然后将其称为adapter.notifyDataSetChanged();,如

str1.add(edit_message.getText().toString());
adapter.notifyDataSetChanged();

尝试将此代码添加到Button onclick()

演示输出:

enter image description here

答案 2 :(得分:1)

假设你有一个arraylist

ArrayList<String> dataList = new Arraylist ();

所以On clicking of button, you need to add the new data item into your data arraylist.

首先,在edittext中输入值并存储在字符串中。

String editedValue = yourEditText.getText.toString();

然后我们需要在我们的datalist中添加它。

喜欢

dataList.add(editedValue);

然后只需调用adapter.notifyDataSetChanged()

yourAdapter.notifyDataSetChanged();

它会起作用。