Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package pack.OM1;
  2.  
  3.  
  4.  
  5. import java.io.DataInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import android.app.Activity;
  13. import android.content.Context;
  14. import android.content.SharedPreferences;
  15. import android.content.res.Resources;
  16. import android.database.Cursor;
  17. import android.os.Bundle;
  18. import android.provider.Contacts.People;
  19. //import android.view.ContextMenu;
  20. import android.view.View;
  21. import android.view.View.OnClickListener;
  22. import android.widget.AdapterView;
  23. import android.widget.AdapterView.OnItemSelectedListener;
  24. import android.widget.ArrayAdapter;
  25. import android.widget.EditText;
  26. import android.widget.Spinner;
  27. import android.widget.TextView;
  28.  
  29. public class OM1Activity extends Activity {
  30.     /** Called when the activity is first created. */
  31.     @Override
  32.     public void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.        
  35.        
  36.        
  37.         int x=loadTitlePref(this);
  38.         x++;
  39.        saveTitlePref(this, x);
  40.        
  41.         setContentView(R.layout.main);
  42.         ((TextView)findViewById(R.id.TextView01)).setText(String.valueOf(x));
  43.        
  44.        
  45.         findViewById(R.id.Button01).setOnClickListener(new  OnClickListener(){
  46.  
  47.             @Override
  48.             public void onClick(View arg0) {
  49.                 FileInputStream in;
  50.                 try {
  51.                     in = openFileInput("plik");
  52.                     char [] buf = new char [15];
  53.                     int ile=0;
  54.                     int i;
  55.                     while((i=in.read())!=-1){
  56.                         if(ile<15){
  57.                         buf[ile]=(char)i;
  58.                         ile++;
  59.                         }
  60.                     }
  61.                     in.close();
  62.                     buf[ile]='\0';
  63.                     buf[ile+1]='\0';
  64.                     String text=String.valueOf(buf);
  65.                     ((EditText)findViewById(R.id.EditText01)).setText(text);
  66.                 } catch (FileNotFoundException e) {
  67.                     // TODO Auto-generated catch block
  68.                     e.printStackTrace();
  69.                 } catch (IOException e) {
  70.                     // TODO Auto-generated catch block
  71.                     e.printStackTrace();
  72.                 }
  73.             }});
  74.        
  75.         findViewById(R.id.Button02).setOnClickListener(new OnClickListener() {
  76.            
  77.             @Override
  78.             public void onClick(View arg0) {
  79.                 FileOutputStream out;
  80.                
  81.                 try {
  82.                     out=openFileOutput("plik", 0);
  83.                     String tmp=((EditText)findViewById(R.id.EditText01)).getText().toString();
  84.                     out.write(tmp.getBytes());
  85.                     out.close();
  86.                 } catch (FileNotFoundException e) {
  87.                     // TODO Auto-generated catch block
  88.                     e.printStackTrace();
  89.                 } catch (IOException e) {
  90.                     // TODO Auto-generated catch block
  91.                     e.printStackTrace();
  92.                 }
  93.  
  94.             }
  95.         });
  96.        
  97.        
  98.         Spinner sp = (Spinner)findViewById(R.id.Spinner01);
  99.         final TextView tv = (TextView)findViewById(R.id.TextView01);
  100.         try
  101.         {
  102.         final Cursor c = this.managedQuery(People.CONTENT_URI,
  103.         new String[] {People.NAME,People.NUMBER},
  104.         null,
  105.         null,
  106.         null);
  107.         ArrayAdapter<String> arAd = new ArrayAdapter<String>(this,
  108.                 android.R.layout.simple_spinner_item);
  109.         arAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  110.         while(c.moveToNext())
  111.         {
  112.         //  arAd.add(c.getString(0)+" "+c.getString(1)+"\n");
  113.             arAd.add(c.getString(0)+"\n");
  114.         }
  115.        
  116.         sp.setAdapter(arAd);
  117.         sp.setOnItemSelectedListener(new OnItemSelectedListener() {
  118.  
  119.             @Override
  120.             public void onItemSelected(AdapterView<?> arg0, View arg1,
  121.                     int arg2, long arg3) {
  122.             c.moveToPosition(arg2);
  123.             tv.setText(c.getString(1));
  124.                
  125.             }
  126.  
  127.             @Override
  128.             public void onNothingSelected(AdapterView<?> arg0) {
  129.                 // TODO Auto-generated method stub
  130.                
  131.             }
  132.            
  133.            
  134.            
  135.         });
  136.         }catch (Exception e)
  137.         {
  138.         tv.setText(e.getMessage());
  139.         }
  140.  
  141.     }
  142.    
  143.     // Write the prefix to the SharedPreferences object for this widget
  144.     static void saveTitlePref(Context context, int i) {
  145.         SharedPreferences.Editor prefs = context.getSharedPreferences(WINDOW_SERVICE, MODE_PRIVATE).edit();
  146.         prefs.putInt(WINDOW_SERVICE, i);
  147.         prefs.commit();
  148.     }
  149.  
  150.     // Read the prefix from the SharedPreferences object for this widget.
  151.     // If there is no preference saved, get the default from a resource
  152.     static int loadTitlePref(Context context ){
  153.         SharedPreferences prefs = context.getSharedPreferences(WINDOW_SERVICE, MODE_PRIVATE);
  154.         int prefix =0;
  155.         prefix=prefs.getInt(WINDOW_SERVICE, MODE_PRIVATE);
  156.         return prefix;
  157.         }
  158.  
  159.        
  160.        
  161.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement