-
-
Save cofearabi/beaf8a5157f13390b6a0 to your computer and use it in GitHub Desktop.
get the stock data from mysql database and display a chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" > | |
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> | |
<Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> | |
</LinearLayout> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="@string/hello_world" | |
android:id="@+id/TextView02" /> | |
<LinearLayout | |
android:id="@+id/chart" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:orientation="horizontal" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="80dp" | |
android:text="fin" /> | |
</LinearLayout> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.achartenginesample" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="17" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.example.achartenginesample.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.achartenginesample; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import org.achartengine.ChartFactory; | |
import org.achartengine.GraphicalView; | |
import org.achartengine.model.XYMultipleSeriesDataset; | |
import org.achartengine.model.XYSeries; | |
import org.achartengine.renderer.XYMultipleSeriesRenderer; | |
import org.achartengine.renderer.XYSeriesRenderer; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.os.Handler; | |
import android.os.Message; | |
//import android.text.Editable; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
//import android.widget.EditText; | |
import android.widget.LinearLayout; | |
import android.widget.Spinner; | |
import android.widget.TextView; | |
import android.widget.AdapterView.OnItemSelectedListener; | |
public class MainActivity extends Activity implements Runnable,OnClickListener{ | |
private String errmsg=""; | |
private int[] dataArray; | |
private GraphicalView mChart; | |
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset(); | |
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); | |
private XYSeries mCurrentSeries; | |
private XYSeriesRenderer mCurrentRenderer; | |
private Button button1; | |
private String code="7203"; | |
private String name=""; | |
public void run_the_thread(){ | |
Thread thread = new Thread(this); | |
thread.start(); | |
} | |
public void draw_or_repaint(){ | |
LinearLayout layout = (LinearLayout) findViewById(R.id.chart); | |
if (mChart == null) { | |
initChart(); | |
addSampleData(); | |
mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f); | |
layout.addView(mChart); | |
} else { | |
addSampleData(); | |
mChart.repaint(); | |
} | |
} | |
public void run() { | |
dataArray = getSqlData(); | |
handler.sendEmptyMessage(0); | |
} | |
private Handler handler = new Handler() { | |
public void handleMessage(Message msg) { | |
int i =0; | |
while(dataArray[i]!=0){ | |
i=i+1; | |
} | |
errmsg=errmsg + code + " "+ name; | |
TextView textView = (TextView)findViewById(R.id.TextView02); | |
textView.setText(errmsg +" "+ i); | |
errmsg = ""; | |
draw_or_repaint(); | |
} | |
}; | |
private void initChart() { | |
mCurrentSeries = new XYSeries("Sample Data"); | |
mDataset.addSeries(mCurrentSeries); | |
mCurrentRenderer = new XYSeriesRenderer(); | |
mRenderer.addSeriesRenderer(mCurrentRenderer); | |
} | |
private void addSampleData0(){ | |
mCurrentSeries.add(1, 2); | |
mCurrentSeries.add(2, 3); | |
mCurrentSeries.add(3, 2); | |
mCurrentSeries.add(4, 5); | |
mCurrentSeries.add(5, 4); | |
mCurrentSeries.add(6, 3); | |
mCurrentSeries.add(7, 5); | |
} | |
private void addSampleData(){ | |
mCurrentSeries.clear(); | |
if(dataArray[0]==0)return; | |
int i =9999; | |
while(dataArray[i]==0){ | |
i=i-1; | |
} | |
int j = 1; | |
while(i>=0){ | |
mCurrentSeries.add(j, dataArray[i]); | |
i=i-1; | |
j=j+1; | |
} | |
} | |
private int[] getSqlData() { | |
int intarray[] =new int[10000]; | |
int i; | |
i=0; | |
while(i<10000){ | |
intarray[i]=0; | |
i++; | |
} | |
System.out.println("Select Records Example by using the Prepared Statement!"); | |
Connection con = null; | |
int count = 0; | |
try{ | |
Class.forName("com.mysql.jdbc.Driver"); | |
con = DriverManager.getConnection | |
("jdbc:mysql://192.168.2.100:3306/stock_db","user","user"); | |
try{ | |
String sql; | |
String hiduke; | |
int kakaku; | |
// sql | |
// = "SELECT title,year_made FROM movies WHERE year_made >= ? AND year_made <= ?"; | |
sql | |
= "SELECT hiduke,jikoku,code,kakaku FROM stock " + | |
" where code = " + code + | |
" order by hiduke desc,jikoku desc limit 10000"; | |
PreparedStatement prest = con.prepareStatement(sql); | |
//prest.setInt(1,1980); | |
//prest.setInt(2,2004); | |
ResultSet rs = prest.executeQuery(); | |
while (rs.next()){ | |
hiduke = rs.getString(1); | |
kakaku = rs.getInt(4); | |
// System.out.println(hiduke + "\t" + "- " + kakaku); | |
intarray[count]=kakaku; | |
count++; | |
} | |
System.out.println("Number of records: " + count); | |
prest.close(); | |
con.close(); | |
} | |
catch (Exception s){ | |
System.out.println("SQL statement is not executed!"); | |
errmsg=errmsg+s.getMessage(); | |
} | |
} | |
catch (Exception e){ | |
e.printStackTrace(); | |
errmsg=errmsg+e.toString(); | |
} | |
return intarray; | |
} | |
public void onClick(View v){ | |
finish(); | |
} | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
button1=(Button)findViewById(R.id.button1); | |
button1.setOnClickListener(this); | |
String[] zzz = readfile("zzz.txt"); | |
int zzzlen=0; | |
while( zzz[zzzlen] != null ){ | |
zzzlen = zzzlen + 1; | |
} | |
CharSequence[] list = new CharSequence[zzzlen]; | |
for (int i = 0; i < list.length; i++) { | |
list[i] = getname("zzz.txt",zzz[i]); | |
} | |
Spinner spinner = (Spinner)findViewById(R.id.Spinner01); | |
spinner.setAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, list)); | |
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
// code = ((Spinner)parent).getSelectedItem().toString(); | |
int int_index=((Spinner)parent).getSelectedItemPosition(); | |
code=getcode_from_index("zzz.txt",int_index); | |
name = getname("zzz.txt",code); | |
run_the_thread(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
}); | |
} | |
protected void onResume() { | |
super.onResume(); | |
} | |
public String[] readfile(String inputfilename ){ | |
String strarray[] =new String[100]; | |
int i = 0; | |
String arg2 = ""; | |
// Read | |
{ | |
try { | |
String sdPath = Environment.getExternalStorageDirectory().getPath(); | |
String fileName = "/" + inputfilename; // zzz.txt"; | |
BufferedReader br = null; | |
String strtmp[]; | |
try { | |
FileReader fr = new FileReader(sdPath + fileName); | |
br = new BufferedReader(fr); | |
StringBuilder sb = new StringBuilder(); | |
String str; | |
while((str = br.readLine()) != null){ | |
sb.append(str +"\r\n"); | |
strtmp=str.split(",",0); | |
strarray[i]=strtmp[0]; | |
// strarray[i]=str; | |
i=i+1; | |
// Toast.makeText(this, str, Toast.LENGTH_LONG).show(); | |
} | |
} finally { | |
br.close(); | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
arg2=arg2+e.toString(); | |
} | |
} | |
return strarray; | |
} | |
public String getname(String inputfilename,String code ){ | |
String arg2 = ""; | |
String kaerichi=""; | |
// Read | |
{ | |
try { | |
String sdPath = Environment.getExternalStorageDirectory().getPath(); | |
String fileName = "/" + inputfilename; //zzz.txt | |
BufferedReader br = null; | |
String strtmp[]; | |
try { | |
FileReader fr = new FileReader(sdPath + fileName); | |
br = new BufferedReader(fr); | |
StringBuilder sb = new StringBuilder(); | |
String str; | |
while((str = br.readLine()) != null){ | |
strtmp=str.split(",",0); | |
if(strtmp[0].equals(code)){ | |
kaerichi=strtmp[1]; | |
} | |
} | |
} finally { | |
br.close(); | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
arg2=arg2+e.toString(); | |
} | |
} | |
return kaerichi; | |
} | |
public String getcode_from_index(String inputfilename,int int_index){ | |
String arg2 = ""; | |
String kaerichi=""; | |
int i=0; | |
try { | |
String sdPath = Environment.getExternalStorageDirectory().getPath(); | |
String fileName = "/" + inputfilename; //zzz.txt | |
BufferedReader br = null; | |
String strtmp[]; | |
try { | |
FileReader fr = new FileReader(sdPath + fileName); | |
br = new BufferedReader(fr); | |
StringBuilder sb = new StringBuilder(); | |
String str; | |
while((str = br.readLine()) != null){ | |
// sb.append(str +"\r\n"); | |
strtmp=str.split(",",0); | |
if(i == int_index){ | |
kaerichi=strtmp[0]; | |
} | |
// strarray[i]=str; | |
i=i+1; | |
// Toast.makeText(this, str, Toast.LENGTH_LONG).show(); | |
} | |
} finally { | |
br.close(); | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
arg2=arg2+e.toString(); | |
} | |
return kaerichi; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment