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

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > Android ListView แบบ Custom Layout แสดงข้อมูล ListView หลายคอลัมบ์ Multiple Column



Clound SSD Virtual Server

Android ListView แบบ Custom Layout แสดงข้อมูล ListView หลายคอลัมบ์ Multiple Column

Android Listview แบบ Custom Layout ตัวอย่างการใช้ ListView แบบ Custom Multiple Column Layout โดยจะสร้าง Layout ที่เป็นหลาย Column เพื่อจะนำ Column นั้นมาเป็นรูปแบบการแสดงผลของ ListView และข้อมูลที่จะนำมา Map กับ ListView จะใช้เป็นแบบ HashMap ซึ่งจะมีการเก็บข้อมูลแบบ Key และ Value ผ่าน SimpleAdapter ของ Android

		/*** Rows 1 ***/
		map = new HashMap<String, String>();
		map.put("MemberID", "1");
		map.put("Name", "Weerachai");
		map.put("Tel", "0819876107");
		MyArrList.add(map);
		
		/*** Rows 2 ***/
		map = new HashMap<String, String>();
		map.put("MemberID", "2");
		map.put("Name", "Win");
		map.put("Tel", "021978032");
		MyArrList.add(map);	


ตัวอย่างการสร้างข้อมูลในรูปแบบของ HashMap ที่ประกอบด้วย Key และ Value

Android ListView and Custom Multiple Column

ListView แสดงข้อมูลแบบ Multiple Column ด้วย Custom Layout


จาก Screenshot ของ ListView จะแบ่งการแสดงผลข้อมูลออกเป็น 3 Column ซึ่ง Column เหล่านี้จะมาจาก Custom Layout ของ activity_column.xml


Android ListView and Custom Multiple Column


        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_column,
                new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});      
        lisView1.setAdapter(sAdap);


ใน setAdapter จะมีการ Map ข้อมูลจาก HashMap กับ R.layout.activity_column และ Widgets รวมทั้ง Key ของข้อมูล

Code เต็ม ๆ

Android ListView and Custom Multiple Column

โครงสร้างของไฟล์ประกอบด้วย 3 ไฟล์คือ MainActivity.java, activity_main.xml และ activity_column.xml


Android ListView and Custom Multiple Column
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0.1">   
   
   <ListView
       android:id="@+id/listView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
   </ListView>
	        
</LinearLayout>



Android ListView and Custom Multiple Column
activity_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/linearLayout1" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" >

	<TextView
	android:id="@+id/ColMemberID" 
		android:layout_width="0dp"
		android:layout_height="wrap_content" 
		android:layout_weight="1"
		android:text="MemberID"/>

	<TextView
		android:id="@+id/ColName" 
		android:layout_width="0dp"
		android:layout_height="wrap_content" 
		android:layout_weight="2"
		android:text="Name"/>

	<TextView
	    android:id="@+id/ColTel"
	    android:layout_width="0dp"
	    android:layout_height="wrap_content"
	    android:layout_weight="1"
	    android:text="Tel" />

</LinearLayout>









MainActivity.java
package com.myapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        // listView1
        final ListView lisView1 = (ListView)findViewById(R.id.listView1); 
        
		ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		
		/*** Rows 1 ***/
		map = new HashMap<String, String>();
		map.put("MemberID", "1");
		map.put("Name", "Weerachai");
		map.put("Tel", "0819876107");
		MyArrList.add(map);
		
		/*** Rows 2 ***/
		map = new HashMap<String, String>();
		map.put("MemberID", "2");
		map.put("Name", "Win");
		map.put("Tel", "021978032");
		MyArrList.add(map);		
       
        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_column,
                new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});      
        lisView1.setAdapter(sAdap);
        

    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}


Screenshot

Android ListView and Custom Multiple Column

แสดงข้อมูล ListView แบบหลาย Column









Basic Android and ListView Widgets

Basic Android and ListView Widgets and SQLite Database


   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-22 12:35:14 / 2017-03-26 21:38:03
  Download : No files
 Sponsored Links / Related

 
Android ListView and ImageView Image / Text in ListView Custom Layout Column
Rating :

 
Android แสดงรูปภาพ ImageView จาก Web URL ของเว็บไซต์ บน ListView
Rating :

 
Android ListView Padding and Pagination
Rating :

 
Android กับ ListView แสดงข้อมูลจาก Database ของ SQLite
Rating :

 
Android ListView and Checkbox
Rating :

 
Android ListView and Buttons inside Create Custom Command in ListView
Rating :

 
Android Populate ListView ImageView get Image Resource from SD Card
Rating :

 
Android EditText or Textbox in ListView
Rating :

 
Android Gesture on ListView Detecting Sliding Items (Flip and Swipe)
Rating :

 
Android and Web Server ของ PHP กับ MySQL แสดงบน ListView ในรูปแบบของ JSON
Rating :

 
Android JSON Retrieving Data from URL Web Server (PHP MySQL and JSON)
Rating :

 
Android ProgressDialog When Click Item in ListView for Download file from Server
Rating :

 
Android Multiple Upload Send file to Server and Show Items ProgressBar in ListView
Rating :

 
Android Multiple Download file in ListView and Show Progress unit percentage
Rating :

 
Android AsyncTask update ListView notifyDataSetChanged when Items Load finish
Rating :

 
Android (ListView/GridView) get Result from Web Server and Paging Pagination
Rating :

 
Android Performance of Listview
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
   







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