Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pack.OM1;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.res.Resources;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.Contacts.People;
- //import android.view.ContextMenu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ArrayAdapter;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.TextView;
- public class OM1Activity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- int x=loadTitlePref(this);
- x++;
- saveTitlePref(this, x);
- setContentView(R.layout.main);
- ((TextView)findViewById(R.id.TextView01)).setText(String.valueOf(x));
- findViewById(R.id.Button01).setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- FileInputStream in;
- try {
- in = openFileInput("plik");
- char [] buf = new char [15];
- int ile=0;
- int i;
- while((i=in.read())!=-1){
- if(ile<15){
- buf[ile]=(char)i;
- ile++;
- }
- }
- in.close();
- buf[ile]='\0';
- buf[ile+1]='\0';
- String text=String.valueOf(buf);
- ((EditText)findViewById(R.id.EditText01)).setText(text);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }});
- findViewById(R.id.Button02).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- FileOutputStream out;
- try {
- out=openFileOutput("plik", 0);
- String tmp=((EditText)findViewById(R.id.EditText01)).getText().toString();
- out.write(tmp.getBytes());
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- Spinner sp = (Spinner)findViewById(R.id.Spinner01);
- final TextView tv = (TextView)findViewById(R.id.TextView01);
- try
- {
- final Cursor c = this.managedQuery(People.CONTENT_URI,
- new String[] {People.NAME,People.NUMBER},
- null,
- null,
- null);
- ArrayAdapter<String> arAd = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item);
- arAd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- while(c.moveToNext())
- {
- // arAd.add(c.getString(0)+" "+c.getString(1)+"\n");
- arAd.add(c.getString(0)+"\n");
- }
- sp.setAdapter(arAd);
- sp.setOnItemSelectedListener(new OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- c.moveToPosition(arg2);
- tv.setText(c.getString(1));
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- }catch (Exception e)
- {
- tv.setText(e.getMessage());
- }
- }
- // Write the prefix to the SharedPreferences object for this widget
- static void saveTitlePref(Context context, int i) {
- SharedPreferences.Editor prefs = context.getSharedPreferences(WINDOW_SERVICE, MODE_PRIVATE).edit();
- prefs.putInt(WINDOW_SERVICE, i);
- prefs.commit();
- }
- // Read the prefix from the SharedPreferences object for this widget.
- // If there is no preference saved, get the default from a resource
- static int loadTitlePref(Context context ){
- SharedPreferences prefs = context.getSharedPreferences(WINDOW_SERVICE, MODE_PRIVATE);
- int prefix =0;
- prefix=prefs.getInt(WINDOW_SERVICE, MODE_PRIVATE);
- return prefix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement