Wednesday 16 March 2011

ListView Data Select And Delete

New Android Project Select

Project Name : ListviewSample

Build Target : Android 2.2

Application Name :  ListviewSample

Package Name :  com.ovte.listviewsample

Create Activity : ListviewSample

Min SDK Version : 8

Open AndroidManifest.xml File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ovte.listviewsample"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="ListviewSample"
                  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>
    <uses-sdk android:minSdkVersion="3" />
</manifest> 


Open ListviewSample.java File

package com.ovte.listviewsample;

import java.util.ArrayList;
import java.util.Arrays;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ListviewSample extends ListActivity {

    /** We want to display these names as our list items */
    String[] names = {
 "Piyush","Ankit","Dipak","Hitesh","Renuka","Dharmesh","Mitesh","Mihir","Lalit","Kiran","Rohit","Krunal"
    };
   
    private ArrayAdapter<String> adapter;        // The list adapter
    private String selectedItem;                // Stores the selected list item
    private final Context context = this;        // This context
   
   
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
       
        // Create the listener for normal item clicks
        OnItemClickListener itemListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long rowid) {

                Toast.makeText(
                        getApplicationContext(),
                        "You have clicked on " + parent.getItemAtPosition(position).toString() + ".",
                        Toast.LENGTH_SHORT).show();
            }
        };
       
        // Create the listener for long item clicks
        OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long rowid) {
               
                // Store selected item in global variable
                selectedItem = parent.getItemAtPosition(position).toString();
       
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Do you want to remove " + selectedItem + "?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       
                        adapter.remove(selectedItem);
                        adapter.notifyDataSetChanged();
               
                        Toast.makeText(
                                getApplicationContext(),
                                selectedItem + " has been removed.",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                // Create and show the dialog
                builder.show();
               
                // Signal OK to avoid further processing of the long click
                return true;
            }
        };
       
        // Convert array to ArrayList
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
       
        // Create new ArrayAdapter with the ArrayList as underlying data source
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
       
        // Assign adapter to the list
        setListAdapter(adapter);
       
        // Assign both click listeners
        getListView().setOnItemClickListener(itemListener);
        getListView().setOnItemLongClickListener(itemLongListener);
    }
}

Output 

  Application Run Then Default Display This Type
 

    Click On ListView Then Selected Value Display


 
Long Press on Value When Delete Dialog Display


36 comments:

  1. thanks for your time and give us such a nice example.

    ReplyDelete
  2. This is very helpful example for all android developer.
    Thanks for it.

    ReplyDelete
  3. Thanks a lot for this guide!!!

    ReplyDelete
  4. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
    Click here:
    angularjs training in velarchery
    Click here:
    angularjs training in sholinganallur

    ReplyDelete
  5. Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
    Click here:
    Microsoft azure training in sollinganallur

    ReplyDelete
  6. Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.

    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    ReplyDelete
  7. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    Devops training in velachery
    Devops training in annanagar

    ReplyDelete
  8. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python training in tambaram
    python training in annanagar
    python training in OMR
    python training in chennai

    ReplyDelete
  9. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    angularjs Training

    in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  10. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  11. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  12. I have picked cheery a lot of useful clothes outdated of this amazing blog. I’d love to return greater than and over again. Thanks! 
    java training in chennai | java training in bangalore

    java interview questions and answers | core java interview questions and answers

    ReplyDelete
  13. Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate

    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

    ReplyDelete
  14. Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!
    python interview questions and answers | python tutorialspython training institute in electronic city

    ReplyDelete
  15. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    angularjs Training in bangalore

    angularjs Training in bangalore

    ReplyDelete
  16. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  17. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  18. Excellent post, it will be definitely helpful for many people. Keep posting more like this.

    DevOps Training in Chennai
    Splunk Training in Chennai

    ReplyDelete
  19. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website.thanks a lot.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  20. Want to learn Oracle DBA along with the job opportunities? Infycle are with you to make your dream into reality. Infycle Technologies gives the most trustworthy Oracle DBA Training in Chennai, in 100% hands-on practical training with professional tutors in the field. Along with that, the mock interviews will be assigned for the candidates, so that, they can meet the job interviews with full confidence. To transform your career to next level, call 7502633633 to Infycle Technologies and grab a free demo to know more

    ReplyDelete
  21. Did you want to set your career towards Big Data? Then Infycle is with you to make this into reality. Infycle Technologies gives the combined and best Big Data Hadoop Training in Chennai, in 100% hands-on training guided by specialized trainers in the field. In addition to this, the mock interviews will be given to the candidates, so that they can face the interviews with complete confidence. Apart from all, the candidates will be placed in the top MNC's with a great salary package. To get it all, call 7502633633 and make this happen for your happy life.

    https://infycletechnologies.com/

    ReplyDelete
  22. Fetch Oracle DBA Training in Chennai for making the best career in the software industry with Infycle Technologies. Infycle Technologies offers the best Oracle training in Chennai, providing courses for Oracle and many other software courses in 100% hands-on practical training with professional trainers in the domain. Along with the coaching, the placement interviews will be arranged for the students, so that they can set their careers at high standards. Of all that, 200% placement assurance will be given here. To have the best career, call 7502633633 to Infycle Technologies and grab a free demo to know more.

    ReplyDelete
  23. Learn Amazon Web Services for making your career as a shining sun with Infycle Technologies. Infycle Technologies is the best AWS training center in Chennai, providing complete hands-on practical training of professional specialists in the field. In addition to that, it also offers numerous programming language tutors in the software industry such as Oracle, Java, Python, AWS, Hadoop, etc. Once after the training, interviews will be arranged for the candidates, so that, they can set their career without any struggle. Of all that, 200% placement assurance will be given here. To have the best career, call 7502633633 to Infycle Technologies and grab a free demo to know more.
    Best AWS training in chennai

    ReplyDelete
  24. Grab the Digital Marketing Training in Chennai from Infycle Technologies, the best software training institute, and Placement center in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Hadoop, Selenium, Android, and iOS Development, DevOps, Oracle etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.

    ReplyDelete
  25. If you are dreaming of an IT job !!! Then AWS Course in Chennai!!Is the best choice for you. Yes, what you heard is Right Infycle offering you an AWS course for an Affordable price with experienced trainees, Practical Classes, Flexible timing, and more.

    ReplyDelete
  26. Oxford Migration for digital marketing
    check
    Reply

    ReplyDelete