public class ListaAlunosActivity extends AppCompatActivity {
private ListView listaAlunos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
//Carrega os dados
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);
//Chama Formulario Aluno
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 v) {
Intent intentValProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(intentValProFormulario);
}
});
registerForContextMenu(listaAlunos);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_lista_alunos, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_enviar_notas:
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
AlunoConverter conversor = new AlunoConverter();
String json = conversor.converteParaJSON(alunos);
WebClient client = new WebClient();
String resposta = client.post(json);
Toast.makeText(this, resposta, Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
carregaLista();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
final Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(info.position);
//Sub-menu LIGAR
MenuItem itemLigar = menu.add("Ligar");
itemLigar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
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;
}
});
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
dao.deleta(aluno);
dao.close();
carregaLista();
Toast.makeText(ListaAlunosActivity.this, "Aluno " + aluno.getNome() + " excluido", Toast.LENGTH_SHORT).show();
return false;
}
});
}