Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid : How to add an item to a listview onclick of a button?
    text
    copied!<p>I have a listview and a button in my layout file. I want to add items to listview on click of that button. The listview should be empty when the activity is started but it should grow by adding the items to it. On click of "AddItem" button i'am showing one AlertDialog to take item/input from user. How do i add that item to listview? Please help me with code. Which adapter should i use to use my own layout inside ListView? Should i extend ListActivity ? </p> <p>Here is my layout file : my_list.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/blue" android:orientation="vertical" &gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:divider="@android:color/black" android:dividerHeight="2dp" &gt; &lt;/ListView&gt; &lt;Button android:id="@+id/addItemButtom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_weight="1" android:text="Add Item" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Here is my row_item.xml</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center" android:orientation="horizontal" &gt; &lt;TextView android:layout_weight = "1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Newly Added Item : " android:textColor="@android:color/black" android:textSize="17dp" /&gt; &lt;TextView android:layout_weight = "1" android:id="@+id/itemTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>Here is my java file :</p> <pre><code>public class MyList extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_list); findViewById(R.id.addItemButtom).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.enterInverter: AlertDialog.Builder alert = new AlertDialog.Builder(this); final EditText et = new EditText(this); et.setHint("Enter Item"); et.setMaxLines(1); et.setTextSize(17); alert.setTitle("Enter Item"); alert.setView(et); alert.setInverseBackgroundForced(true); alert.setPositiveButton("Add", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String item = et.getText().toString().trim(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }); alert.show(); break; default: break; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload