1

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;
    }
}
}
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
  • I should take a look at this : http://stackoverflow.com/questions/2383847/android-layout-with-listview-and-buttons – user1264255 Apr 24 '12 at 07:47

2 Answers2

4

The ArrayAdapter might be the point to start. Of course it would change according to your list, but I suggest you to use ArrayAdapter. Get the reference of your list, define a new adapter for it and set it with setAdapter method. Then, you can add/remove items to your adapter.

Siyamed
  • 495
  • 2
  • 11
  • Could you please provide some code? I haven't used adapters till now – Rahul Baradia Apr 24 '12 at 08:38
  • 1
    Thanks alot. It works now. I changed the listview id and then added this code in activity file. In onCreate() : list = (ListView)findViewById(R.id.myListView); adapter = new ArrayAdapter(MyList.this, R.layout.my_list_row, R.id.itemTextView); list.setAdapter(adapter); And after getting the user input : adapter.add(item); adapter.notifyDataSetChanged(); – Rahul Baradia Apr 24 '12 at 09:12
3

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

sique
  • 241
  • 1
  • 2
  • 12