package mrcToolkit.mobilerelayscontroller; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.os.AsyncTask; import android.view.View; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends Activity { private ListView mList; private ArrayList arrayList; private MyCustomAdapter mAdapter; private TCPClient mTcpClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); arrayList = new ArrayList(); Button bt1 = (Button)findViewById(R.id.relay1button); Button bt2 = (Button)findViewById(R.id.relay2button); Button bt3 = (Button)findViewById(R.id.relay3button); Button bt4 = (Button)findViewById(R.id.relay4button); Button bt5 = (Button)findViewById(R.id.relay5button); mList = (ListView)findViewById(R.id.list); mAdapter = new MyCustomAdapter(this,arrayList); mList.setAdapter(mAdapter); new connectTask().execute(""); //button 1 bt1.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ //byte mess = 0x38; String message = "a"; //String message = Byte.toString(mess); arrayList.add("c: Relais 1"); if(mTcpClient != null){ mTcpClient.sendMessage(message); } mAdapter.notifyDataSetChanged(); } }); //button 2 bt2.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ String message = "b"; arrayList.add("c: Relais 2"); if(mTcpClient != null){ mTcpClient.sendMessage(message); } mAdapter.notifyDataSetChanged(); } }); //button 3 bt3.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ String message = "c"; arrayList.add("c: Relais 3"); if(mTcpClient != null){ mTcpClient.sendMessage(message); } mAdapter.notifyDataSetChanged(); } }); //button 4 bt4.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ String message = "d"; arrayList.add("c: Relais 4"); if(mTcpClient != null){ mTcpClient.sendMessage(message); } mAdapter.notifyDataSetChanged(); } }); //button 5 bt5.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ String message = "e"; arrayList.add("c: Relais 5"); if(mTcpClient != null){ mTcpClient.sendMessage(message); } mAdapter.notifyDataSetChanged(); } }); } public class connectTask extends AsyncTask{ @Override protected TCPClient doInBackground(String... arg0) { // TODO Auto-generated method stub mTcpClient = new TCPClient(new TCPClient.OnMessageReceived(){ public void messageReceived(String message){ publishProgress(message); } }); mTcpClient.run(); return null; } protected void onProgressUpdate(String... values){ super.onProgressUpdate(values); arrayList.add(values[0]); mAdapter.notifyDataSetChanged(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }