Java tutorial
package com.lance.commu.fragment; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.widget.Toast; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.example.slidingsimplesample.R; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; import com.lance.commu.sqliteDB.DB_Handler; public class BaseActivity extends SlidingFragmentActivity { // Sherlock public static boolean flag = true; protected ListFragment mFrag; ConnectivityManager connect; String requestURL1; int index = 0; private static HashMap<String, String> contactList; // , String[] dB_Phone_List; // Oracle DB String[] phone_Book_List = new String[1000]; // , // ArrayList<String> for_Sort_List1 = new ArrayList<String>(); ArrayList<String> for_Sort_List2 = new ArrayList<String>(); static String[] for_Sort_Array1; // Oracle DB , ArrayList<String> union_Phone_List_Number = new ArrayList<String>(); ArrayList<String> union_Phone_List_Name = new ArrayList<String>(); //Sqlite DB DB_Handler db_Handler; Cursor cursor = null; public static ArrayList<String> sqlite_Code = new ArrayList<String>(); public static ArrayList<String> sqlite_Name = new ArrayList<String>(); public static ArrayList<String> sqlite_Phone_Number = new ArrayList<String>(); public BaseActivity() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("base onCreate "); // set the Behind View setBehindContentView(R.layout.menu_frame); if (savedInstanceState == null) { FragmentTransaction t = this.getSupportFragmentManager().beginTransaction(); mFrag = new MenuListFragment(); t.replace(R.id.menu_frame, mFrag); t.commit(); } else { mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame); } // customize the SlidingMenu SlidingMenu sm = getSlidingMenu(); sm.setShadowWidthRes(R.dimen.shadow_width); sm.setShadowDrawable(R.drawable.shadow); sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); sm.setFadeDegree(0.35f); sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); sm.setBehindOffset(400); getSupportActionBar().setDisplayHomeAsUpEnabled(true); //////////////////////////////////////// connect = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); // requestURL1 = "http://121.157.84.63:8080/lance/androidFriendList.jsp"; // // // OracleDB // ListView getContactData(); //Sqlite DB //Oracle DB Sqlite // Oracle DB //Sqlite // Oracle DB Sqlite try { db_Handler = DB_Handler.open(this); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("db open "); } // //Oracle DB if (connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI) .getState() == NetworkInfo.State.CONNECTED) { if (flag == true) { new Networking1().execute(); System.out.println(" "); flag = false; // // Sqlite ListView } } else { Toast toast = Toast.makeText(BaseActivity.this, " ", Toast.LENGTH_SHORT); toast.show(); } System.out.println("base onCreate "); }//Oncreate end // private void getContactData() { Cursor phoneCursor = null; contactList = new HashMap<String, String>(); try { // URI Uri uContactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // String strProjection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME; // phoneCursor = getContentResolver().query(uContactsUri, null, null, null, strProjection); phoneCursor.moveToFirst(); String name = ""; String phoneNumber = ""; // int nameColumn = phoneCursor.getColumnIndex(Phone.DISPLAY_NAME); // int phoneColumn = phoneCursor.getColumnIndex(Phone.NUMBER); while (!phoneCursor.isAfterLast()) { name = phoneCursor.getString(nameColumn); phoneNumber = phoneCursor.getString(phoneColumn); // HashMap data contactList.put(phoneNumber, name); phoneCursor.moveToNext(); } } catch (Exception e) { e.printStackTrace(); } finally { if (phoneCursor != null) { phoneCursor.close(); phoneCursor = null; for (Map.Entry<String, String> s : contactList.entrySet()) { String phone_number = s.getKey(); phone_number = phone_number.replaceAll("-", ""); String name = s.getValue(); // phone_Book_List[index++] = phone_number; phone_Book_List[index++] = name; } } } }// getContactData end //AsyncTask private class Networking1 extends AsyncTask<URL, Integer, String> { @Override protected void onPreExecute() { //doInBackground , AsyncTask.execute() // super.onPreExecute(); } @Override protected String doInBackground(URL... params) { // //AsyncTask.execute(params) String result = null; try { //Oracle DB result result = sendData1(requestURL1); } catch (ClientProtocolException e) { e.printStackTrace(); System.out.println("clientprotocol exception"); } catch (IOException e) { e.printStackTrace(); System.out.println("io excption"); } onCancelled(); return result; } @Override protected void onProgressUpdate(Integer... values) { //doInBackground UI //doInBackground UI //doInBackground publishProgress( progress ) super.onProgressUpdate(values); } @Override protected void onPostExecute(String result) { //doInBackground return //AsyncTask parameter null // onPostExecute UIThread super.onPostExecute(result); //System.out.println(result); // sqlite .() db_Handler.removeData(); sqlite_Name.clear(); sqlite_Code.clear(); sqlite_Phone_Number.clear(); //Oracle DB result dB_Phone_List dB_Phone_List = result.split(","); dB_Phone_List[0] = dB_Phone_List[0].substring(25, 38); dB_Phone_List[dB_Phone_List.length - 1] = dB_Phone_List[dB_Phone_List.length - 1].substring(1, 14); // '-' for (int i = 0; i < dB_Phone_List.length; i++) { dB_Phone_List[i] = dB_Phone_List[i].trim(); dB_Phone_List[i] = dB_Phone_List[i].replaceAll("-", ""); } //OracleDB for (int i = 0; i < index; i++) { for (int j = 0; j < dB_Phone_List.length; j++) { if (phone_Book_List[i].equals(dB_Phone_List[j])) { // //for_Sort_List1 //( ) for_Sort_List1.add(phone_Book_List[i + 1] + "," + phone_Book_List[i]); } } } // Collections.sort(for_Sort_List1); // , for_Sort_List2 ( ) for (int i = 0; i < for_Sort_List1.size(); i++) { for_Sort_Array1 = for_Sort_List1.get(i).split(","); for_Sort_List2.add(for_Sort_Array1[0]); for_Sort_List2.add(for_Sort_Array1[1]); } // . for (int i = 0; i < for_Sort_List2.size(); i += 2) { union_Phone_List_Name.add(for_Sort_List2.get(i)); union_Phone_List_Number.add(for_Sort_List2.get(i + 1)); } // Sqlite // sqlite for (int i = 0; i < union_Phone_List_Name.size(); i++) { db_Handler.insert(union_Phone_List_Name.get(i), union_Phone_List_Number.get(i)); } cursor = db_Handler.selectAll(); while (cursor.moveToNext()) { sqlite_Code.add(cursor.getString(0)); sqlite_Name.add(cursor.getString(1)); sqlite_Phone_Number.add(cursor.getString(2)); } cursor.close(); System.out.println(" "); } @Override protected void onCancelled() { //AsyncTask // for_Sort_List1.clear(); for_Sort_List2.clear(); union_Phone_List_Name.clear(); union_Phone_List_Number.clear(); super.onCancelled(); } }// Networking1 end private String sendData1(String requestURL1) throws ClientProtocolException, IOException { HttpPost request = makeHttpPost1(requestURL1); HttpClient client = new DefaultHttpClient(); ResponseHandler<String> reshandler = new BasicResponseHandler(); String result = client.execute(request, reshandler); return result; } private HttpPost makeHttpPost1(String requestURL1) { HttpPost request = new HttpPost(requestURL1); List<NameValuePair> dataList = new ArrayList<NameValuePair>(); request.setEntity(makeEntity1(dataList)); return request; } private HttpEntity makeEntity1(List<NameValuePair> dataList) { HttpEntity result = null; try { result = new UrlEncodedFormEntity(dataList, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); System.out.println(" exception"); } return result; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: toggle(); return true; } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main, menu); return true; } public void fragmentReplace(int reqNewFragmentIndex) { Fragment newFragment = null; newFragment = getFragment(reqNewFragmentIndex); final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_mainContainer, newFragment); getSlidingMenu().showContent(); transaction.commit(); } private Fragment getFragment(int idx) { Fragment newFragment = null; switch (idx) { case 0: newFragment = new Fragment_Guide(); break; case 1: newFragment = new Fragment_Guide(); break; case 2: newFragment = new Fragment_FriendList(); break; case 3: newFragment = new Fragment_Set(); break; case 4: newFragment = new Fragment_Information(); break; case 5: newFragment = new Fragment_License(); break; case 6: newFragment = new Fragment_Logout(); break; default: break; } return newFragment; } }