How to click on a Listview item and open an Activity reference to the item I clicked on?

Asked

Viewed 726 times

3

I have a listview that has as content a list with country names recorded in the bank (Sqlite). I needed to click for example on Item Brazil, and open another Listview with the states of Brazil, I’ve done it without database, but with database I’m not getting because I need to pull from the database and not from the object, someone has some idea of how to do?

  • When you click on an item from ListView he takes the position, why not take the Brazil Name and do a search in the bank to fill your object again?

  • @daniel12345smith, please add the code of how you are filling this ListView will help a lot by doing this.

1 answer

1

There are numerous ways to achieve this goal. I will suggest an approach that I consider efficient.

The ideal would be to implement your own version of ** Simplecursoradapter**, register a callback responsible for executing the desired action and pass on to this callback some data relating to your record, preferably an ID.

In your callback you run the operation using the received reference, in case you open a new Activity. Receive the reference in Activity and make a new query to DB with the reference.

1 - Tutorial to implement Simplecursoradapter

2 - Implement an interface in your Adapter to pass the info to your callback

public interface MyListener {
    void setOnClickAdapter(final View view, final int myId );
}

3 - Register your system in the method bindView of your Adapter

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
   //....
   MyListener listener = (TaskListener)context;
   listener.setOnClickAdapter( myTextView, id );

4 - In your Activity used as context implement the Adapter interface

@Override
public void setOnClickAdapter( final View view, final int id)
{
   view.setOnClickListener( new View.OnClickListener )
        @Override
        public void onClick(View view) {
            // Execute sua operação
        }
}

Browser other questions tagged

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