pior que tá tudo igual
public class ListaAlunosActivity extends Activity{
private ListView lista;
private Aluno aluno;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listagem_alunos);
lista = (ListView) findViewById(R.id.lista);
registerForContextMenu(lista);
lista.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view,
int position, long id) {
//Toast.makeText(ListaAlunosActivity.this, "aluno clicado é: "+parent.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
aluno = (Aluno) adapter.getItemAtPosition(position);
return false;//true consumir o evento sozinho
}
});
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
//Toast.makeText(ListaAlunosActivity.this, "posição: "+position, Toast.LENGTH_SHORT).show();
Aluno alunoParaSerAlterado = (Aluno) adapter.getItemAtPosition(position);
Intent irParaOFormulario = new Intent(ListaAlunosActivity.this,FormularioActivity.class);
irParaOFormulario.putExtra("alunoSelecionado", alunoParaSerAlterado);
startActivity(irParaOFormulario);
}
});
}
public void onResume(){
super.onResume();
carregaLista();
}
public void carregaLista(){
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.getLista();
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1,alunos);
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_lista_alunos, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.novo:
Intent irParaFormulario = new Intent(this,FormularioActivity.class);
startActivity(irParaFormulario);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuItem ligar = menu.add("Ligar");
ligar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent irParaTelaDeDiscagem = new Intent(Intent.ACTION_CALL);
Uri telefoneDoAluno = Uri.parse("tel:"+aluno.getTelefone());
irParaTelaDeDiscagem.setData(telefoneDoAluno);
startActivity(irParaTelaDeDiscagem);
return false;
}
});
menu.add("Enviar Sms");
menu.add("Achar no Mapa");
MenuItem site = menu.add("Navegar no Site");
site.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent abrirOsiteDoAluno = new Intent(Intent.ACTION_VIEW);
Uri siteDoAluno= Uri.parse("http://"+aluno.getSite());
abrirOsiteDoAluno.setData(siteDoAluno);
startActivity(abrirOsiteDoAluno);
return false;
}
});
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
dao.deletar(aluno);
dao.close();
carregaLista();
return false;
}
});
menu.add("Enviar email");
super.onCreateContextMenu(menu, v, menuInfo);
}
}
public class FormularioActivity extends Activity{
private String caminhoArquivo;
private FormularioHelper helper;
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.formulario);
final Button botao = (Button) findViewById(R.id.botao);
helper = new FormularioHelper(this);
final Aluno alunoParaSerAlterado = (Aluno) getIntent().getSerializableExtra("alunoSelecionado");
if(alunoParaSerAlterado != null){
helper.colocaAlunoNoFormulario(alunoParaSerAlterado);
}
botao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Aluno aluno = helper.pegaAlunoDoFormulario();
AlunoDAO alunoDAO = new AlunoDAO(FormularioActivity.this);
if(alunoParaSerAlterado != null){
botao.setText("Alterar");
aluno.setId(alunoParaSerAlterado.getId());
alunoDAO.alterar(aluno);
}else{
alunoDAO.insere(aluno);
}
alunoDAO.close();
finish();
}
});
ImageView foto = helper.getFoto();
foto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent irParaCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
caminhoArquivo = getExternalFilesDir(null) + "/"+System.currentTimeMillis()+".png";
File arquivo = new File(caminhoArquivo);
Uri localFoto = Uri.fromFile(arquivo);
irParaCamera.putExtra(MediaStore.EXTRA_OUTPUT,localFoto);
startActivityForResult(irParaCamera,123);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 123){
if(resultCode == Activity.RESULT_OK){
helper.carregaImagem(caminhoArquivo);
}else{
caminhoArquivo = null;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
public class FormularioHelper {
private EditText campoNome;
private EditText campoSite;
private EditText campoEndereco;
private EditText campoTelefone;
private SeekBar campoNota;
private Aluno aluno;
private ImageView foto;
public FormularioHelper(FormularioActivity activity) {
aluno = new Aluno();
campoNome = (EditText) activity.findViewById(R.id.nome);
campoSite = (EditText) activity.findViewById(R.id.site);
campoEndereco = (EditText) activity.findViewById(R.id.endereco);
campoTelefone = (EditText) activity.findViewById(R.id.telefone);
campoNota = (SeekBar) activity.findViewById(R.id.nota);
foto =(ImageView) activity.findViewById(R.id.foto);
}
public Aluno pegaAlunoDoFormulario(){
String nome = campoNome.getText().toString();
String site = campoSite.getText().toString();
String endereco = campoEndereco.getText().toString();
String telefone = campoTelefone.getText().toString();
int nota = campoNota.getProgress();
aluno.setNome(nome);
aluno.setSite(site);
aluno.setEndereco(endereco);
aluno.setTelefone(telefone);
aluno.setNota(Double.valueOf(nota));
return aluno;
}
public void colocaAlunoNoFormulario(Aluno alunoAserAlterado) {
aluno = alunoAserAlterado;
campoNome.setText(aluno.getNome());
campoEndereco.setText(aluno.getEndereco());
campoSite.setText(aluno.getSite());
campoTelefone.setText(aluno.getTelefone());
campoNota.setProgress(aluno.getNota().intValue());
if(aluno.getCaminhoFoto() != null){
carregaImagem(aluno.getCaminhoFoto());
}
}
public ImageView getFoto() {
return foto;
}
public void carregaImagem(String caminhoArquivo) {
aluno.setCaminhoFoto(caminhoArquivo);
Bitmap imagem = BitmapFactory.decodeFile(caminhoArquivo);
Bitmap imagemReduzida = Bitmap.createScaledBitmap(imagem, 100, 100,true);
foto.setImageBitmap(imagemReduzida);
}
}
public class AlunoDAO extends SQLiteOpenHelper{
private static final String DATABASE = "NomeDoBanco";
private static final String TABELA = "Alunos";
private static final int VERSAO = 2;
public AlunoDAO(Context ctx){
super(ctx, DATABASE, null,VERSAO);
}
@Override
public void onCreate(SQLiteDatabase database) {
String sql = " CREATE TABLE "+ TABELA + " ("
+" id INTEGER PRIMARY KEY, "
+" nome TEXT INIQUE NOT NULL, "
+" telefone TEXT, "
+" endereco TEXT, "
+" site TEXT, "
+" nota REAL,"
+" caminhoFoto TEXT"
+" );";
database.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS "+ TABELA;
database.execSQL(sql);
onCreate(database);
}
public void insere(Aluno aluno) {
ContentValues cv = new ContentValues();
cv.put("nome", aluno.getNome());
cv.put("endereco", aluno.getEndereco());
cv.put("telefone", aluno.getTelefone());
cv.put("site", aluno.getSite());
cv.put("nota", aluno.getNota());
cv.put("caminhoFoto",aluno.getCaminhoFoto());
getWritableDatabase().insert(TABELA, null, cv);
}
public List<Aluno> getLista() {
List<Aluno> alunos = new ArrayList<Aluno>();
String sql = "SELECT * FROM "+ TABELA + " ;";
Cursor cursor = getReadableDatabase().rawQuery(sql, null);
while(cursor.moveToNext()){
Aluno aluno = new Aluno();
aluno.setId(cursor.getInt(cursor.getColumnIndex("id")));
aluno.setNome(cursor.getString(cursor.getColumnIndex("nome")));
aluno.setEndereco(cursor.getString(cursor.getColumnIndex("endereco")));
aluno.setSite(cursor.getString(cursor.getColumnIndex("site")));
aluno.setTelefone(cursor.getString(cursor.getColumnIndex("telefone")));
aluno.setCaminhoFoto(cursor.getString(cursor.getColumnIndex("caminhoFoto")));
aluno.setNota(cursor.getDouble(cursor.getColumnIndex("nota")));
alunos .add(aluno);
}
return alunos;
}
public void deletar(Aluno aluno) {
String args[]={String.valueOf(aluno.getId())};
getWritableDatabase().delete(TABELA, "id=?",args);
}
public void alterar(Aluno aluno) {
ContentValues cv = new ContentValues();
cv.put("nome", aluno.getNome());
cv.put("endereco", aluno.getEndereco());
cv.put("telefone", aluno.getTelefone());
cv.put("site", aluno.getSite());
cv.put("nota", aluno.getNota());
cv.put("caminhoFoto",aluno.getCaminhoFoto());
String[] args= new String[]{String.valueOf(aluno.getId())};
getWritableDatabase().update(TABELA,cv,"id=?", args);
}
}
//listagem de alunos
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lista"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</LinearLayout>
//formulario
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/foto"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:src="@drawable/ic_launcher" android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nome:" />
<EditText
android:id="@+id/nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Site:" />
<EditText
android:id="@+id/site"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Endereço:" />
<EditText
android:id="@+id/endereco"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telefone:" />
<EditText
android:id="@+id/telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nota:" />
<SeekBar android:id="@+id/nota" android:layout_height="wrap_content" android:layout_width="wrap_content" android:max="10"/>
<Button android:id="@+id/botao"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Salvar"/>
</LinearLayout>
//android manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.caelum.cadastrocaelum"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="br.com.caelum.cadastro.ListaAlunosActivity">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="br.com.caelum.cadastro.FormularioActivity" ></activity>
</application>
</manifest>