I have an activity class setup from a tutorial I found. The activity class simply opens a new screen and displays the message from a textbox on the mainActivity. The APP calls a php file on a server which returns JSON data. That side all works well in the browser when I test it.
Now for the android issue. I am trying to get that data to be shown in the messageActivity class. I have tried to simply use the below code to do it but eclipse flags the intent line saying the “The constructor Intent(Connect, Class) is undefined”
My question is this what is the correct way to fire the activity so that the JSON data will be shown and secondly if you look at the quaryDB method how do I get the JSON data to the messageActivity from the response?
Main Activity:
public class MainActivity extends Activity {
RadioButton radioButton1, radioButton2, radioButton3;
public final static String EXTRA_MESSAGE = "com.example.xxxxxxx.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1 = (RadioButton)findViewById(R.id.radioButton1);
radioButton2 = (RadioButton)findViewById(R.id.radioButton2);
radioButton3 = (RadioButton)findViewById(R.id.radioButton3);
radioButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton1.isChecked()){
radioButton2.setChecked(false);
radioButton3.setChecked(false);
}
}
});
radioButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton2.isChecked()){
radioButton3.setChecked(false);
radioButton1.setChecked(false);
}
}
});
radioButton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton3.isChecked()){
radioButton2.setChecked(false);
radioButton1.setChecked(false);
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.PracticeTypes, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Connect c = new Connect();
c.quaryDB(this);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id){
Toast.makeText(parent.getContext(), "The Selected Practice is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent){
}
}
public void sendMessage(View view){
Intent intent= new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
};
The DisplayMessageActivity:
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView= new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
And then the connect class that gets the JSON data response:
public class Connect{
void Connect(){
}
public void quaryDB(Context context){
Connection conn=null;
try{
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.6/DevServer/getAllByZip.php");
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
String succMsg = "Successful Execute.\n";
Toast.makeText(context, succMsg, Toast.LENGTH_LONG).show();
}
catch (Exception e){
String errMsg ="Cannot connect to database server.\n"+e.toString();
Toast.makeText(context, errMsg, Toast.LENGTH_LONG).show();
}
finally{
if (conn !=null){
try{
conn.close();
}
catch (Exception e){
}
}
}
}
public void sendMessage(View view){
Intent intent= new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
The sendMessage method doesn’t feel right being in the Connect class but I am lost as to how to properly send the JSON data to the DisplayMessageActivity. After I figure that part out I will be able to begin parsing the data and doing what needs to be done.
Fisrt I’ll explain the issue:
The constructer for
Intentis expecting a activity context as first parameter. When you use it in a class that extendsActivityyou can usethisas first parameter, which is the activity itself.When you use it in the class
Connect, which doesn’t extendActivity, usingthisas first parameter reverts to the classConnectwich doesn´t have a context.Solution:
If you are calling
sendMessagefrom the activity, you can do the following:in the activity
in the class
ConnectIf you are not calling it from the activity, pass the context to the class constructer:
and you can now use the context in
sendMessage()good luck