<repo_name>RotcivOcnarb/Arqdesis-Aula15<file_sep>/app/src/main/java/br/usjt/arqdesis/cliente/ListaClientesActivity.java
package br.usjt.arqdesis.cliente;

import android.app.Activity;
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.ArrayList;

public class ListaClientesActivity extends Activity {

    public static final String ID = "br.usjt.arqdesis.cliente.id";
    ArrayList<String> lista;
    Activity atividade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_clientes);
        atividade = this;
        Intent intent = getIntent();
        String chave = intent.getStringExtra(MainActivity.CHAVE);
        lista = buscaClientes(chave);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                Intent intent = new Intent(atividade, DetalheClienteActivity.class);
                intent.putExtra(ID, ListaDados.getInstance().getIdOf(lista.get(position)));
                startActivity(intent);
            }

        });
    }

    public ArrayList<String> buscaClientes(String chave){

        ArrayList<String> lista = ListaDados.getInstance().getLista();
        if(chave == null || chave.length() == 0){
            return lista;
        }
        else{
            ArrayList<String> subLista = new ArrayList<String>();
            for(String nome : lista){
                if(nome.toUpperCase().contains(chave.toUpperCase())){
                    subLista.add(nome);
                }
            }
            return subLista;
        }

    }

}
<file_sep>/app/src/main/java/br/usjt/arqdesis/cliente/DetalheClienteActivity.java
package br.usjt.arqdesis.cliente;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class DetalheClienteActivity extends Activity {

    int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detalhe_cliente);

        TextView nome = (TextView) findViewById(R.id.nome_selecionado);
        Intent intent = getIntent();

        position =  intent.getIntExtra(ListaClientesActivity.ID, -1);

        nome.setText(
                ListaDados.getInstance().getLista().get(position)
        );
    }

    boolean editing = false;

    public void editarNome(View view){
        editing = !editing;

        if(editing){

            Button botao = (Button) findViewById(R.id.botao_editar);
            botao.setText("Salvar");

            EditText nome = (EditText) findViewById(R.id.nome_selecionado);
            nome.setEnabled(true);

        }
        else{
            Button botao = (Button) findViewById(R.id.botao_editar);
            botao.setText("Editar");

            EditText nome = (EditText) findViewById(R.id.nome_selecionado);

            ListaDados.getInstance().getLista().set(position, nome.getText().toString());

            nome.setEnabled(false);
        }
    }

    public void excluirCliente(View view){
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Excluir");
        alertDialog.setMessage("Deseja mesmo excluir " + ListaDados.getInstance().getLista().get(position) + "?");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "SIM",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ListaDados.getInstance().getLista().remove(position);

                        Intent intent = new Intent(DetalheClienteActivity.this, MainActivity.class);
                        startActivity(intent);

                    }
                });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "NÃO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();

    }
}
