PROIECTE

Easy:

  • Calculator
  • Convertor unitati de masura
  • Hidden Objects
  • Gaseste diferentele
  • Brain shift
  • Life Expectancy Quiz - propus de Marius
  • Logo Quiz
  • Razboi (carti)

Medium:

  • X si Zero
  • Spanzuratoarea
  • Memory Game
  • Sudoku (fara generare) - il face Daria
  • Viteza de reactie - il face Ionut
  • Quiz Vrei sa fii milionar? - il face Andrei

Stocare Date

Exemplu

MainActivity xml:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/tv1"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ce trimit in activitatea urmatoare:" />
 
     <TextView
        android:id="@+id/tv2"
        android:layout_below="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
 
     <Button
         android:id="@+id/button2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/tv2"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="44dp"
         android:text="Random" />
 
     <Button
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/button2"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="18dp"
         android:text="New Activity" />
 
</RelativeLayout>

SecondActivity xml:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity" >
 
    <ListView 
	    android:id="@android:id/list"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content" />
 
</RelativeLayout>

MainActivity java:

package com.example.leaderboard;
 
import java.util.ArrayList;
import java.util.Random;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
	private TextView t;
	private Button b;
 
	private String nume;
	private Integer scor;
	private Button random;
 
	ArrayList<String> names;
	ArrayList<Integer> scores;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		t = (TextView) findViewById(R.id.tv2);
		b = (Button) findViewById(R.id.button1);
		random = (Button) findViewById(R.id.button2);
 
		names = new ArrayList<String>();
		scores = new ArrayList<Integer>();
 
		names.add("Diana"); names.add("Alexandra"); names.add("Iulia"); names.add("Stefan");
		scores.add(7); scores.add(8); scores.add(9); scores.add(10);
 
		random.setOnClickListener(new OnClickListener() { 
		       @Override
		        public void onClick(View v) {
			    	Random rand = new Random();
			   		int poz_nume = rand.nextInt(4);
			   		int poz_scor = rand.nextInt(4);
 
			   		nume = names.get(poz_nume);
			   		scor = scores.get(poz_scor);
 
			   		t.setText(nume + " " + scor.toString());
			       }
		       });
 
		b.setOnClickListener(new OnClickListener() { 
	       @Override
	        public void onClick(View v) {
		       Intent i = new Intent(MainActivity.this, SecondActivity.class);
		       i.putExtra("nume", nume);
		       i.putExtra("scor", scor);
		       startActivity(i);
		       }
	       });		
	}
}

SecondActivity java:

package com.example.leaderboard;
 
import java.util.ArrayList;
 
import android.os.Bundle;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
public class SecondActivity extends ListActivity {
 
	public static final String PREFS_NAME = "MyPrefsFile";
	static ArrayList<String> castigatori = new ArrayList<String>();
	ArrayAdapter<String> adapter;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
 
		// Restore preferences
	     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	     String string_castigatori = settings.getString("string", null);
	     if(string_castigatori != null && !string_castigatori.equals(""))
	     {
	    	 System.out.println("String:" + string_castigatori);
	     	parseString(string_castigatori);
	     }
 
		 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, castigatori);
 
		//Primire date din activitatea precedenta
		Bundle b = new Bundle();
		b = getIntent().getExtras();
		String nume = b.getString("nume");
		Integer scor = b.getInt("scor");
 
		String castigator = nume + " " + scor.toString();
		castigatori.add(castigator);
 
		setListAdapter(adapter);
 
	}
 
	@Override
	    protected void onStop(){
	       super.onStop();
 
	       String string_castigatori = createString();
 
	      // We need an Editor object to make preference changes.
	      // All objects are from android.context.Context
	      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	      SharedPreferences.Editor editor = settings.edit();
	      editor.putString("string", string_castigatori);
 
	      // Commit the edits!
	      editor.commit();
	    }
 
	private String createString() {
		// TODO Auto-generated method stub
		String string_castigatori = "";
		ArrayList<String> castig = new ArrayList<String>();
		for(int i = 0; i<castigatori.size();i++)
			castig.add(castigatori.get(i));
		while(!castig.isEmpty())
		{
			string_castigatori += castig.get(0);
			string_castigatori += "/";
			castig.remove(0);
 
		}
		return string_castigatori;
	}
 
	 private void parseString(String string_castigatori) {
		// TODO Auto-generated method stub
		 castigatori.clear();
		String[] elemente = string_castigatori.split("/");
		for(int i = 0; i < elemente.length; i++)
		{
			System.out.println("element:" + elemente[i]);
			castigatori.add(elemente[i]);
		}
	}
}
introducere_android/curs/curs6.txt · Ultima modificare: 2013/08/13 10:31 de către iulia.paraicu
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0