开发者

Dealing with Listeners

开发者 https://www.devze.com 2023-04-09 13:22 出处:网络
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.

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 e开发者_开发问答vents, 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.


You could just store the group and child in instance variables before the call to startActivityForResult and then use these instance variables in onActivityResult.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消