Slide show

Total Pageviews

Powered by Blogger.

Translate

Search This Blog

Pages

ADD ITEM ON LIST VIEW IN ANDROID

JAVA CODE OF DYNAMIC LIST VIEW IN ANDROID:-


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;


public class MainActivity extends ListActivity
{
    private static final int ADD_ITEM = 0;
    private static final int REMOVE_ITEM = 1;
    private static final int EXIT_ITEM = 2;

    private ArrayAdapter<String> dataAdapter;
    private Dialog editorDialog = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        dataAdapter = new ArrayAdapter<String>(this, R.layout.item,
            R.id.itemName);
        dataAdapter.add("apple");
        dataAdapter.add("orange");
        dataAdapter.add("tomato");

        setListAdapter(dataAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        Resources resource = getApplicationContext().getResources();
        menu.add(Menu.NONE, ADD_ITEM, ADD_ITEM,
            resource.getText(R.string.ADD_ITEM)).setIcon(R.drawable.add);
        menu.add(Menu.NONE, REMOVE_ITEM, REMOVE_ITEM,
            resource.getText(R.string.REMOVE_ITEM)).setIcon(R.drawable.remove);
        menu.add(Menu.NONE, EXIT_ITEM, EXIT_ITEM,
            resource.getText(R.string.EXIT_ITEM)).setIcon(R.drawable.exit);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case ADD_ITEM:
            showDialog(0);
            break;
        case REMOVE_ITEM:
            dataAdapter.remove(dataAdapter.getItem(dataAdapter.getCount() - 1));
            break;
        case EXIT_ITEM:
            finish();
        }
        return false;
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        Dialog editor = editorDialog;
        if (editorDialog == null)
        {
            editor = createEditorDialog();
        }
        return editor;
    }

    private Dialog createEditorDialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.addDialogTitle);

        View content = getLayoutInflater().inflate(R.layout.editor,
            (ViewGroup) findViewById(R.id.editLayout));
        builder.setView(content);
        builder.setPositiveButton(R.string.addButtonLabel,
            new DialogInterface.OnClickListener()
            {

                public void onClick(DialogInterface dialog, int which)
                {
                    Dialog source = (Dialog) dialog;
                    EditText nameField = (EditText) source
                        .findViewById(R.id.itemField);
                    String name = nameField.getText().toString();

                    EditText timesField = (EditText) source
                        .findViewById(R.id.timesField);
                    Integer times = Integer.valueOf(timesField.getText()
                        .toString());

                    if ((name.length() > 0) && (times > 0))
                    {
                        for (int count = 0; count < times; count++)
                        {
                            dataAdapter.add(name);
                        }
                    }
                    dialog.dismiss();
                }
            });

        builder.setNegativeButton(R.string.cancelButtonLabel,
            new DialogInterface.OnClickListener()
            {

                public void onClick(DialogInterface dialog, int which)
                {
                    dialog.dismiss();
                }
            });

        return builder.create();
    }
}


XML CODE :-

CODE OF EDITOR.XML  :- 

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

    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:fitsSystemWindows="true"
        android:orientation="horizontal" android:padding="5dp" gravity="center_horizontal">

        <TableRow android:layout_width="wrap_content"
            android:layout_height="fill_parent" android:gravity="center_horizontal">

            <TextView android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/nameLabel"
                android:text="@string/nameLabel" android:gravity="right|center_vertical"
                android:layout_marginRight="5dp" />

            <EditText android:layout_height="fill_parent" android:id="@+id/itemField"
                android:layout_width="fill_parent" android:width="200dp" />
        </TableRow>
    </TableLayout>

    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:orientation="horizontal"
        android:gravity="center_horizontal">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/timesLabel"
            android:text="@string/timesLabel" android:gravity="right|center_vertical"
            android:layout_marginRight="5dp" />

        <EditText android:text="1" android:id="@+id/timesField" 
            android:width="100dp" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:digits="0123456789" />
    </LinearLayout>
</LinearLayout>




CODE OF ITEM.XML :-

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:paddingLeft="5dp"
android:paddingRight="5dp">

<TextView android:id="@+id/itemName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Test view" />
</LinearLayout>


CODE OF STRING XML:-

<?xml version="1.0" encoding="utf-8"?>
<resources>

 
    <string name="app_name">Array</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    
    
     <string name="applicationName">String List</string>

    <string name="priceFormat">{0,number, currency}</string>

    <!-- Options menu -->
    <string name="ADD_ITEM">Add to End</string>
    <string name="REMOVE_ITEM">Remove Last</string>
    <string name="EXIT_ITEM">Exit</string>

    <!-- Parameters for the "Add Item" dialog -->
    <string name="addDialogTitle">Add Item</string>
    <string name="nameLabel">Item </string>
    <string name="priceLabel">Price </string>
    <string name="timesLabel">Times</string>

    <!-- Labels for the button -->
    <string name="addButtonLabel">Add</string>
    <string name="cancelButtonLabel">Cancel</string>
    
    

</resources>


MANIFEST FILE CODE :-


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.array"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.array.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


RUN & ENJOY THE APPLICATION .


THANKS.









No comments:

vehicles

business

health