Holiday group assistant for Android basic Integration Project (2), android Group

Source: Internet
Author: User

Holiday group assistant for Android basic Integration Project (2), android Group

Basic Android Integration Project (1)Holiday group assistant part 2

-- Reprinted with the source: coder-pig


This section introduces:

In the previous section, we have made the first interface of our group assistant and completed reading contacts and database

Input, the work to be completed in this section is:

1) customize the list items of our ListView, two textviews + CheckBox;

2) use SimpleCursorAdapter to display data to ListView;

3) select all or none of listview

4) Click the reset button to reset the state in the database to-1.

Now, let's start with the content in this section!


Body: 1. Create the layout of the second Activity:


The layout code is as follows:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/RelativeLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. festivalsmshelper. mainActivity "> <TextView android: id =" @ + id/TextView1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: text = "select the person to send"/> <ListView android: id = "@ + id/listcontacts" android: layout_width = "match_parent" android: layout_height = "match_parent" android: layout_alignParentLeft = "true" android: layout_below = "@ + id/TextView1" android: layout_abve = "@ + id/buttomlayout" android: layout_marginTop = "10dp"> </ListView> <LinearLayout android: id = "@ + id/buttomlayout" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: authorization = "true" android: layout_alignParentLeft = "true" android: orientation = "horizontal"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/btnall" android: text = "select all"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/btnreset" android: text = "reset"/> <Button android: id = "@ + id/btnnext" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <TextView android: layout_marginLeft = "20dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "selected" android: textSize = "15sp"/> <TextView android: layout_marginLeft = "20dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/textshow" android: textSize = "15sp"/> </LinearLayout> </RelativeLayout>


2. Create a layout file for the list items of Listview:

The layout is divided into three levels, and two textviews are hidden.

The Code is as follows:

<?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:orientation="horizontal"    android:gravity="center_horizontal"     >        <TextView        android:gravity="center"     android:id="@+id/listid"    android:layout_width="0dp"     android:layout_weight="1"    android:layout_height="match_parent"    android:visibility="gone"    />        <TextView        android:gravity="center"     android:id="@+id/listname"    android:layout_width="0dp"     android:layout_weight="1"    android:layout_height="match_parent"    />    <TextView        android:gravity="center"     android:id="@+id/listphone"    android:layout_width="0dp"     android:layout_weight="1"    android:layout_height="match_parent"    />        <TextView     android:id="@+id/listhide"    android:layout_width="wrap_content"     android:layout_height="match_parent"    android:visibility="gone"        />            <CheckBox        android:focusable="false"         android:clickable="false"     android:id="@+id/listchoice"    android:layout_width="0dp"     android:layout_weight="1"    android:layout_height="match_parent"      />    </LinearLayout>



3. Use SimpleCursorAdapter to bind a database with ListView:

The Code is as follows:

Private void getContacts () {GetContactsService gs = new GetContactsService (ChooseActivity. this); Cursor cursor = gs. query ("select _ id, pname, pnumber, pstate from contacts", null); // Note: If SimpleCursorAdapter is used, this _ id is indispensable! Otherwise, an error is always reported. // you need to create a hidden component to put _ idsimpleCursorAdapter = new SimpleCursorAdapter (ChooseActivity. this, R. layout. listitem, cursor, new String [] {"_ id", "pname", "pnumber", "pstate"}, new int [] {R. id. listid, R. id. listname, R. id. listphone, R. id. listhide}); list. setAdapter (simpleCursorAdapter );}






Note !!!! When SimpleCursorAdapter is used, the bound database table must have the _ id field or as _ id;

The _ id item must be included in the data retrieved during binding. Otherwise, the following error will be reported!

Java. lang.IllegalArgumentException:Column '_ id' does not exist


4. Call this method in OnCreate:

Call getContacts () of 3 in Activity, and we can see that our database data has been displayed on ListView:




5. Click the operations performed by the listview list item:

① Set the setOnItemClickListener Method for listView, that is, click event

② What do I need to leave after clicking? Changes the checkbox selection status and counts the number of selected items, and then displays them.

First, define a number for counting the currently selected check boxes:

private int setShow(){int num = 0;for(int i = 0;i < list.getChildCount();i++){LinearLayout layout = (LinearLayout) list.getChildAt(i);CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);if(cbx.isChecked())num++;}return num;}


