简体   繁体   中英

Dealing with Listeners

I know the listeners are supposed to make life easier, especially in a multi-tasking environment like android, but sometimes (to me) it just makes things harder.

Right now I have an ExpandableList activity which presents a list of events, and I want the user to select the group & child of interest and select a notification sound that will play when that event happens. So the list is set up and I have a setOnChildClickListener set up which runs my SetRingtone method:

protected void SetRingtone(int groupPosition, int childPosition) {
  Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER);
  intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE, 
    RingtoneManager.TYPE_ALL);
  intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE, 
    "Select Tone");
  startActivityForResult( intent, PICK_NOTIFICATION_SOUND); 
}

So that method has access to the selected group and child positions. The problem is that to get the ringtone selected from the ringtone selector, you have to set up another lister, onActivityResult:

protected void onActivityResult(int requestCode, 
    int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    Uri uri = 
     data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    if (uri != null) {
      String ringTonePath = uri.toString();
      ExpandableListView variableView = 
      (ExpandableListView)findViewById(android.R.id.list);
      int p = variableView.getSelectedItemPosition();
      Log.d("phca", "The selected item number was: " + 
      p + "; The sound selected is: " + uri );
    }
  }
}

And now I don't have access to the selected group and child. As you can see, I tried to get the position from the getSelectedItemPosition, but it always returns -1 here.

I know I am probably making this harder than it really is.

您可以在调用startActivityForResult之前将组和子级存储在实例变量中,然后在onActivityResult中使用这些实例变量。

Did you try to use variableView.getSelectedItem()? If this return null so it mean that something wrong with your list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM