Prévia do material em texto
1
UNIVERSIDADE VEIGA DE ALMEIDA
ANÁLISE E DESENVOLVIMENTO DE SISTEMAS
AVA II
PROGRAMAÇÃO PARA DISPOSITIVOS MÓVEIS
Guilherme Tel Souza Ferreira
Rio de Janeiro, Setembro de 2020
2
UNIVERSIDADE VEIGA DE ALMEIDA
Programação Para Dispositivos Móveis
Professor: Carlos A. Sicsú A. do Nascimento
Aluno: Guilherme Tel Souza Ferreira
Matricula: 20193300088
Rio de Janeiro, Setembro de 2020
3
Introdução
O presente projeto tem o objetivo de suprir as necessidade de uma lista de tarefas simples,
com a qual é possível realizar a inclusão, alteração e exclusão de uma tarefa, as tarefas são
armazenadas em banco de dados SQLite fornecido pelo ecossistema Android, portanto não há perda
de informação ao sair da aplicação. A visualização das tarefas é feita a partir de uma lista, na qual a
tarefa mais recente aparece sempre abaixo. Para esse projeto foi utilizado o modelo de 3 camadas
para a arquitetura da aplicação.
4
Código xml da activity_main da aplicação
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/lista"
android:layout_width="312dp"
android:layout_height="388dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></ListView>
<Button
android:id="@+id/btn_app_sair"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="sair"
android:text="@string/btn_app_sair"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_incluir_task"
app:layout_constraintTop_toBottomOf="@+id/lista" />
<TextView
android:id="@+id/txt_title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="@string/txt_title_list"
app:layout_constraintBottom_toTopOf="@+id/lista"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.457" />
<Button
android:id="@+id/btn_incluir_task"
5
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="incluirTarefa"
android:text="@string/btn_incluir_task"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lista" />
</androidx.constraintlayout.widget.ConstraintLayout>
6
Código xml da activity_tratar_tarefa da aplicação
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TratarTarefa">
<Button
android:id="@+id/btn_alterar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="84dp"
android:layout_marginBottom="16dp"
android:onClick="alterarInserir"
android:text="Alterar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title_person"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/input_task_locate"
android:layout_width="257dp"
android:layout_height="42dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title_locate" />
<EditText
android:id="@+id/input_task_priority"
android:layout_width="93dp"
android:layout_height="42dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toStartOf="@+id/input_task_locate"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title_priority" />
<TextView
android:id="@+id/txt_title_locate"
android:layout_width="63dp"
android:layout_height="38dp"
android:layout_marginTop="16dp"
7
android:layout_marginEnd="208dp"
android:layout_marginRight="208dp"
android:text="@string/txt_title_locate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_task_desription" />
<TextView
android:id="@+id/txt_title_priority"
android:layout_width="84dp"
android:layout_height="38dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="@string/txt_title_priority"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_task_desription" />
<TextView
android:id="@+id/txt_title_task"
android:layout_width="98dp"
android:layout_height="27dp"
android:text="@string/txt_title_task"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.076"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="16dp" />
<EditText
android:id="@+id/input_task_title"
android:layout_width="363dp"
android:layout_height="48dp"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title_task" />
<TextView
android:id="@+id/txt_title_description"android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginTop="16dp"
android:text="@string/txt_title_description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.077"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_task_title" />
<EditText
android:id="@+id/input_task_desription"
android:layout_width="370dp"
android:layout_height="74dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_title_description" />
8
<Button
android:id="@+id/btn_excluir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_marginBottom="16dp"
android:onClick="excluir"
android:text="Excluir"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_task_person"
app:layout_constraintVertical_bias="0.006" />
<Button
android:id="@+id/btn_voltar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:onClick="voltar"
android:text="Voltar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_task_person"
app:layout_constraintVertical_bias="0.719" />
<TextView
android:id="@+id/txt_title_person"
android:layout_width="93dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:text="@string/txt_title_person"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_task_priority" />
<EditText
android:id="@+id/input_task_person"
android:layout_width="252dp"
android:layout_height="48dp"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toEndOf="@+id/txt_title_person"
app:layout_constraintTop_toBottomOf="@+id/input_task_priority" />
</androidx.constraintlayout.widget.ConstraintLayout>
9
Aplicação em execução
Início do aplicativo e tela para inclusão da primeira tarefa
10
Primeira tarefa incluída na tela inicial e alteração da primeira tarefa
11
Primeira tarefa alterada e segunda tarefa adicionada
12
Código das Classes Java
Classe Tarefa_DAO
package com.example.reminderlist;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class Tarefa_DAO {
private SQLiteDatabase database;
private String [] columns = {
TarefaSQLiteOpenHelper.COLUNA_ID,
TarefaSQLiteOpenHelper.COLUNA_TITLE,
TarefaSQLiteOpenHelper.COLUNA_DESCRIPTION,
TarefaSQLiteOpenHelper.COLUNA_PRIORITY,
TarefaSQLiteOpenHelper.COLUNA_LOCATE,
TarefaSQLiteOpenHelper.COLUNA_PERSON
};
private TarefaSQLiteOpenHelper sqliteOpenHelper;
public Tarefa_DAO(Context context) {
sqliteOpenHelper = new TarefaSQLiteOpenHelper(context);
}
public void open() throws SQLException {
database = sqliteOpenHelper.getWritableDatabase();
}
public void close() {
sqliteOpenHelper.close();
}
public void inserir(String title, String description, int priority, String locate,
String person) {
ContentValues values = new ContentValues();
values.put(TarefaSQLiteOpenHelper.COLUNA_TITLE, title);
values.put(TarefaSQLiteOpenHelper.COLUNA_DESCRIPTION,description);
values.put(TarefaSQLiteOpenHelper.COLUNA_PRIORITY, String.valueOf(priority));
values.put(TarefaSQLiteOpenHelper.COLUNA_LOCATE, locate);
values.put(TarefaSQLiteOpenHelper.COLUNA_PERSON, person);
long insertId = database.insert(TarefaSQLiteOpenHelper.TABELA,null, values);
}
public void alterar(long id, String title, String description, int priority, String
locate, String person){
ContentValues values = new ContentValues ();
values.put(TarefaSQLiteOpenHelper.COLUNA_TITLE, title);
values.put(TarefaSQLiteOpenHelper.COLUNA_DESCRIPTION, description);
13
values.put(TarefaSQLiteOpenHelper.COLUNA_PRIORITY, String.valueOf(priority));
values.put(TarefaSQLiteOpenHelper.COLUNA_LOCATE, locate);
values.put(TarefaSQLiteOpenHelper.COLUNA_PERSON, person);
database.update(TarefaSQLiteOpenHelper.TABELA, values,
TarefaSQLiteOpenHelper.COLUNA_ID + "=" + id, null);
}
public void apagar (long id) {
database.delete (TarefaSQLiteOpenHelper.TABELA ,
TarefaSQLiteOpenHelper.COLUNA_ID
+ " = " + id , null );
}
public Tarefa buscar(long id) {
Cursor cursor = database.query(TarefaSQLiteOpenHelper.TABELA,
columns , TarefaSQLiteOpenHelper.COLUNA_ID + " = " + id,
null , null , null , null);
cursor.moveToFirst();
Tarefa tarefa = new Tarefa();
tarefa.setId(cursor.getLong(0));
tarefa.setTitle(cursor.getString(1));
tarefa.setDescription(cursor.getString(2));
tarefa.setPriority(cursor.getInt(3));
tarefa.setLocate(cursor.getString(4));
tarefa.setPerson(cursor.getString(5));
cursor.close();
return tarefa;
}
public List<Tarefa> getAll() {
List <Tarefa> tarefas = new ArrayList<Tarefa>();
Cursor cursor = database.query(TarefaSQLiteOpenHelper.TABELA,
columns,null ,null ,null ,null ,null);
cursor.moveToFirst ();
while (!cursor.isAfterLast()) {
Tarefa tarefa = new Tarefa();
tarefa.setId(cursor.getLong(0));
tarefa.setTitle(cursor.getString(1));
tarefa.setDescription(cursor.getString(2));
tarefa.setPriority(cursor.getInt(3));
tarefa.setLocate(cursor.getString(4));
tarefa.setPerson(cursor.getString(5));
tarefas.add(tarefa);
cursor.moveToNext();
}
cursor.close();
return tarefas;
}
}
14
Classe Tarefa
package com.example.reminderlist;
public class Tarefa {
private long id;
private String title;
private String description;
private int priority;
private String locate;
private String person;
public Tarefa() {
title = "Nome da Tarefa";
description = "Finalizar a AVA 2 da disciplina de Android";
priority = 1;
locate = "Portal do Aluno";
person = "Guilherme Tel";
}
public long getId() {
return id;
}
public void setId(longid) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
if (!title.isEmpty()) {
this.title = title;
}
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
if (!description.isEmpty()) {
this.description = description;
}
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
if (priority >= 0) {
this.priority = priority;
}
}
public String getLocate() {
15
return locate;
}
public void setLocate(String locate) {
if (!locate.isEmpty()) {
this.locate = locate;
}
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
if (!person.isEmpty()) {
this.person = person;
}
}
public String textoLista() {
String item;
item = getTitle();
item += "\nDescrição: " + getDescription();
item += "\nPrioridade: " + getPriority();
item += "\nLocal: " + getLocate();
item += "\nPessoa: " + getPerson();
return item;
}
}
16
Classe TarefaSQLiteOpenHelper
package com.example.reminderlist;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TarefaSQLiteOpenHelper extends SQLiteOpenHelper {
public static final String TABELA = " Tarefa ";
public static final String COLUNA_ID = " id ";
public static final String COLUNA_TITLE = " title ";
public static final String COLUNA_DESCRIPTION = " description ";
public static final String COLUNA_PRIORITY = " priority ";
public static final String COLUNA_LOCATE = " locate ";
public static final String COLUNA_PERSON = " person ";
private static final String DATABASE_NAME = "tarefas.db";
private static final int DATABASE_VERSION = 1;
private static final String CRIAR_BANCO = " create table "
+ TABELA + "("
+ COLUNA_ID + " integer primary key autoincrement , "
+ COLUNA_TITLE + " text not null , "
+ COLUNA_DESCRIPTION + " text not null, "
+ COLUNA_PRIORITY + " integer not null, "
+ COLUNA_LOCATE + " text not null, "
+ COLUNA_PERSON + " text not null);";
public TarefaSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL (CRIAR_BANCO);
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db. execSQL (" DROP TABLE IF EXISTS " + TABELA );
onCreate (db);
}
}
17
Classe TratarTarefa
package com.example.reminderlist;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TratarTarefa extends AppCompatActivity {
EditText input_task_title, input_task_desription, input_task_priority,
input_task_locate, input_task_person;
Button btn_alterar, btn_excluir;
private int acao;
private long id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tratar_tarefa);
btn_alterar = (Button) findViewById(R.id.btn_alterar);
btn_excluir = (Button) findViewById(R.id.btn_excluir);
input_task_title = (EditText) findViewById(R.id.input_task_title);
input_task_desription = (EditText) findViewById(R.id.input_task_desription);
input_task_priority = (EditText) findViewById(R.id.input_task_priority);
input_task_locate = (EditText) findViewById(R.id.input_task_locate);
input_task_person = (EditText) findViewById(R.id.input_task_person);
acao = getIntent().getExtras().getInt("acao");
id = getIntent().getExtras().getLong("id");
if (acao == -1) {
setTitle("Inserir Tarefa");
btn_alterar.setText("Incluir");
btn_excluir.setEnabled(false);
input_task_title.setText("Nome tarefa");
input_task_desription.setText("Descrição tarefa");
input_task_priority.setText("1");
input_task_locate.setText("Local da tarefa");
input_task_person.setText("Realizador da tarefa");
} else {
setTitle("Alterar ou Excluir Disciplina");
Tarefa aux = new Tarefa();
Tarefa_DAO dao = new Tarefa_DAO(this);
dao.open();
aux = dao.buscar(id);
input_task_title.setText(aux.getTitle());
input_task_desription.setText(aux.getDescription());
input_task_priority.setText(String.valueOf(aux.getPriority()));
input_task_locate.setText(aux.getLocate());
input_task_person.setText(aux.getPerson());
18
dao.close();
}
}
// Método para preparar os dados para retornar à atividade principal
public void alterarInserir(View v) {
int priority;
String title, description, locate, person;
title = input_task_title.getText().toString();
description = input_task_desription.getText().toString();
locate = input_task_locate.getText().toString();
person = input_task_person.getText().toString();
priority = Integer.parseInt(input_task_priority.getText().toString());
Tarefa_DAO dao = new Tarefa_DAO(this);
dao.open();
if (acao == -1) { dao.inserir(title, description, priority, locate, person); }
else{ dao.alterar(id, title, description, priority, locate, person); }
dao.close();
finish();
}
public void excluir(View v) {
if (acao == 0) {
Tarefa_DAO dao = new Tarefa_DAO(this);
dao.open();
dao.apagar(id);
dao.close();
}
finish();
}
public void voltar(View v) {
finish();
}
}
19
Classe MainActivity
package com.example.reminderlist;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.Iterator;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemClickListener {
ListView lista;
Intent intent;
public static final int ACTIVITY_REQUEST_TAREFA = 1;
private Tarefa_DAO dao ;
private String[] tarefas;
private long[] idTarefas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView) findViewById(R.id.lista);
setTitle("Gerenciador de Tarefas");
dao = new Tarefa_DAO(this);
dao.open();
lista.setOnItemClickListener(this);
}
@Override
protected void onResume(){
dao.open();
super.onResume();
List<Tarefa> listaTarefas = dao.getAll();
tarefas = new String[listaTarefas.size()];
idTarefas = new long[listaTarefas.size()];
int i =0;
Iterator<Tarefa> iterator = listaTarefas.iterator();
while (iterator.hasNext()) {
Tarefa aux = new Tarefa();
aux = (Tarefa) iterator.next();
tarefas[i]= aux.textoLista();
idTarefas[i] = aux.getId();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
20
android.R.layout.simple_list_item_1, tarefas);
lista.setAdapter(adapter);
}
@Override
protected void onPause(){
dao.close();
super.onPause();
}
public void onItemClick(AdapterView<?> parent, View view, int position, long
ident){
long id = idTarefas[position];
intent = new Intent(getApplicationContext(), TratarTarefa.class);
intent.putExtra("acao", 0);
intent.putExtra("id", id);
startActivity(intent);
}
public void incluirTarefa(View v){
intent = new Intent(getApplicationContext(), TratarTarefa.class);
intent.putExtra("acao", -1);
intent.putExtra("id", 0L);
startActivity(intent);
}
public void sair(View v){
finish();
}
}
21
Conclusão
O projeto avança não só nos conceitos da programação para o ecossistema Android, como
também reforça os princípios de estruturas de dados, orientação a objetos e sobretudo padrão de
projetos, que podem ser aplicados em grades equipes e projetos robustos, é possível observar que a
persistência de dados transforma a aplicação na medida que permite mais funcionalidade e
escalabilidade.
22
Bibliografia
https://developer.android.com/