-3

Can anyone help me with this. I want to send data from edittext to another activity in the JSONMessage. I want to send to the IDDevice in my second activity.

Here is my code

It's my firstActivity

 et = (EditText) findViewById(R.id.editText1);
    bt = (Button) findViewById(R.id.bAdd);
    lv = (ListView) findViewById(R.id.listView);
    arrayList = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);

    lv.setAdapter(adapter);
    onButtonClick();


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String inputID = et.getText().toString();
            Intent IDdevice = new Intent(MainActivity.this, ControlLed.class);
            IDdevice.putExtra("ID", inputID);
            startActivity(IDdevice);
        }
    });
}

And second activity

 public void device1on(){
    String topic = "server/esp001";
    MqttMessage message = new MqttMessage();
    message.setPayload("{\"idDevice\":\"esp001\",\"status\":\"0\",\"data\":\"100\",\"address\":\"1\",\"function\":\"1\",\"user\":\"admin\"}".getBytes());// I want to send data from first activity to the idDevice
    try {
        client.publish(topic, message);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
TLDima
  • 45
  • 3

2 Answers2

2

In second activity, get id from Intent using getStringExtra method as mentioned below:

String id = getIntent().getStringExtra("ID");

And then set it to message object

JSONObject json = new JSONObject();
try {
    json.put("idDevice", id);
    json.put("status", "0");
    json.put("data", "100");
    json.put("address", "1");
    json.put("function", "1");
    json.put("user", "admin");
} catch (JSONException e) {

}
String payload = json.toString(); //"{\"idDevice\":\"value of id\",\"status\":\"0\",‌​\"data\":\"100\",\"a‌​ddress\":\"1\",\"fun‌​ction\":\"1\",\"user‌​\":\"admin\"}"
message.setPayload(payload.getBytes());
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • Thanks for the answer, but its not that i want. I want to put it in to the jsonMessage. – TLDima May 23 '17 at 08:33
  • I mean i want to put text in to the idDevice message.setPayload("{\"idDevice\":\"Here\",\"status\":\"0\",\"data\":\"100\",\"address\":\"1\",\"function\":\"1\",\"user\":\"admin\"}".getBytes());// I want to send data from first activity to the idDevice – TLDima May 23 '17 at 08:34
  • I have created JSON object dynamically – PEHLAJ May 23 '17 at 08:55
  • Accept the answer then – PEHLAJ May 23 '17 at 09:43
0
String getvallue;    
Intent i=i.getIntent();        
getVallue=i.getStringExtra("name");  
Textview txt=(Textview)findviewbyid(R.id.txt);
txt.setText(getVallue);
Phil3992
  • 1,059
  • 6
  • 21
  • 45
Akash Soni
  • 11
  • 8
  • **From review queue**: May I request you to please add some context around your source-code. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 24 '17 at 01:33