Home Backend Development XML/RSS Tutorial How android uses DOM to parse XML + how to make an emoticon pop-up box

How android uses DOM to parse XML + how to make an emoticon pop-up box

Feb 20, 2017 pm 03:01 PM

Rendering:


How to parse the following xml:


1

2

3

4

5

6

7

8

9

10

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<array>

    <string>(#大笑)</string>

    <string>(#微笑)</string>

    <string>(#亲亲)</string>

    <string>(#抱抱)</string>

    <string>(#色色)</string>

    <string>(#好失望哟)</string>

</array>

Copy after login


Like this To parse:



##

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

public class MessageFaceModel {

 

    /** single instance of this class */

    private static MessageFaceModel instance = null;

     

    /** context */

    private boolean mInitialized = false;

     

    private HashMap<String,Bitmap> mFaceMap = new HashMap<String,Bitmap>();

     

    private ArrayList<String> mFaceStrings = new ArrayList<String>();

     

    private ArrayList<Bitmap> mFaceIcons = new ArrayList<Bitmap>();

     

    /**

     * constructor

     */

    private MessageFaceModel(){

         

    }

     

    /**

     * Factory method

     */

    public static synchronized MessageFaceModel getInstance(){

        if(instance == null){

            instance = new MessageFaceModel();

        }

        return instance;

    }

     

    /**

     * initialize face data

     */

    public void init(Context context){

        if(mInitialized){

            //initialize only once

            return;

        }

         

        mFaceMap.clear();

        mFaceStrings.clear();

        mFaceIcons.clear();

         

        AssetManager assetManager = context.getAssets();

        ArrayList<String> faces = new ArrayList<String>();

        DocumentBuilderFactory docBuilderFactory = null;

        DocumentBuilder docBuilder = null;

        Document doc = null;

        try {

            docBuilderFactory = DocumentBuilderFactory.newInstance();

            docBuilder = docBuilderFactory.newDocumentBuilder();

            doc = docBuilder.parse(assetManager.open("MessageFace.xml"));

            Element root = doc.getDocumentElement();

            NodeList nodeList = root.getElementsByTagName("string");

            for(int i =0;i< nodeList.getLength();i++)

            {

                Node node = nodeList.item(i);

                String s = "";

                NodeList list = node.getChildNodes();

                if(list != null){

                    for(int j = 0; j < list.getLength(); j++){

                        s += list.item(j).getNodeValue();

                    }

                }

                faces.add(s);

            }

             

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } finally{

            doc = null;

            docBuilder = null;

            docBuilderFactory = null;

 

        }

         

        int i;

        for(i = 0; i < faces.size(); ++i){

            int index = i + 1;

            int id = context.getResources().getIdentifier(  

                    "msgface_" + index,   

                    "drawable", "com.example.tianqitongtest");

            try {

                Bitmap bm =  BitmapFactory.decodeResource(context.getResources(),id);

                mFaceMap.put(faces.get(i), bm);

                mFaceStrings.add(faces.get(i));

                mFaceIcons.add(bm);

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

         

        mInitialized = true;

         

    }

     

    public ArrayList<Bitmap> getFaceIcons(){

        return mFaceIcons;

    }

     

    public ArrayList<String> getFaceStrings(){

        return mFaceStrings;

    }

     

    public Bitmap getFaceIcon(String face){

        if(mFaceMap != null){

            return mFaceMap.get(face);

        }else{

            return null;

        }

    }

     

    public void clear() {

        mInitialized = false;

        mFaceMap.clear();

        mFaceStrings.clear();

        mFaceIcons.clear();

    }

}

Copy after login

Then write this Dialog style activity:



1

2

3

4

<activity android:name=".InputFaceActivity"

           android:theme="@android:style/Theme.Dialog"

           android:configChanges="keyboardHidden|orientation">

       </activity>

Copy after login

Layout is:


##

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="300dp"

  android:minHeight="100dp"

  android:background="#EFEFEF">

  <GridView

      xmlns:android="http://schemas.android.com/apk/res/android"

      android:id="@+id/input_face_gridview"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:layout_marginLeft="18dp"

      android:layout_marginRight="10dp"

      android:layout_marginTop = "18dp"

      android:layout_marginBottom = "30dp"

      android:numColumns="auto_fit"

      android:horizontalSpacing="10dp"

      android:verticalSpacing="15dp"

      android:columnWidth="50dp"

      android:stretchMode="columnWidth"

      android:gravity="center"

      android:layout_weight="1.0">

  </GridView>

   

  <LinearLayout

      xmlns:android="http://schemas.android.com/apk/res/android"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content">

      <Button

          android:id="@+id/input_face_cancel_button"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:background="@drawable/cancel_button_style">

      </Button>

  </LinearLayout>

</RelativeLayout>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

public class InputFaceActivity extends Activity{

 

    private MessageFaceModel mMessageFaceModel = MessageFaceModel.getInstance();

    public static final int SELECT_STATE_FACE_ICON = 209;

    public static final int SELECT_MESSAGE_FACE_ICON = 109;

    private int mWidth = 0;

     

     

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        mWidth = this.getResources().getDimensionPixelSize(R.dimen.image_width);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,

                             WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        setContentView(R.layout.input_face_activity);

        GridView gridView = (GridView) findViewById(R.id.input_face_gridview);  

        gridView.setAdapter(new FaceListAdapter());

        gridView.setOnItemClickListener(new FaceListOnItemClickListener());

         

        Button cancelButton = (Button)findViewById(R.id.input_face_cancel_button);

        cancelButton.setOnClickListener(new OnClickListener(){

 

            @Override

            public void onClick(View arg0) {

                finish();

            }

             

        });

 

    }

     

    private class FaceListAdapter extends BaseAdapter {

         

        public int getCount() {

            if(mMessageFaceModel.getFaceIcons() != null){

                return mMessageFaceModel.getFaceIcons().size();

            }else{

            return 0;

            }

        }

 

        public Object getItem(int arg0) {

            return arg0;

        }

 

        public long getItemId(int arg0) {

            return arg0;

        }

 

        public View getView(int position, View convertView, ViewGroup parent) {

             

            ImageView view = new ImageView(InputFaceActivity.this);

            view.setImageBitmap(mMessageFaceModel.getFaceIcons().get(position));

             

            view.setLayoutParams(new GridView.LayoutParams(mWidth, mWidth));

            view.setScaleType(ScaleType.CENTER);

            return view;

        }

         

    }

     

}

Copy after login

The above is how android uses DOM to parse XML + how to make an emoticon pop-up box. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Oppo Find X8 design looks like a cross between Apple iPhone 16 Pro and OnePlus Open in early images Oppo Find X8 design looks like a cross between Apple iPhone 16 Pro and OnePlus Open in early images Sep 28, 2024 am 06:04 AM

Historically, Oppo has refreshed its flagship 'Find X' series in late winter or early spring, save for the original Find X that it announced in June 2018. To that end, the Find X7 and Find X7 Ultra are barely more than six months old at this point. H

Galaxy A16 5G: Leak reveals future release aimed at freshening up Samsung\'s cheapest Galaxy A series model Galaxy A16 5G: Leak reveals future release aimed at freshening up Samsung\'s cheapest Galaxy A series model Sep 12, 2024 pm 12:20 PM

It seems that the launch of a new entry-level Galaxy A series smartphone could be in the offing. Although Samsung has not officially announced anything just yet, Android Headlines has already published render images of what it claims is a Galaxy A15

Xiaomi Redmi Note 14, Redmi Note 14 Pro and Redmi Note 14 Pro Plus to shake up mid-range smartphone market with Gorilla Glass Victus 2 and IP69 certification Xiaomi Redmi Note 14, Redmi Note 14 Pro and Redmi Note 14 Pro Plus to shake up mid-range smartphone market with Gorilla Glass Victus 2 and IP69 certification Sep 24, 2024 pm 12:15 PM

Xiaomi has just announced the release date for its next Redmi Note smartphones. As expected, these are set to arrive as three Redmi Note 14 series handsets. More specifically, the company intends to release the Redmi Note 14, Redmi Note 14 Pro and Re

See all articles