Sunday 4 August 2013

Delete Row Item in ListView Android

2 comments
Today I will display how to delete row item from List in android. In this project I will be having list, on long press of item  an alert dialog box will pop out asking you for confirmation with having Yes and Cancel Button.




Step 1:- create activity_main.xml
<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" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>



Step 2:- create list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

Step 3:- MainActivity.java
package com.arpit.deleterowlistview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
    ListView lv;
    ArrayAdapter<String> adapter;
    List<String> arr;
    String[] language = { "C", "Java", "C++", "C-sharp", ".Net", "SQL",
            "Android", "PhoneGap", "Ios", "windows", "PHP", "Phyton", "Perl" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        arr = new ArrayList<String>(Arrays.asList(language));
        adapter = new ArrayAdapter<String>(this, R.layout.list_row, arr);
        lv.setAdapter(adapter);
       
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            // setting onItemLongClickListener and passing the position to the function
                      @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                removeItemFromList(position);  
               
                return true;
            }
        });
    }
// method to remove list item
    protected void removeItemFromList(int position) {
        final int deletePosition = position;
       
        AlertDialog.Builder alert = new AlertDialog.Builder(
                MainActivity.this);
   
        alert.setTitle("Delete");
        alert.setMessage("Do you want delete this item?");
        alert.setPositiveButton("YES", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TOD O Auto-generated method stub
                   
                    // main code on after clicking yes
                    arr.remove(deletePosition);
                    adapter.notifyDataSetChanged();
                    adapter.notifyDataSetInvalidated();
     
            }
        });
        alert.setNegativeButton("CANCEL", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
     
        alert.show();
     
    }
}


Catch Me On: CreateAppFaster.com

2 comments:

  1. in this code delete last item but what for selected position delete ?

    ReplyDelete
  2. Did you realize there's a 12 word phrase you can tell your crush... that will trigger intense feelings of love and instinctual appeal for you buried within his heart?

    That's because hidden in these 12 words is a "secret signal" that fuels a man's instinct to love, treasure and look after you with his entire heart...

    12 Words Who Trigger A Man's Love Instinct

    This instinct is so built-in to a man's brain that it will make him try harder than ever before to make your relationship as strong as it can be.

    Matter of fact, fueling this dominant instinct is so mandatory to achieving the best possible relationship with your man that the instance you send your man one of these "Secret Signals"...

    ...You'll immediately notice him expose his soul and mind for you in such a way he's never expressed before and he will recognize you as the only woman in the world who has ever truly appealed to him.

    ReplyDelete