Search code examples
androidandroid-listviewadapterlistadapter

Android : How to add an item to a listview onclick of a button?


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 ?

Here is my layout file : my_list.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

<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" >
</ListView>

<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" />


</LinearLayout>

Here is my row_item.xml

 <?xml version="1.0" encoding="utf-8"?>
 <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" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:gravity="center"
    android:orientation="horizontal" >

    <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" />

    <TextView android:layout_weight = "1" 
        android:id="@+id/itemTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>

Here is my java file :

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;
    }
}
}

Solution

  • where is your listview?

    when you add your list item in your list(Array or Arraylist or...)

     your_adapter.notifyDataSetChanged();
    

    it makes draw your fresh listview