Android Adapter ListActivity
AdapterListActivity.java code
package
android.example.adapter;
import
android.app.ListActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.BaseAdapter;
import
android.widget.ListView;
import
android.widget.TextView;
import
android.widget.Toast;
public class
AdapterListActivityActivity extends ListActivity {
public String[] contents = {"text
1", "text 2", "text 3", "text
4", "text 5",
"text 6", "text
7", "text 8", "text 9", "text
10","text 11", "text 12"};
public int[] ids =
{101,102,103,104,105,106,107,108,109,110,111,112};
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final BaseAdapter
adapter = new BaseAdapter() {
public View
getView(int position, View convertView, ViewGroup parent) {
TextView tv = new
TextView(AdapterListActivityActivity.this);
tv.setTextSize(30.0f);
tv.setText(contents[position]);
return tv;
}
public long getItemId(int position) {
// return position;
return ids[position];
}
public Object
getItem(int index) {
return contents[index];
}
public int getCount()
{
return contents.length;
}
};
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new
OnItemClickListener() {
public void
onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() + ",
pos:"+ position +
",
id:" + id, Toast.LENGTH_SHORT).show();
}
});
}
}
main.xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#9999CC"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Download Android Adapter ListActivity Example code
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com