Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone


Android and ArrayList (Java)

Android and ArrayList (Java) สำหรับ ArrayList ในภาษา Java ที่จะพัฒนาบน Android นิยมใช้จัดเก็บกับชุดของข้อมูลที่ไม่ทราบขนาดหรือ Size ที่แท้จริง โดยที่เราสามารถเพิ่ม สมาชิกให้กับ ArrayList ผ่าน method ที่มีชื่อว่า add โดนสามารถเพิ่มสมาชิกหรือข้อมูลใน ArrayList ได้เรื่อย ๆ มากพอกับความต้องการของข้อมูลชุดเหล่านั้น และสามารถเพิ่มปรับลดขนาดของ ArrayList ในตำแหน่ง index ต่าง ๆ ได้ ใน ArrayList สามารถรับข้อมูลได้หลากหลายประเภท ที่อยู่ในชุดประเภทเดียวกัน รองรับข้อมูลที่ซ้ำกันได้ รวมทั้งที่เป็นค่าว่าง (null) โดยสมาชิกของ ArrayList จะถูกจัดเก็บตามลำดับที่เพิ่มเข้ามา และ ArrayList ยังเป็นชนิดที่เป็น unsynchronized สามารถเข้ามาใช้ข้อมูลใน ArrayList ได้ในเวลาเดียวกันหลาย ๆ ครั้ง โดยไม่มีปัญหาจะต้องรอคิวว่าให้ Thread อื่นที่เรียกใช้ ArrayList ตัวนั้น ๆ ทำงานจนเสร็จสิ้นเสียก่อน

รูปแบบการประกาศ ArrayList
1.ArrayList<String> myArrList = new ArrayList<String>();
2.myArrList.add("Belgium");
3.myArrList.add("France");
4.myArrList.add("Italy");
5.myArrList.add("Germany");

ประกาศตัวแปร myArrList เป็น ArrayList แบบ String ซึ่งมีสมาชิกอยู่ 4 รายการ คือ Belgium, France, Italy, Germany

การนับขนาด Size ใน ArrayList
1.myArrList.size();


การ Loop ค่าใน ArrayList มาใช้งาน
1.for (int i = 0; i < myArrList.size(); i++) {
2.    // myArrList.get(i))
3.}

อีกวิธี
1.for (String temp : myArrList) {
2.    //temp;
3.}


การตรวจสอบว่า ArrayList มีสมาชิกอยู่หรือไม่
1.if (myArrList.contains("Italy"))
2.{
3.     // Already member
4.}


สำหรับ ArrayList การทำงานคล้าย ๆ กับ HashSet เพียงแต่ HashSet จะมไยอมให้มีสมาชิกซ้ำกันได้

Android and HashSet (Java)



Example 1 การใช้ ArrayList บน Android ด้วยภาษา Java แบบง่าย ๆ

ออกแบบ XML Layout

Android and ArrayList (Java)

activity_main.xml
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    xmlns:tools="http://schemas.android.com/tools"
03.    android:layout_width="match_parent"
04.    android:layout_height="match_parent" >
05. 
06.    <TextView
07.        android:id="@+id/textView1"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content"
10.        android:layout_alignParentTop="true"
11.        android:layout_centerHorizontal="true"
12.        android:layout_marginTop="22dp"
13.        android:text="TextView" />
14. 
15.    <TextView
16.        android:id="@+id/textView2"
17.        android:layout_width="wrap_content"
18.        android:layout_height="wrap_content"
19.        android:layout_alignLeft="@+id/textView1"
20.        android:layout_below="@+id/textView1"
21.        android:layout_marginTop="20dp"
22.        android:text="TextView" />
23.     
24. 
25. 
26.    <ListView
27.        android:id="@+id/listView1"
28.        android:layout_width="match_parent"
29.        android:layout_height="wrap_content"
30.        android:layout_alignParentLeft="true"
31.        android:layout_marginTop="99dp" >
32. 
33.    </ListView>
34. 
35.</RelativeLayout>


