4

Here is a good tutorial in Android documentation with source code explaining GestureListview. But I cannot find how to get the list item position in onGesturePerformed(GestureOverlayView overlay, Gesture gesture) method?

Please see the comment in the code with a ? mark to understand my query. Thank you.

public class GesturesListActivity extends ListActivity implements OnGesturePerformedListener {
private GestureLibrary mLibrary;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Populate the activity with the names of our contacts
    Cursor query = managedQuery(Contacts.People.CONTENT_URI,
            new String[] { Contacts.People._ID, Contacts.People.DISPLAY_NAME },
            null, null, Contacts.People.DEFAULT_SORT_ORDER);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1, query,
            new String[] { Contacts.People.DISPLAY_NAME },
            new int[] { android.R.id.text1 });

    setListAdapter(adapter);

    mLibrary = GestureLibraries.fromRawResource(this, R.raw.actions);
    if (!mLibrary.load()) {
        finish();
    }

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
    if (predictions.size() > 0) {
        if (predictions.get(0).score > 1.0) {
            String action = predictions.get(0).name;
            if ("action_add".equals(action)) {
                Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();                
            } else if ("action_delete".equals(action)) {
                Toast.makeText(this, "Removing a contact", Toast.LENGTH_SHORT).show();




                //How to get the specific position in the list to remove the contact on which the gesture event took place?




            } else if ("action_refresh".equals(action)) {
                Toast.makeText(this, "Reloading contacts", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

}

Imon
  • 3,905
  • 4
  • 25
  • 25

1 Answers1

1

We can override the protected void onListItemClick(ListView l, View v, int position, long id)

so that this will get the view of the list item that you have clicked

Ganesh
  • 11
  • 1
  • But I need it not when the item is clicked, rather I need the position when the user slides his finger on the item in a predefined direction. onListItemClick will have a different functionality - like it will show the detailed contact. And sliding finger in left direction will remove the contact. – Imon May 04 '12 at 12:58