Get item data in Listview

Asked

Viewed 1,222 times

0

I’m developing a college project that consists of an agenda that works with Sqlite database. It may seem trivial, but the way I’m proceeding makes things a little more complex.

I want that, when clicking 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 taking 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 take the data from the listview item and send it to another Activity directly after the click, I would do only one action, which would be the update. So how can I do this thing that I plan?

  • 2

    It would be good to add what you have already done. So it facilitates in building the answer.

1 answer

0


Try it this way. On 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 from Activity you do so:

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

}

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

Browser other questions tagged

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