Get item data in ListView

0

I'm developing a college project that consists of a schedule that works with SQLite database. It may sound trivial, but the way I'm doing it makes things a bit more complex.

When I click on a list item, the user is directed to another activity with a form, whose fields will already be loaded with the data shown in the clicked ListView. I already tried to do getting the position of the item and using as ID to be returned, but I would have to do two operations, one to read in the database and one to update. If there is a way to get the data from the listview item and send it to another activity directly after the click, I would just do one action, which would be the update. So how can I do this that I plan?

    
asked by anonymous 10.06.2014 / 01:12

1 answer

0

Try this. In your activity you put:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Object objetoComDados = adaptador.getItem(position);
                    Intent intent = new Intent(activity, ActivityQueRecebeOsDados.class);
                    intent.putExtra("dados",objetoComDados);
                    activity.startActivity(intent);
        }
    });
}

And on your adapter you put it like this:

public class Adaptador extends BaseAdapter{
    @Override
    public Object getItem(int position) {
        return listaDeObjetos.get(position);
    }
}

And to get the data in the activity you do it like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if( getIntent().hasExtra("dados");){              
       Object objectoComDados  = getIntent().getSerializableExtra("dados");

}

Remembering that the 'objectData' class has to implement the 'Serializable' interface.

    
11.06.2014 / 19:12