public class AlunosAdapter extends BaseAdapter {
private final List<Aluno> alunos;
private final Context context;
public AlunosAdapter(Context context, List<Aluno> alunos) {
this.context = context;
this.alunos = alunos;
}
@Override
public int getCount() {
return alunos.size();
}
@Override
public Object getItem(int position) {
return alunos.get(position);
}
@Override
public long getItemId(int position) {
return alunos.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Aluno aluno = alunos.get(position);
LayoutInflater inflater = LayoutInflater.from(context);
View view = convertView;
if (view == null){
view = inflater.inflate(R.layout.list_item, parent, false);
}
TextView campoNome = (TextView) view.findViewById(R.id.item_nome);
campoNome.setText(aluno.getNome());
TextView campoTelefone = (TextView) view.findViewById(R.id.item_telefone);
campoTelefone.setText(aluno.getTelefone());
ImageView campoFoto = (ImageView) view.findViewById(R.id.item_foto);
String caminhoFoto = aluno.getCaminhoFoto();
if (caminhoFoto != null){
Bitmap bitmap = BitmapFactory.decodeFile(caminhoFoto);
Bitmap bitmapReduzido = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
campoFoto.setImageBitmap(bitmapReduzido);
campoFoto.setScaleType(ImageView.ScaleType.FIT_XY);
}
return view;
}
}
public class MainActivity extends AppCompatActivity {
private ListView listaAlunos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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(MainActivity.this, FormularioActivity.class);
intentVaiProFormulario.putExtra("aluno", aluno);
startActivity(intentVaiProFormulario);
}
});
Button novoAluno = (Button) findViewById(R.id.novoAluno);
novoAluno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentVaiProFormulario = new Intent(MainActivity.this, FormularioActivity.class);
startActivity(intentVaiProFormulario);
}
});
registerForContextMenu(listaAlunos);
}
private void carregaLista() {
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
AlunosAdapter adapter = new AlunosAdapter(this, alunos);
listaAlunos.setAdapter(adapter);
}
@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);
MenuItem itemLigar = menu.add("Ligar");
itemLigar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem iem){
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.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;
}
});
MenuItem itemSMS = menu.add("Enviar SMS");
Intent intentSMS = new Intent(Intent.ACTION_VIEW);
intentSMS.setData(Uri.parse("sms:" + aluno.getTelefone()));
itemSMS.setIntent(intentSMS);
MenuItem itemMapa = menu.add("Visualizar no mapa");
Intent intentMapa = new Intent((Intent.ACTION_VIEW));
intentMapa.setData(Uri.parse("geo:0,0?q=" + aluno.getEndereco()));
itemMapa.setIntent(intentMapa);
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);
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO dao = new AlunoDAO(MainActivity.this);
dao.deleta(aluno);
dao.close();
carregaLista();
return false;
}
});
}