如何将数据从类传递到活动

时间:2019-05-24 10:18:05

标签: java android

我有一个带有卡片布局的viewpager,并希望发送用户单击以打开特定活动页面的页面位置。我想将位置信息从适配器类发送到我的活动,但是我收到一个空指针异常错误。

我尝试在这些代码中使用处理程序和自定义接口。

我的自定义界面

package com.example.authentication.Interface;

public interface MyCustomInterface {
    public void sendData (int pos);
}

我在Activity中的sendData函数

@Override
public void sendData(final int pos) {

    Log.i("TAG", String.valueOf(pos));

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {

            switch (pos) {

                case 0:
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
                case 1:
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
                case 2:
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
                case 3:
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
            }

        }
    });
}

我的适配器类onClickListener

view.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            //this will log the page number that was click
            Log.i("TAG", "This page was clicked: " + position);
            myCustomInterface.sendData(position);
        }
    });

错误

  

java.lang.NullPointerException:尝试调用接口方法   '无效   com.example.authentication.Interface.MyCustomInterface.sendData(java.lang.String)'   在一个空对象引用上   com.example.authentication.ListAdapter $ 1.onClick(ListAdapter.java:63)

3 个答案:

答案 0 :(得分:0)

在您的适配器类中添加以下内容:

private MyCustomInterface mCustomInterface;

然后在适配器类的adpater构造函数中:

public MyAdapter(Context context,ArrayList<SomeData> data, mCustomInterface) {
this.context=context;    
this.mCustomInterface= mCustomInterface;
this.dataSet = data;
} 

然后在创建适配器实例时在您的活动类中:

MyAdapter mAdapter= new MyAdapter(context,data, new MyCustomInterface() {
                @Override
                public void sendData (int position) {

                   //do whatever you want here  
                }

            });

希望有帮助!

答案 1 :(得分:0)

  1. 在您的活动中实现界面
  2. 使用构造函数将接口对象传递给Adapter。

MyAdapter adapter = new MyAdapter(MainActivity.this,list,this);

现在在Adapter类中 使用上面由Ahmad讨论的构造函数。 公共MyAdapter(上下文ctx,List <>列表,MyCustomInterface接口){

}

3。使用侦听器对象调用接口函数 interface.sendData(position);

答案 2 :(得分:0)

据我了解,您有一个viewpager,并且您想将已单击的位置发送到另一个活动,对不对? 这样,您可以使用SharedPreferences来将位置保存在sharedpreferance中,并稍后在any活动的sendData add中使用它:

SharedPreferences pref;
      switch (pos) {

                case 0: 
            pref=getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=pref.edit();
            editor.putInt("POS",0);
            editor.commit();
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
                case 1:
            pref=getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=pref.edit();
            editor.putInt("POS",1);
            editor.commit();
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
                case 2:
            pref=getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=pref.edit();
            editor.putInt("POS",0);
            editor.commit();
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
                case 3:
            pref=getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=pref.edit();
            editor.putInt("POS",0);
            editor.commit();
                    intent.putExtra("Code", "MN");
                    startActivity(intent);
                    break;
            }

然后,在任何要添加的活动中:

        SharedPreferences prefs=getContext().getSharedPreferences("MyPrefs", 
            Context.MODE_PRIVATE);
        int i=prefs.getInt("POS",1);

并根据需要使用“ i” ...