Then select the check box after clicking and change the number of selected items:

List. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {CheckBox cb = (CheckBox) view. findViewById (R. id. listchoice); TextView itemhide = (TextView) view. findViewById (R. id. listhide); TextView itemid = (TextView) view. findViewById (R. id. listid); int state = Integer. parseInt (itemhide. getText (). toString (); // use this variable to identify whether the button selected state * =-1; itemhide. setText (state + ""); if (state = 1) cb. setChecked (true); else cb. setChecked (false); textshow. setText (setShow () + "item ");}});



After the above Code is completed, the following results are displayed:




6. Implementation of the Select All function:

Select All list items and then display the number!

Btnall. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {allflag * =-1; if (allflag =-1) {for (int I = 0; I <list. getChildCount (); I ++) {LinearLayout layout = (LinearLayout) list. getChildAt (I); CheckBox cbx = (CheckBox) layout. findViewById (R. id. listchoice); cbx. setChecked (true); btnall. setText ("NONE"); textshow. setText (setShow () + "item") ;}} else if (allflag = 1) {for (int I = 0; I <list. getChildCount (); I ++) {LinearLayout layout = (LinearLayout) list. getChildAt (I); CheckBox cbx = (CheckBox) layout. findViewById (R. id. listchoice); cbx. setChecked (false); btnall. setText ("select all"); textshow. setText (setShow () + "item ");}}}});

The effects of the above Code:





7. Click the next button to overwrite the trigger event:

The work to be completed in this button event:

① Traverse the listview and retrieve the selected contact id from the checkbox. Store the corresponding contact id in the set!

② Store the set in Intent, it. putIntegerArrayListExtra ("ids", checkedId );


The Code is as follows:

Btnnext. setOnClickListener (new OnClickListener () {public void onClick (View v) {ArrayList <Integer> checkedId = new ArrayList <Integer> (); for (int I = 0; I <list. getChildCount (); I ++) {LinearLayout layout = (LinearLayout) list. getChildAt (I); CheckBox cbx = (CheckBox) layout. findViewById (R. id. listchoice); TextView txtid = (TextView) layout. findViewById (R. id. listid); if (cbx. isChecked () checkedId. add (Integer. parseInt (txtid. getText (). toString ();} // jump to the third page, and store the data to the intent object Intent it = new Intent (ChooseActivity. this, ThridActivity. class); it. putIntegerArrayListExtra ("ids", checkedId); startActivity (it );}});
Well, the second interface is ready! This section also ends!



Knowledge Point summary:

Now, let's summarize the knowledge points used in this section:

1) use the weight attribute in LinearLayout to divide the horizontal or vertical direction into multiple copies!

2) use visibility to hide components:Visible (visible), invisible (invisible but still occupying space), and gone (invisible or not occupying space)

3) SimpleCursorAdapter binds the database to the ListViewNote that the field _ id or the field as _ id must be included;

4) how to count the number of columns in the selected status in the listview check box and traverse the listview!

5) Use putIntegerArrayListExtra of intent to store data of the ArrayList <T> set type and transfer it to another Activity!










How can I crack the limit on sending SMS messages to Android systems? I want to send over 100 text messages to my group on a holiday, so I have to order to continue to be speechless on my cell phone.

Provides SMS sending platform, MMS sending platform software, and wap platform software. 3g integrated messaging platform (SMS message wap)
Provides SMS sending platform, MMS sending platform software, and wap platform software. 3g integrated messaging platform (SMS message wap)
 
Powerful chat software with group-sent SMS

[Dianxun sms] professional sms platform, one-to-one personalized sms, ultra-long sms, free handling, vip service.
 

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.