Created
January 9, 2019 19:31
-
-
Save AlessandroLima66/2280b902cc59ef459ad5f99ac3bbe41c to your computer and use it in GitHub Desktop.
Classe ListaAlunosActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListaAlunosActivity extends AppCompatActivity { | |
private ListView listaAlunos; | |
private Aluno aluno; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_lista_alunos); | |
//Pegando Refência do XML | |
listaAlunos = (ListView) findViewById(R.id.lista_alunos); | |
listaAlunos.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> lista, View item, int position, long id) { | |
Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(position); | |
Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class); | |
intentVaiProFormulario.putExtra("aluno", aluno); | |
startActivity(intentVaiProFormulario); | |
} | |
}); | |
Button novoAluno = (Button) findViewById(R.id.novo_aluno); | |
novoAluno.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class); | |
startActivity(intentVaiProFormulario); | |
} | |
}); | |
registerForContextMenu(listaAlunos); | |
} | |
private void carregaLista() { | |
AlunoDAO dao = new AlunoDAO(this); | |
List<Aluno> alunos = dao.buscaAlunos(); | |
dao.close(); | |
//convertendo String para View | |
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos); | |
listaAlunos.setAdapter(adapter); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
carregaLista(); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
switch (requestCode) { | |
case 123: | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Intent intentLigar = new Intent(Intent.ACTION_CALL); | |
intentLigar.setData(Uri.parse("tel:" + aluno.getTelefone())); | |
startActivity(intentLigar); | |
} | |
} else { | |
Toast.makeText(ListaAlunosActivity.this, | |
"A permissão é necessária", | |
Toast.LENGTH_LONG).show(); | |
} | |
break; | |
} | |
} | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) { | |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; | |
aluno = (Aluno) listaAlunos.getItemAtPosition(info.position); | |
//CALL PHONE | |
MenuItem itemLigar = menu.add("Ligar"); | |
//ouvindo para pedir permissão | |
itemLigar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem menuItem) { | |
if (ActivityCompat.checkSelfPermission(ListaAlunosActivity.this, Manifest.permission.CALL_PHONE) | |
!= PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(ListaAlunosActivity.this, | |
new String[]{Manifest.permission.CALL_PHONE}, 123); | |
} else { | |
Intent intentLigar = new Intent(Intent.ACTION_CALL); | |
intentLigar.setData(Uri.parse("tel:" + aluno.getTelefone())); | |
startActivity(intentLigar); | |
} | |
return false; | |
} | |
}); | |
//SMS | |
MenuItem itemSMS = menu.add("Enviar SMS"); | |
Intent intentSMS = new Intent(Intent.ACTION_VIEW); | |
intentSMS.setData(Uri.parse("sms:" + aluno.getTelefone())); | |
itemSMS.setIntent(intentSMS); | |
//MAPA | |
MenuItem itemMapa = menu.add("Visualizar no mapa"); | |
Intent intentMapa = new Intent(Intent.ACTION_VIEW); | |
//Pegando o endereço com nivel de zoom | |
//intentMapa.setData(Uri.parse("geo:0,0?z=14&q=" + aluno.getEndereco())); | |
intentMapa.setData(Uri.parse("geo:0,0?q=" + aluno.getEndereco())); | |
itemMapa.setIntent(intentMapa); | |
//SITE | |
MenuItem itemSite = menu.add("Visitar Site"); | |
Intent intentSite = new Intent(Intent.ACTION_VIEW); | |
String site = aluno.getSite(); | |
if (!site.startsWith("http://")) { | |
site = "http://" + site; | |
} | |
intentSite.setData(Uri.parse(site)); | |
itemSite.setIntent(intentSite); | |
//DELETAR | |
MenuItem deletar = menu.add("Deletar"); | |
deletar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem menuItem) { | |
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this); | |
dao.deleta(aluno); | |
dao.close(); | |
carregaLista(); | |
return false; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment