Android 如何使用适配器在按钮单击时添加 ListView 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22144891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add ListView items on button click using adapter
提问by Nevaeh
How to take data that was typed in EditText and by clicking "submit" in that window should add it to previous activity listview items? What I need to do is:
如何获取在 EditText 中输入的数据并通过单击该窗口中的“提交”将其添加到以前的活动列表视图项中?我需要做的是:
- Creating EditText and submit button
- Creating listview in same Activity
- By clicking submit button it should display it in listview.
- 创建 EditText 和提交按钮
- 在同一个活动中创建列表视图
- 通过单击提交按钮,它应该在列表视图中显示它。
I saw this similar question here:add items to listview dynamically android
我在这里看到了这个类似的问题:add items to listview dynamic android
But i couldn't understand the answer.Somebody please explain how to do this.
但我无法理解答案。请有人解释如何做到这一点。
回答by Shahinoor Shahin
You just do the following : Prepare your xml like this :
您只需执行以下操作: 像这样准备您的 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>
Activity looks like following :
活动如下所示:
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();
}
});
}
}
回答by M D
Create String Arraylist
and initialize it
创建String Arraylist
并初始化它
ArrayList<String> str1 = new ArrayList<String>();
Add some values on it
在其上添加一些值
str1.add("First Row");
str1.add("Second Row");
str1.add("Third Row");
str1.add("Fourth Row");
str1.add("Fifth Row");
Then set Adapter
to ListView
然后设置Adapter
为ListView
adapter=new ListAdapter(this,str1);
list.setAdapter(adapter);
then add your EditText
text into str1 and then called adapter.notifyDataSetChanged();
like
然后添加EditText
文字到str1和当时称为adapter.notifyDataSetChanged();
像
str1.add(edit_message.getText().toString());
adapter.notifyDataSetChanged();
Try to add this code into Button onclick()
尝试将此代码添加到 Button onclick()
Demo Output:
演示输出:
回答by Thunder
Suppose you have an arraylist
假设你有一个数组列表
ArrayList<String> dataList = new Arraylist ();
So On clicking of button, you need to add the new data item into your data arraylist.
所以 On clicking of button, you need to add the new data item into your data arraylist.
For this first take the value entered in edittext and store in a string.
为此,首先获取在 edittext 中输入的值并存储在字符串中。
String editedValue = yourEditText.getText.toString();
Then we need to add this in our datalist.
然后我们需要将它添加到我们的数据列表中。
Like
喜欢
dataList.add(editedValue);
And then just call adapter.notifyDataSetChanged()
然后只需调用 adapter.notifyDataSetChanged()
yourAdapter.notifyDataSetChanged();
It will work.
它会起作用。