Android ArrayAdapter Example |
Android ArrayAdapter Example สำหรับ ArrayAdapter จัดอยู่ในกลุ่มของ class widget ซึ่งจะใช้กับชุดข้อมูลที่อยู่ในรูปแบบของ Array รูปแบบการอ้างถึง Index ของ Array ต่าง ๆ โดยสามารถใช้ setAdapter ร่วมกับ ListView , GridView และอื่น ๆ
รูปแบบของ ArrayAdapter
public class myAdapter extends ArrayAdapter<Array>
{
public myAdapter(Context c, int textViewResourceId,CountryList[] data) {
// TODO Auto-generated method stub
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
return convertView;
}
}
Example
โครงสร้างของไฟล์ประกอบด้วย 3 ไฟล์คือ MainActivity.java, activity_main.xml และ activity_column.xml
activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ListView and ArrayAdapter : "
android:layout_span="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
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>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="By.. ThaiCreate.Com" />
</LinearLayout>
</TableLayout>
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/ColID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="ID"/>
<TextView
android:id="@+id/ColCode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Code"/>
<TextView
android:id="@+id/ColCountry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Country" />
</LinearLayout>
MainActivity.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
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);
CountryList country_data[] = new CountryList[]
{
new CountryList("1","TH","Thailand"),
new CountryList("2","VN","Vietnam"),
new CountryList("3","ID","Indonesia"),
new CountryList("4","LA","Laos"),
new CountryList("5","MY","Malaysia")
};
CountryAdapter adapter = new CountryAdapter(this,
R.layout.activity_column, country_data);
lisView1.setAdapter(adapter);
}
public class CountryList {
public String ID;
public String Code;
public String Country;
public CountryList(){
super();
}
public CountryList(String ID, String Code, String Country) {
super();
this.ID = ID;
this.Code = Code;
this.Country = Country;
}
}
public class CountryAdapter extends ArrayAdapter<CountryList>
{
Context context;
CountryList country_data[] = null;
public CountryAdapter(Context c, int textViewResourceId,CountryList[] data) {
super(c, R.layout.activity_column, data);
context = c;
country_data = data;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
CountryHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_column, null);
holder = new CountryHolder();
holder.ID = (TextView) convertView.findViewById(R.id.ColID);
holder.Code = (TextView) convertView.findViewById(R.id.ColCode);
holder.Country = (TextView) convertView.findViewById(R.id.ColCountry);
convertView.setTag(holder);
} else {
holder = (CountryHolder) convertView.getTag();
}
CountryList country = country_data[position];
holder.ID.setText(country.ID);
holder.Code.setText(country.Code);
holder.Country.setText(country.Country);
return convertView;
}
}
public class CountryHolder {
TextView ID;
TextView Code;
TextView Country;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Screenshot
Example ArrayAdapter
อ่านเพิ่มเติม ArrayAdapter
http://developer.android.com/reference/android/widget/ArrayAdapter.html
Property & Method (Others Related) |
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2012-08-11 16:40:03 /
2012-08-14 17:12:19 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|
|