Android ListView Usage

Source: Internet
Author: User
Tags shuffle

Write a simple example to illustrate the use of the ListView: Given a word, there are four meanings below, to find the correct one, whether or not successful, will jump to the next word;
The main uses of knowledge are: Findviewbyid (), ListView, Adapterview, Anonymous inner class, ArrayList some usage:

The following main look at the code, the code inside the comment is very detailed, will not be too much to repeat:
Xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "vertical">7 8     <TextView9         Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:id= "@+id/wordtextview" A Android:text= "word" - android:textsize= "26DP" - android:layout_gravity= "Center"/> the     <ListView -         Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" - Android:id= "@+id/definitions_listview"> +  -     </ListView> +  A </LinearLayout>

Java files:

Setonitemclicklistener This is the monitor method of the ListView;
1  PackageCom.chenye.dictionarychange;2 3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.widget.AdapterView;7 ImportAndroid.widget.ArrayAdapter;8 ImportAndroid.widget.ListView;9 ImportAndroid.widget.TextView;Ten ImportAndroid.widget.Toast; One  A ImportOrg.w3c.dom.Text; -  - Importjava.util.ArrayList; the Importjava.util.Collection; - Importjava.util.Collections; - ImportJava.util.HashMap; - ImportJava.util.Scanner; +  -  Public classMainactivityextendsappcompatactivity { +  A     PrivateHashmap<string, String> dictionary;//A dictionary that stores words-meaning of words at     PrivateArraylist<string> chosenwords;//Store all the words in Chosenwords -     PrivateString Word;//Word -     PrivateArraylist<string> definations;//meaning list -     PrivateArrayadapter<string>adapter; -  - @Override in     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); to Setcontentview (r.layout.activity_main); + readalldefination (); -          This. Chosenwords =NewArraylist<> ( This. Dictionary.keyset ());//get all the words and exist in ArrayList the          This. definations =NewArraylist<>(); *          This. adapter =NewArrayadapter<> ( ThisAndroid. R.layout.simple_list_item_1, This. definations);//Initialize Arrayadapter $         //randomly select 5 words meaningPanax Notoginseng pick4definations (); -         //Add a list to adapter theListView Defnlistview =Findviewbyid (r.id.definitions_listview); +Defnlistview.setadapter ( This. adapter); A         //listen to this list and use the anonymous inner class the        Defnlistview.setonitemclicklistener (New Adapterview.onitemclicklistener () { + @Override -              Public voidOnitemclick (adapterview<?> adapterview, view view,intILongl) { $ListView Defnlistview =Findviewbyid (r.id.definitions_listview); $String Choosedefntext = defnlistview.getitematposition (i). toString ();//get the click position of the string, I is the position of the list, in most cases I and L (List of the first few lines) is the same -String correctdefn = mainactivity. This. Dictionary.get (Word);//get an explanation of the corresponding word -                 //If the selection is consistent with the correct result: the                 if(Correctdefn.equals (Choosedefntext)) { -Toast.maketext (mainactivity. This, "Correct", Toast.length_short). Show ();Wuyi}Else{ theToast.maketext (mainactivity. This, "wrong", Toast.length_short). Show (); -                 } Wu                 //after selecting the list again after re-refreshing, that is to pick a word again and the meaning of 5 words - pick4definations (); About  $             } -         }); -     } -      Public voidpick4definations () { A  +Collections.shuffle ( This. chosenwords);//disrupt the placement of words the          This. Word = This. Chosenwords.get (0);//gets the first word in the list that holds the word -TextView Wordtext =Findviewbyid (r.id.wordtextview); $Wordtext.settext ( This. Word);//Show Words the          This. Definations.clear ();//prevent results from having dirty data, clear the list of word meanings the          for(inti = 0; I < 4; i++){ theString defn = This. Dictionary.get ( This. Chosenwords.get (i));//get to the meaning of the word the              This. Definations.add (defn); -         } inCollections.shuffle ( This. definations);//Disrupt Results the          This. adapter.notifydatasetchanged ();//Notify Adpter Change the     } About     //put all the words and their meanings in the Dictionary of Dictionary the     Private voidreadalldefination () { theScanner Scanner =NewScanner (Getresources (). Openrawresource (R.raw.gre_words)); the         if( This. Dictionary = =NULL){ +              This. Dictionary =NewHashmap<>(); -         } the          while(Scanner.hasnext ()) {BayiString line =scanner.nextline (); thestring[] spiece = line.split ("\ t"); the              This. Dictionary.put (Spiece[0], spiece[1]); -         } -     } the}

Android ListView Usage

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.