Open clicked Listview item in a new Activity

Asked

Viewed 275 times

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?

1 answer

0

To solve this problem you need to create a condition to recognize the item that was clicked. See below how it would be:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

            /* o position representa a posição que o usuário clicou*/

            switch (position){
                // case clicar na posição 0, significa que e o primeiro item
                case 0:
                    // faça algo referente ao item 1 a este item aqui dentro

                    break;
                // case clicar na posição 1, significa que e o segundo item
                case 1:
                    // faça algo referente ao item 2 a este item aqui dentro

                    break;
                // case clicar na posição 2, significa que e o terceiro item
                case 2:
                    // faça algo referente ao item 3 a este item aqui dentro

                    break;
                // assim por diante ...
            }
        }
    });
  • Isn’t there a better way to do that? Since a list can have hundreds of items... I do not program on android, but I believe this way is "wrong"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.