Android Query from database and show list

We make 2 layout2 (2 pages)

1. layout Main1.xml for first item
2.layout Sippner.xml : after click on item from Main1.xml it will show list of data from database of student database file.
----------------------------------------------------------------------------

MainActivity.java
package com.example.myui2;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity  {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
Cursor c=this.getContentResolver().query(
Uri.parse(”content://rmutp.students/stu”),
null, null,null,null);
SimpleCursorAdapter sca=new SimpleCursorAdapter (
this,
R.layout.spinnertemplate,
c,
new String[] {”firstname”,”lastname”},
new int[] {R.id.textView1,R.id.textView2});
Spinner sn=(Spinner)findViewById(R.id.spinner1);
sn.setAdapter(sca);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
-----------------------------------------------------------------------------------------

โปรแกรมหน้าจอ Spinnertemplate.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >
<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher” />
<LinearLayout
android:layout_width=”wrap_content”
android:layout_height=”match_parent”
android:orientation=”vertical” >
<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView” />
<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView” />
</LinearLayout>
</LinearLayout>

-----------------------------------------------------------------------------
Main1.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >
<Spinner
android:id=”@+id/spinner1″
android:layout_width=”match_parent”
android:layout_height=”wrap_content” />
</LinearLayout>
----------------------------------------------------------------------------
Result after Run
29
After click first item it will show list of data from student database.

28
จากการฝึกอบรม Android วิทยากร อ.โชคชัย จันทร์เชย