How to click on a ListView item and open an Activity referring to the item I clicked on?

3

I have a listView that contains a list of countries named in the database (SQLite). I needed to click for example in Item Brazil, and open another ListView with the states of Brazil, I already did this without database, but with bank I can not because I need to pull the bank and not the object, does anyone have any idea how to do ?

    
asked by anonymous 20.07.2015 / 17:11

1 answer

1

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

The ideal would be to implement your own version of ** SimpleCursorAdapter **, register a callback responsible for performing the desired action and pass back to this callback some data pertaining to your record, preferably an ID.

In your callback you perform the operation using the reference you received, in case you open a new activity. Get the reference in activity and make a new reference to DB with the reference.

1 - Tutorial to implement SimpleCursorAdapter

2 - Implement an interface on your adapter to pass info to your callback

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

3 - Register your listener on the bindView method 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
        }
}
    
21.07.2015 / 04:36