简体   繁体   English

与听众打交道

[英]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. 我知道听众应该让生活更轻松,尤其是在像Android这样的多任务环境中,但有时(对我而言)这只会使事情变得更难。

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. 现在,我有一个ExpandableList活动,它提供事件列表,并且我希望用户选择感兴趣的组和子对象,并选择在该事件发生时将播放的通知声音。 So the list is set up and I have a setOnChildClickListener set up which runs my SetRingtone method: 这样就建立了列表,并且我设置了一个setOnChildClickListener来运行我的SetRingtone方法:

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: 问题是要从铃声选择器中选择铃声,您必须设置另一个列表器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. 如您所见,我尝试从getSelectedItemPosition获取位置,但此处始终返回-1。

I know I am probably making this harder than it really is. 我知道我可能会比实际做起来困难。

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

Did you try to use variableView.getSelectedItem()? 您是否尝试使用variableView.getSelectedItem()? If this return null so it mean that something wrong with your list 如果此返回null,则表示您的列表有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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