FACULDADE DE TECNOLOGIA SENAC PELOTAS Curso Superior de Tecnologia em Análise e Desenvolvimento de Sistemas Tópicos Avançados em ADS – Edécio Fernando Iepsen ============================== Exemplo Adapters / Spinners <?xml version="1.0" encoding="utf-8"?> <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="br.com.edecio.spinnercidades.MainActivity"> <TextView android:id="@+id/txtCidade" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/selecione_a_cidade" /> <Spinner android:id="@+id/spnCidade" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/txtCidade"></Spinner> </RelativeLayout> package br.com.edecio.spinnercidades; import import import import import import import android.support.v7.app.AppCompatActivity; android.os.Bundle; android.view.View; android.widget.AdapterView; android.widget.ArrayAdapter; android.widget.Spinner; android.widget.Toast; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { private ArrayAdapter<String> adpCidades; private Spinner spnCidade; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spnCidade = (Spinner) findViewById(R.id.spnCidade); adpCidades = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item); adpCidades.add("Pelotas"); adpCidades.add("Rio Grande"); adpCidades.add("Porto Alegre"); adpCidades.add("Caxias do Sul"); spnCidade.setAdapter(adpCidades); adpCidades.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnCidade.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String cidade = adpCidades.getItem(i); Toast.makeText(this, "Cidade Selecionada: " + cidade, Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } } ============================== Exemplo Uso de Threads com AsyncTask <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="br.com.edecio.contagemregressiva.MainActivity"> <Button android:id="@+id/btnContar" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/iniciar_contagem"/> <TextView android:id="@+id/txtNumero" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:textStyle="bold" android:text="0"/> </LinearLayout> package br.com.edecio.contagemregressiva; import import import import import android.support.v7.app.AppCompatActivity; android.os.Bundle; android.view.View; android.widget.Button; android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnContar; private TextView txtNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnContar = (Button) findViewById(R.id.btnContar); txtNum = (TextView) findViewById(R.id.txtNumero); btnContar.setOnClickListener(this); } @Override public void onClick(View view) { Conta task = new Conta(btnContar, txtNum); task.execute(10); } } package br.com.edecio.contagemregressiva; import import import import android.os.AsyncTask; android.os.SystemClock; android.widget.Button; android.widget.TextView; public class Conta extends AsyncTask<Integer, Integer, Void> { private Button btn; private TextView txt; public Conta(Button button, TextView textview) { this.btn = button; this.txt = textview; } @Override protected void onPreExecute() { btn.setEnabled(false); } @Override protected void onPostExecute(Void aVoid) { btn.setEnabled(true); } @Override protected void onProgressUpdate(Integer... values) { int n = values[0]; txt.setText(String.valueOf(n)); } @Override protected Void doInBackground(Integer... integers) { int limite = integers[0]; for (int i=1; i<=limite; i++) { SystemClock.sleep(500); publishProgress(i); } return null; } } ============================== Exemplo Manipulação de Arquivos Texto na Memória Interna <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="br.com.edecio.listadecompras.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/informe_o_produto" /> <EditText android:id="@+id/edtProduto" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnAdiciona" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/adicionar_item" android:onClick="Adiciona"/> <Button android:id="@+id/btnLista" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/listar_itens" android:onClick="Lista"/> <EditText android:id="@+id/edtLista" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> package br.com.edecio.listadecompras; import import import import import import android.support.v7.app.AppCompatActivity; android.os.Bundle; android.view.View; android.widget.Button; android.widget.EditText; android.widget.Toast; import import import import import import java.io.FileInputStream; java.io.FileNotFoundException; java.io.FileOutputStream; java.io.IOException; java.io.PrintWriter; java.util.Scanner; public class MainActivity extends AppCompatActivity { private EditText edtProduto; private EditText edtLista; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtProduto = (EditText) findViewById(R.id.edtProduto); edtLista = (EditText) findViewById(R.id.edtLista); } public void Adiciona(View view) throws FileNotFoundException { String produto = edtProduto.getText().toString(); try { FileOutputStream fs = openFileOutput("compras.txt", MODE_APPEND); PrintWriter pw = new PrintWriter(fs); pw.println(produto); pw.close(); Toast.makeText(this, "Ok! Produto Cadastrado", Toast.LENGTH_SHORT).show(); edtProduto.setText(""); edtProduto.requestFocus(); } catch (IOException e) { Toast.makeText(this, "Erro..." + e.getMessage(), Toast.LENGTH_SHORT).show(); } } public void Lista(View view) throws FileNotFoundException { try { FileInputStream fs = openFileInput("compras.txt"); Scanner entrada = new Scanner(fs); StringBuilder linhas = new StringBuilder(); while (entrada.hasNextLine()) { String linha = entrada.nextLine(); linhas.append(linha + System.lineSeparator()); } edtLista.setText(linhas); } catch (IOException e) { Toast.makeText(this, "Erro..." + e.getMessage(), Toast.LENGTH_SHORT).show(); } } }