-1

Here is my activity code

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_engineer_recycler );
    String value = "hello";
    }

Here is my class file :

public class complaintAdapter{
 //code here
} 

Now can anyone help me how pass that "hello" from activity to class

Nikhil Lohar
  • 127
  • 1
  • 9

4 Answers4

1

There could be two solutions to this.

Solution 1

Using data from Activity during object creation like this.

Create a constructor in the class that could take the desired data like this

public class complaintAdapter{
private String _value;
    public complaintAdapter(String value){
        _value = value;
        // use _value
    }
}

This can be used in the activity like this.

Create a method in the class and call it using the data as a parameter.

String value = "hello";
complaintAdapter complaintAdapterObj = new complaintAdapter(value);

Solution 2

Using data from the activity in the method call like this.

Create a method in the class and call it using the data as a parameter.

public class complaintAdapter{
    private String _value;
    public void sendData(String value){
        _value = value;
        // use _value
    }
}

This can be used in the activity like this

String value = "hello";
complaintAdapter complaintAdapterObj = new complaintAdapter();
complaintAdapterObj.sendData(value);
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

Change it in this way:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_engineer_recycler );
    String value = "hello";
    ComplaintAdapter adapter = new ComplaintAdapter(value);

    //access to value
    adapter.getValue();  
}

and change Class in this way:

public class ComplaintAdapter{
     private String value;

     public ComplaintAdapter(String value){
        this.value = value;
     }

     public String getValue(){
        return this.value;
     }
} 
Mahdi Rajabi
  • 582
  • 1
  • 4
  • 11
  • In ComplaintAdapter Class how to access the value outside this subclass – Nikhil Lohar Sep 18 '18 at 16:10
  • You have reference in onCreate method called adapter. you can add setter and getter method to ComplaintAdapter, then use them like adapter.getValue() which returns the value – Mahdi Rajabi Sep 19 '18 at 13:28
0

To pass any value to class you should create an appropriate constructor. In this case you should do like this:

public class complaintAdapter {

  private String value;

  public complaintAdapter(String value) {
    this.value = value;
  }
}

Then you create an instance in your activity:

complaintAdapter adapter = new complaintAdapter(value);

P.S. As Java Code conventions rule says - any java class name should start with uppercase symbol so you better rename your class to ComplaintAdapter

David
  • 351
  • 3
  • 11
0

Create a constructor in your ComplaintAdapter class

public class ComplaintAdapter{
 private String value;

 public ComplaintAdapter(String value){
    this.value = value;
 }
}

Then when you create the object of this class pass the value to constructor

ComplaintAdapter adapter = new ComplaintAdapter(value); 
AIK
  • 498
  • 2
  • 6
  • 20