0
I am creating a project, but I am not able to open the item listView
. I am already inserting and editing, but I can only open the last created item. For example, I have 10 items in the list, but only the last one I create, regardless of which I click, always opens the same. If I delete the last created, only the previous one opens.
I would like to know how to open the clicked item with its content, because only when I click can I delete or edit. What I need to implement is listaUma
at the end of DAOideia
.
Listidea.class
public class ListaIdeia extends ActionBarActivity{
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_ideia);
this.listView = (ListView)findViewById(R.id.listView);
ArrayList<Ideia> lista = new DAOIdeia(this).buscar();
IdeiaAdapter ideiaAdapter = new IdeiaAdapter(this,lista);
this.listView.setAdapter(ideiaAdapter);
this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int p, long id) {
if(p < 200) {
Intent segundaActivity = new Intent(ListaIdeia.this, DetalhesIdeia.class);
startActivity(segundaActivity);
}
}
});
DAOIdeia.class
public class DAOIdeia {
private SQLiteDatabase db;
public DAOIdeia(Context context){
this.db = new SQLiteCore(context).getWritableDatabase();
}
public void insert(Ideia ideia){
ContentValues valores = new ContentValues();
valores.put("nome",ideia.getNome());
valores.put("descricao",ideia.getDescricao());
this.db.insert("ideia",null,valores);
}
public void update(Ideia ideia){
ContentValues valores = new ContentValues();
valores.put("nome",ideia.getNome());
valores.put("descricao",ideia.getDescricao());
this.db.update("ideia",valores,"_id = ?",new String[]{""+ideia.get_id()});
}
public void delete(Ideia ideia){
this.db.delete("ideia","_id = ?",new String[]{""+ideia.get_id()});
}
public ArrayList<Ideia> buscar(){
ArrayList<Ideia> lista = new ArrayList<>();
String[] colunas = new String[]{"_id","nome","descricao"};
Cursor cursor = this.db.query("ideia",colunas,null,null,null,null,"nome DESC");
if (cursor.getCount() >0){
cursor.moveToFirst();
do{
Ideia ideia = new Ideia();
ideia.set_id(cursor.getInt(0));
ideia.setNome(cursor.getString(1));
ideia.setDescricao(cursor.getString(2));
lista.add(ideia);
}while(cursor.moveToNext());
}
return lista;
}
public ArrayList<Ideia> listaUma(){
ArrayList<Ideia> lista = new ArrayList<>();
String[] colunas = new String[]{"_id","nome","descricao"};
Cursor cursor = this.db.query("ideia",colunas,null,null,null,null,"nome DESC");
if (cursor.getCount() >0) {
Ideia ideia = new Ideia();
ideia.set_id(cursor.getInt(0));
ideia.setNome(cursor.getString(1));
ideia.setDescricao(cursor.getString(2));
lista.add(ideia);
}
return lista;
}
Party that creates the ArrayList
public class DetalhesIdeia extends ActionBarActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_ideia);
this.listView = (ListView) findViewById(R.id.listView);
ArrayList<Ideia> lista = new DAOIdeia(this).listaUma();
IdeiaAdapterEdit IdeiaAdapterEdit = new IdeiaAdapterEdit(this, lista);
this.listView.setAdapter(IdeiaAdapterEdit);
}
}
I don’t understand your condition there
if(p < 200)
. What do you mean?– viana