MainActivity.java
01.package com.myapp;
02. 
03.import java.util.ArrayList;
04. 
05.import android.os.Bundle;
06.import android.app.Activity;
07.import android.view.Menu;
08.import android.widget.ArrayAdapter;
09.import android.widget.ListView;
10.import android.widget.TextView;
11. 
12.public class MainActivity extends Activity {
13. 
14.    ArrayAdapter<String> adapter;
15.     
16.    @Override
17.    public void onCreate(Bundle savedInstanceState) {
18.        super.onCreate(savedInstanceState);
19.        setContentView(R.layout.activity_main);
20.         
21.  
22.        /**** Sample 1 ****/
23.        ArrayList<String> myArrList = new ArrayList<String>();
24.        myArrList.add("Belgium");
25.        myArrList.add("France");
26.        myArrList.add("Italy");
27.        myArrList.add("Germany");
28.         
29.        // Get Size ArrayList
30.        final TextView txtView1 = (TextView)findViewById(R.id.textView1);
31.        txtView1.setText("ArrayList Size = " + myArrList.size());
32.         
33.        // Loop Data ArrayList
34.        final TextView txtView2 = (TextView)findViewById(R.id.textView2);
35.        txtView2.setText("");
36.        for (int i = 0; i < myArrList.size(); i++) {
37.            // myArrList.get(i))
38.            txtView2.setText(txtView2.getText() + myArrList.get(i) + ",");
39.        }
40.         
41.        // Loop Sample
42.        //for (String temp : myArrList) {
43.            // txtView2.setText(temp);
44.        //}
45.         
46.        // Convert ArrayList to Array
47.        String[] myArr = {};
48.        myArr = myArrList.toArray(new String[myArrList.size()]);
49.         
50.        // Show Array to ListView
51.        final ListView lView = (ListView)findViewById(R.id.listView1);
52.        adapter = new ArrayAdapter<String>(this,
53.                android.R.layout.simple_list_item_1, myArr);
54.        lView.setAdapter(adapter);
55.         
56.    }
57.     
58.     
59.    @Override
60.    public boolean onCreateOptionsMenu(Menu menu) {
61.        getMenuInflater().inflate(R.menu.activity_main, menu);
62.        return true;
63.    }
64.     
65.}


Screenshot

Android and ArrayList (Java)





Example 2 การใช้ Array ร่วมกับ HashMap เพื่อเก็บข้อมูลที่มี Key และ Value

ออกแบบ XML Layout

Android and ArrayList (Java)

activity_main.xml
01.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    xmlns:tools="http://schemas.android.com/tools"
03.    android:layout_width="match_parent"
04.    android:layout_height="match_parent" >
05. 
06.    <TextView
07.        android:id="@+id/textView1"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content"
10.        android:layout_alignParentTop="true"
11.        android:layout_centerHorizontal="true"
12.        android:layout_marginTop="22dp"
13.        android:text="TextView" />
14. 
15.    <TextView
16.        android:id="@+id/textView2"
17.        android:layout_width="wrap_content"
18.        android:layout_height="wrap_content"
19.        android:layout_alignLeft="@+id/textView1"
20.        android:layout_below="@+id/textView1"
21.        android:layout_marginTop="20dp"
22.        android:text="TextView" />
23.     
24. 
25. 
26.    <ListView
27.        android:id="@+id/listView1"
28.        android:layout_width="match_parent"
29.        android:layout_height="wrap_content"
30.        android:layout_alignParentLeft="true"
31.        android:layout_marginTop="250dp" >
32. 
33.    </ListView>
34. 
35.</RelativeLayout>



Android and ArrayList (Java)

activity_column.xml
01.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.    android:id="@+id/linearLayout1"
03.    android:layout_width="fill_parent"
04.    android:layout_height="fill_parent" >
05. 
06.    <TextView
07.    android:id="@+id/ColMemberID"
08.        android:layout_width="0dp"
09.        android:layout_height="wrap_content"
10.        android:layout_weight="1"
11.        android:text="MemberID"/>
12. 
13.    <TextView
14.        android:id="@+id/ColName"
15.        android:layout_width="0dp"
16.        android:layout_height="wrap_content"
17.        android:layout_weight="2"
18.        android:text="Name"/>
19. 
20.    <TextView
21.        android:id="@+id/ColTel"
22.        android:layout_width="0dp"
23.        android:layout_height="wrap_content"
24.        android:layout_weight="1"
25.        android:text="Tel" />
26. 
27.</LinearLayout>


