在嵌入在fragment中的AdapterView.OnItemClickListener(){}中传递活动的上下文 - 该方法未定义

时间:2014-09-21 11:43:49

标签: android android-activity android-fragments

我在传递此方法中的活动上下文时遇到问题

        private void registerClicksOnParents(ListView listView, final Dialog d, final   
        ImageButton btn, final ChildAndActivityDataClass currentChild) {
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                d.dismiss();
                btn.setImageResource(R.drawable.odebrany);
                btn.setEnabled(false);          
                currentChild.setOdbiorDzieckaIdOfState(App_class.odebrane);
                SingleInfoToServer singleInfoToServer = SingleInfoToServer(currentChild.getIdOfChild(), 
                                                                           currentChild.getOdbiorDzieckaIdOfActivity(), 
                                                                           currentChild.getOdbiorDzieckaIdOfState(),
                                                                           context);
            }               
        });


    }   

当我在onItemClick中调用此方法时,我无法使用getActivity,我尝试使用MainScreen.this,但无效。我尝试以这种方式在Fragment onCreate方法中获取上下文:

public class Aktywnosci_Fragment extends Fragment {

Context context ;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    context = getActivity();

}

但它仍显示相同的The method SingleInfoToServer(int, int, int, Context) is undefined for the type new AdapterView.OnItemClickListener(){}

此方法构造函数如下所示

public SingleInfoToServer( int idOfChild,  int idOfActivity, int idOfState, Context context) 
{
    this.context = context;     
    this.idOfChild = idOfChild;
    this.idOfActivity = idOfActivity;
    this.idOfState = idOfState;
}

所以一切都应该有效

正如你在评论中所说的那样编辑我用这个

private void registerClicksOnParents(ListView listView,final Dialog d,final ImageButton btn,final ChildAndActivityDataClass currentChild,final Activity activity){             listView.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                d.dismiss();
                btn.setImageResource(R.drawable.odebrany);
                btn.setEnabled(false);          
                currentChild.setOdbiorDzieckaIdOfState(App_class.odebrane);
                SingleInfoToServer singleInfoToServer = SingleInfoToServer(currentChild.getIdOfChild(), 
                                                                           currentChild.getOdbiorDzieckaIdOfActivity(), 
                                                                           currentChild.getOdbiorDzieckaIdOfState(),
                                                                           view.getContext());

            }       

    }       );  
        }   

并且没有任何变化

2 个答案:

答案 0 :(得分:2)

  

当我在onItemClick中调用此方法时,我无法使用getActivity,我   尝试使用,MainScreen.this但没有任何作用

您可以使用view.getContext()中的onItemClick来获取内容参考,而不是存储上下文参考。

要创建SingleInfoToServer对象,请使用new关键字:

SingleInfoToServer singleInfoToServer = new SingleInfoToServer(
                                                 currentChild.getIdOfChild(),
                                  currentChild.getOdbiorDzieckaIdOfActivity(), 
                  currentChild.getOdbiorDzieckaIdOfState(), view.getContext());

答案 1 :(得分:0)

您必须使用Activity对象捕获getActivity()响应。然后从活动中你可以获得该背景。