Reputation: 267
In my application, i want to display data that is saved in MY_SQL on android application TextViews. How can i do it
my android application layout is:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cus_name"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/cus_name_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/contact_no"
android:clickable="true"
android:focusable="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/contact_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ticket_no"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/ticket_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<requestFocus />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="@string/task_detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/task_detail_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/attend_by"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/attend_by_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
My java code is:
public static String attend_by_txt;
TextView cus_name_txt, contact_no_txt, ticket_no_txt;
TextView task_detail_txt;
EditText attend_by_txtbx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_task);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
cus_name_txt = (TextView)findViewById(R.id.cus_name_txta);
contact_no_txt = (TextView)findViewById(R.id.contact_no_txta);
task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);
I want that when i click on customer_name, Contact_No, ticket_No, Task_detail. it display data that is saved in MY_SQL. Please guide me how can i retrieve data from database and display it in android. I'll be very thankful to you.
Upvotes: 1
Views: 3031
Reputation: 20563
This depends on whether you want your database to be local or stored on a remote server.
If it is local, You'll have to convert your MYSQL db to a SQLite db since this the one that Android supports.
See here for instructions:
Then you can access it using the native android methods. See db tutorial here:
http://www.vogella.com/articles/AndroidSQLite/article.html
If it is hosted on a remote server, then you'll have to create REST API's (by writing server side code using a language like PHP, Ruby, Python etc) that your Android app can consume to obtain the data from the db.
There are other options that avoid REST API's as well. See:
How to get from a MySql server to an Android app?
See here for a beginners tutorial on REST:
http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/
Upvotes: 3