How to add ListView items on button click using adapter

两盒软妹~` 提交于 2019-12-09 01:16:41

问题


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:

  1. Creating EditText and submit button
  2. Creating listview in same Activity
  3. By clicking submit button it should display it in listview.

I saw this similar question here:add items to listview dynamically android

But i couldn't understand the answer.Somebody please explain how to do this.


回答1:


You just do the following : Prepare your xml like this :

<?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();
            }
        });
    }
}



回答2:


Create String Arraylist and initialize it

    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=new ListAdapter(this,str1);
list.setAdapter(adapter);

then add your EditText text into str1 and then called adapter.notifyDataSetChanged(); like

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

Try to add this code into Button onclick()

Demo Output:




回答3:


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.

For this first take the value entered in edittext and store in a string.

String editedValue = yourEditText.getText.toString();

Then we need to add this in our datalist.

Like

dataList.add(editedValue);

And then just call adapter.notifyDataSetChanged()

yourAdapter.notifyDataSetChanged();

It will work.



来源:https://stackoverflow.com/questions/22144891/how-to-add-listview-items-on-button-click-using-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!