MainActivity.java
01.package com.myapp;
02. 
03.import java.util.ArrayList;
04.import java.util.HashMap;
05. 
06.import android.os.Bundle;
07.import android.app.Activity;
08.import android.view.Menu;
09.import android.widget.ArrayAdapter;
10.import android.widget.ListView;
11.import android.widget.SimpleAdapter;
12.import android.widget.TextView;
13. 
14.public class MainActivity extends Activity {
15. 
16.    ArrayAdapter<String> adapter;
17.     
18.    @Override
19.    public void onCreate(Bundle savedInstanceState) {
20.        super.onCreate(savedInstanceState);
21.        setContentView(R.layout.activity_main);
22.         
23.  
24.        ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
25.        HashMap<String, String> map;
26.         
27.        /*** Rows 1 ***/
28.        map = new HashMap<String, String>();
29.        map.put("MemberID", "1");
30.        map.put("Name", "Weerachai");
31.        map.put("Tel", "0819876107");
32.        myArrList.add(map);
33.         
34.        /*** Rows 2 ***/
35.        map = new HashMap<String, String>();
36.        map.put("MemberID", "2");
37.        map.put("Name", "Win");
38.        map.put("Tel", "021978032");
39.        myArrList.add(map);
40.         
41.        /*** Rows 3 ***/
42.        map = new HashMap<String, String>();
43.        map.put("MemberID", "3");
44.        map.put("Name", "Eak");
45.        map.put("Tel", "0123456789");
46.        myArrList.add(map);    
47.         
48.        // Get Size ArrayList
49.        final TextView txtView1 = (TextView)findViewById(R.id.textView1);
50.        txtView1.setText("ArrayList Size = " + myArrList.size());
51.         
52.        // Loop Data ArrayList
53.        final TextView txtView2 = (TextView)findViewById(R.id.textView2);
54.        txtView2.setText("");
55.        for (int i = 0; i < myArrList.size(); i++) {
56.            String sMemberID = myArrList.get(i).get("MemberID").toString();
57.            String sName = myArrList.get(i).get("Name").toString();
58.            String sTel = myArrList.get(i).get("Tel").toString();
59.            txtView2.setText(txtView2.getText() + "MemberID = " + sMemberID + ",\r\n");
60.            txtView2.setText(txtView2.getText() + "Name = " + sName + ",\r\n");
61.            txtView2.setText(txtView2.getText() + "Tel = " + sTel + "\r\n\r\n");
62.             
63.        }
64.         
65.        // Show ArrayList to ListView
66.        final ListView lView = (ListView)findViewById(R.id.listView1);
67.        SimpleAdapter sAdap;
68.        sAdap = new SimpleAdapter(MainActivity.this, myArrList, R.layout.activity_column,
69.                new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});     
70.        lView.setAdapter(sAdap);
71.         
72.    }
73.     
74.     
75.    @Override
76.    public boolean onCreateOptionsMenu(Menu menu) {
77.        getMenuInflater().inflate(R.menu.activity_main, menu);
78.        return true;
79.    }
80.     
81.}


Screenshot

Android and ArrayList (Java)

   
Hate it
Don't like it
It's ok
Like it
Love it
Share

Property & Method (Others Related)

Android and Collections Set
Android and Array (Java)
Android and HashMap/Hashtable (Java)
Android and HashSet (Java)

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-29 17:45:42 / 2012-07-30 11:06:10
  Download : No files
 Sponsored Links / Related

 
Android Custom Adapter
Rating :

 
Android People Contact List, Name, Phone No, Photo Picture, Email and Address
Rating :

 
Android Rating (Vote) and ListView Part 1
Rating :

 
Android Rating (Vote) and ListView Part 2 (Member Login and Average Rating)
Rating :

 
Android PhoneGap (jQuery Mobile) Create Convert App from Website(URL)
Rating :

 
Android Capture Image and Camera Capture Screenshot (android.view.SurfaceView)
Rating :

 
Android Pull Down to Refresh And Release to Refresh or Update (Part 1)
Rating :

 
Android Pull Down to Refresh And Release to Update (Part 2 , PHP & MySQL)
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่