[英]How to add ListView items on button click using adapter
How to take data that was typed in EditText and by clicking "submit" in that window should add it to previous activity listview items? 如何获取在EditText中输入的数据并在该窗口中单击“提交”应该将其添加到以前的活动列表视图项? What I need to do is:
我需要做的是:
I saw this similar question here: add items to listview dynamically android 我在这里看到了类似的问题: 向listview动态添加项目android
But i couldn't understand the answer.Somebody please explain how to do this. 但我无法理解答案。有人请解释如何做到这一点。
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();
}
});
}
}
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();
然后将您的
EditText
文本添加到str1中,然后调用adapter.notifyDataSetChanged();
like 喜欢
str1.add(edit_message.getText().toString());
adapter.notifyDataSetChanged();
Try to add this code into Button onclick()
尝试将此代码添加到
Button onclick()
Demo Output: 演示输出:
Suppose you have an arraylist 假设你有一个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. 然后我们需要在我们的datalist中添加它。
Like 喜欢
dataList.add(editedValue);
And then just call adapter.notifyDataSetChanged() 然后只需调用adapter.notifyDataSetChanged()
yourAdapter.notifyDataSetChanged();
It will work. 它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.