Android listview add items

In your app, you might want to allow user to add more items to a ListView. In this Android tip, I am going to show you how to achieve the goal. There are two important things to note when you add new items to the ListView. The first thing is that you have to use an ArrayList to store the items of the ListView. It is easy to add more items to the ArrayList. The second thing is that you have to call the notifyDataChanged on the adapter object after you added a new item to the ArrayList. This is to notify the change of items and the ListView refresh itself.

Now, in the activity_main.xml file, you add an EditText, a Button, and a ListView. The EditText allows you to input new item. The Button is clicked to add the new item to the ListView.

<LinearLayout 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"
   android:orientation="vertical"
   android:padding="10dp"
   tools:context=".MainActivity"
  >
   <RelativeLayout      android:layout_width="match_parent"      android:layout_height="wrap_content">      <EditText         android:id="@+id/txtInput"         android:layout_width="200dp"         android:layout_height="wrap_content"         android:hint="New item"         />      <Button         android:id="@+id/btAdd"         android:layout_width="wrap_content"         android:layout_height="wrap_content"
        android:text="Add"         android:layout_toRightOf="@+id/txtInput"         />
     </RelativeLayout>    <ListView    android:id="@+id/list"    android:layout_width="match_parent"    android:layout_height="wrap_content">   </ListView>
</LinearLayout>


In the res/layout directory, you create a layout file for the ListView. The name of the file is list_item.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">
   <TextView
     android:id="@+id/txtview"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />
</LinearLayout>

In the MainActivity class that hosts the ListView, you write the following code.

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends ActionBarActivity {
   ArrayAdapter<String> adapter;
   EditText editText;
  ArrayList<String> itemList;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     String[] items={"Apple","Banana","Clementine"};
     itemList=new ArrayList<String>(Arrays.asList(items));
     adapter=new ArrayAdapter<String>(this,R.layout.list_layout,R.id.txtview,itemList);
     ListView listV=(ListView)findViewById(R.id.list);
     listV.setAdapter(adapter);
     editText=(EditText)findViewById(R.id.txtInput);
     Button btAdd=(Button)findViewById(R.id.btAdd);
     btAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String newItem=editText.getText().toString();
           // add new item to arraylist
           itemList.add(newItem);
           // notify listview of data changed
           adapter.notifyDataSetChanged();
        }
     });
   }
}


listview add items

Posted by: Dara | post date: 05-12-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.