Android listview display data click long press

Source: Internet
Author: User

Reference: http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

1. The simplest listview-arrayadapter

1. arrayadapter (context, int textviewresourceid, list <t> objects)

Textviewresourceid: Layout file, which can be a built-in layout file;

Objects: generic object.

2. Supplement: arraylist is a dynamic array,

Example:

Arraylist list = new arraylist ();

For (INT I = 0; I <10; I ++) List. Add (I );

List. removeat (5 );

3. Supplement: Why should I use list = new arraylist (); Do not use arraylist list = arraylist ();

The problem is that list has multiple implementation classes. Now you are using arraylist. Maybe you need to change to another implementation class on that day, such as struct list or vector, now you only need to change this line:
List list = new external list (); Other code that uses the list does not need to be modified.
Suppose you start to use arraylist alist = new arraylist (), and you have modified it, especially if you use the methods and attributes unique to arraylist.

Http://www.cnblogs.com/aisiteru/articles/1151874.html

4. Supplement: Generic, similar to the C ++ template, such as List & lt; string>

5. Supplement: setcontentview () in the activity class. In Android development, you can use setcontentview () to redirect to the desired layout file.

6. Supplement,

Horizontal line in the code During eclipse Encoding

When I used eclipse encoding today, I found that some variables or class names were inexplicably broken, as shown in:

Now I know what these dashes mean:The classes or methods marked by the dashes are in the old version and are not recommended for the current version.

2. It is generally used in the simplecursoradapter database.

Public simplecursoradapter (context, int layout, cursor C, string [] From, int [])

The following two parameters are a string array containing the database columns and an int array containing the corresponding component ID in the layout file. The function is to automatically map each column of data represented by the string array to the component with the corresponding ID of the layout file.

/** Refresh the listview */
Private void refreshlv ()
{
Databasehelper dbhelper = new databasehelper (mainactivity. This, null, null, 1); // create a database
Dbhelper. getreadabledatabase ();
Mycursor = dbhelper. getcursor ();
Simplecursoradapter myadapter = new simplecursoradapter
(
Mainactivity. This,
R. layout. main_vlist,
Mycursor,
New String [] {databasehelper. field_id, databasehelper. field_event_name, databasehelper. field_remind_time },
New int [] {R. id. eventidtv, R. id. eventnametv, R. id. remindtimetv} // The control represented by this parameter is defined in the layout file represented by the second parameter.
);
Displaylv. setadapter (myadapter );
Dbhelper. Close ();
}
/** Refresh the listview */

 

3. Click an item in the listview

Mylistview. setonitemclicklistener (New onitemclicklistener (){
@ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2,
Long arg3 ){
// Todo auto-generated method stub
Mycursor. movetoposition (arg2 );
_ Id = mycursor. getint (0 );
Myedittext. settext (mycursor. getstring (1 ));
}
});

4. Long press (long press to bring up alertdialog)

Private listview displaylv;

Displaylv. setonitemlongclicklistener (New onitemlongclicklistener ()
{
Public Boolean onitemlongclick (adapterview <?> Arg0, view arg1, final int arg2, long arg3) // The _ ID in the table corresponding to the item clicked by arg2, And the row number clicked by arg3
{
// Todo auto-generated method stub
Databasehelper dbhelper = new databasehelper (mainactivity. This, null, null, 1); // create a database
Dbhelper. getreadabledatabase ();
Mycursor = dbhelper. getcursor ();
Mycursor. movetoposition (arg2); // move to the arg2 row of the database
Final int deleteid = mycursor. getint (0); // ID of row 0th in Column 2nd
Dbhelper. Close ();
New alertdialog. Builder (mainactivity. This)
. Settitle ("Delete Alert ")
. Setmessage
(
"Confirm to delete" +
"<" +
Mycursor. getstring (mycursor. getcolumnindex (databasehelper. field_event_name) +
">? "
)
. Setpositivebutton
(
"OK ",
New dialoginterface. onclicklistener ()
{
Public void onclick (dialoginterface dialog, int which)
{
// Combined with mycursor. movetoposition (arg2), mycursor. getint (0) indicates to get the data corresponding to column 0th of arg2 (this data is returned in integer type, actually _ id)
Databasehelper dbhelper = new databasehelper (mainactivity. This, null, null, 1); // create a database
Dbhelper. Delete (deleteid );
Dbhelper. Close ();
Refreshlv ();
}
}
)
. Setnegativebutton
(
"Cancel ",
New dialoginterface. onclicklistener ()
{
Public void onclick (dialoginterface dialog, int which ){}
}
)
. Show ();
Return true;
}
});
/** Long-pressed displaylv (listview) response */

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.