繁体   English   中英

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

[英]How to add ListView items on button click using adapter

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

  1. 创建EditText并提交按钮
  2. 在同一个Activity中创建listview
  3. 通过单击提交按钮,它应该在列表视图中显示。

我在这里看到了类似的问题: 向listview动态添加项目android

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

您只需执行以下操作:准备您的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();
            }
        });
    }
}

创建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()

演示输出:

在此输入图像描述

假设你有一个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();

它会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM