Reputation: 1922
I want to pass string data from my Activity class to a class which is a TCPClient class. So basically, this string contains the IP address the user inputs from my Activity class view. And then in return I need to pass the IP address to TCPClient class.
Below are the codes that I'm working on.
MainActivity.class:
String add1 = etIPAddress1.getText().toString();
String add2 = etIPAddress2.getText().toString();
String add3 = etIPAddress3.getText().toString();
String add4 = etIPAddress4.getText().toString();
String new_IPAddress = add1 + "." + add2 + "." + add3 + "." + add4;
Log.d("E", new_IPAddress);
TCPClient.class:
public class TCPClient {
private String serverMessage;
//public static final String SERVERIP = "192.168.254.105"; //your computer IP address
public static final String SERVERIP = "";
public static final int SERVERPORT = 4444;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
PrintWriter out;
BufferedReader in;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public TCPClient(OnMessageReceived listener) {
mMessageListener = listener;
}
Does anybody here know how to do this? I would gladly appreciate your help. Thanks.
Upvotes: 0
Views: 198
Reputation: 12219
I'm assuming you have at least basic understanding of the Java language. To get any sort of meaningful answer, you will need to provide more details into what issues/problems you are having. A simple question of 'how to pass data from one object to another' is far too simple and generic to get any sort of quality answer. In particular:
What is wrong with implementing a simple mutator/setter or pass the data through the constructor?
Additionally:
In which case, the question is more how to get an object reference to an existing object from within an Activity.
In which case the question is more how to manage object references in Activities, in particular, maintaining object references through the Activity lifecycle.
In the future please provide as much detail as possible so we can better answer your question.
Upvotes: 0
Reputation: 989
You can achieve this in two ways.
Since you have not declared TCPClient object/reference in MainActivity. The following two ways can help you achieve this. They are as follows :
1). Inner Class
TCPClient being an inner class can even access MainActivity private variables.
`public class MainActivity { onCreate().... /* And other activity methods */ public class TCPClient { /* You class implementation here */} }`
2). A different class with constructor with ip parameter.
Pass the parameters in the TCPClient
constructor
public class TCPClient { public TCPClient(Parameters ... params ) {} /* Implementation here */}
And then from your MainActivity TCPClient tcpClient = new TCPClient(parameters ....);
Upvotes: 2
Reputation: 4707
First, since SERVERIP
in TCPClient
is declared final
, the value is not possible to be changed. You can either:
1) Remove the initial value and create a new constructor (recommended)
public static final String SERVERIP;
public TCPClient(OnMessageReceived listener, String ipAddress) {
mMessageListener = listener;
SERVERIP = ipAddress;
}
then in MainActivity
, call TCPClient client = new TCPClient(yourListener, new_IPAddress);
or, 2) Remove final
modifier
public static String SERVERIP = "";
then in MainActivity
, call TCPClient.SERVERIP = new_IPAddress;
Upvotes: 1
Reputation: 14472
Create a constructor that takes the IP address as an argument.
public class TCPClient {
private String SERVERIP = "";
public TCPClient(String serverIp){
SERVERIP = serverIp;
}
}
Then in your activity.
TCPClient client = new TCPClient(new_IPAddress);
However, the incorrect naming conventions are making my eyes bleed ;) Here is it fixed up:
public class TcpClient {
private String serverIp = "";
public TcpClient(String serverIp){
this.serverIp = serverIp;
}
}
...
TcpClient client = new TcpClient(newIpAddress);
Upvotes: 2
Reputation: 489
Use this in MainActivity.java
Intent myIntent = new Intent(MainActivity.this, TCPClient.class);
myIntent.putExtras("new_IPAddress", new_IPAddress);
startActivity(myIntent);
Use this in TCPClient.java
Bundle extra = getIntent().getExtras();
if(extra != null) {
String value = extra.getString("new_IPAddress")
}
After this use the string wherever you want.
Happy coding :)
Upvotes: 0