Spinner issue, Android - android

I am working on an Android app. A part of my UI has a spinner. The spinner has names of five places listed. Also, on the same layout, there are two radio buttons. I want to create a new activity depending on the option selected-- one from the places list and either of the two radio buttons. Suppose, place1 is selected from the list and radiobutton1 is selected, it needs to open a new intent accordingly.
I have incorporated Adapter View in my code but the application closes after the second screen. LogCat (I'm using Eclipse) points to an adapter related issue. Here is the code. Please suggest another alternative or any modification. Thanks in advance!
package com.example.travel;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
#SuppressWarnings("unused")
public class Mumbai extends Activity {
TextView tv;
//int result_code=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.three);
tv=(TextView)findViewById(R.id.textView1);
Toast.makeText(this, "third Screen.", Toast.LENGTH_SHORT).show();
Button b1=(Button)findViewById(R.id.button1);
Button b2=(Button)findViewById(R.id.button2);
int for_spinner=0;
int for_radio=0;
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Mumbai.this,two.class);
setResult(3,i);
finish();
}
});
RadioGroup radiogroup=(RadioGroup)findViewById(R.id.radioGroup2);
int checkedRadioButton=radiogroup.getCheckedRadioButtonId();
switch(checkedRadioButton) {
case R.id.radio0 : for_radio=1;
break;
case R.id.radio1 : for_radio=2;
break;
}
Spinner answer=(Spinner)findViewById(R.id.spinner1);
answer.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int value_spinner= (Integer) parent.getItemAtPosition(position);
switch(position) {
case 0: //place1
int for_spinner=1;
break;
case 1: //place2
for_spinner=2;
break;
case 2: //place3
for_spinner=3;
break;
case 3: //place4
for_spinner=4;
break;
case 4: //place5
for_spinner=5;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
int for_spinner=1;
}
});
final int new_for_spinner=0;
final int new_for_radio=0;
for_spinner=new_for_spinner;
b2.setOnClickListener(new View.OnClickListener() {
//#SuppressWarnings("unused")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(new_for_radio==1 && new_for_spinner==1)
{
Intent i1= new Intent(Mumbai.this,food.class);
startActivityForResult(i1,1);
i1.putExtra("new_for_spinner","value");
}
if(new_for_radio==1 && new_for_spinner==2)
{
Intent i1= new Intent(Mumbai.this,food.class);
startActivityForResult(i1,1);
i1.putExtra("new_for_spinner","value");
}
if(new_for_radio==1 && new_for_spinner==3)
{
Intent i1= new Intent(Mumbai.this,food.class);
startActivityForResult(i1,1);
i1.putExtra("new_for_spinner","value");
}
if(new_for_radio==1 && new_for_spinner==4)
{
Intent i1= new Intent(Mumbai.this,food.class);
startActivityForResult(i1,1);
i1.putExtra("new_for_spinner","value");
}
if(new_for_radio==1 && new_for_spinner==5)
{
Intent i1= new Intent(Mumbai.this,food.class);
startActivityForResult(i1,1);
i1.putExtra("new_for_spinner","value");
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode==1 && resultCode==0)
{
Toast.makeText(this, "Back from Image page", Toast.LENGTH_SHORT).show();
super.onActivityResult(requestCode, resultCode, data);
}
}
#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;
}
}

You variable new_for_spinner, new_for_radio and for_spinner because of this line:
for_spinner=new_for_spinner;
Also i would suggest you to read http://java.about.com/od/javasyntax/a/nameconventions.htm because naming a class two.class is really bad ....

Related

how to select an item from a list , display the selected item in the editText then from the edit text display it in a toast

i am creating a simple android application consist from 3 activities.
Main Activity
Second Activity
List Page
the first activity has a button that display the listPage using the intent
after the user select an item from the list and click ok the system will transfer him to the second Activity that contain a edit box that the selected item will be display in it.
and after the user click get back message button the system will transfer him to the MainActivity and display the selected item in a Toast .
can anyone help me to accomplish this tasks????
MainActivity.java
package com.devleb.intentmenudemoapp;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
TextView txt1, txt2;
EditText edittxt1, edittxt2;
Button btn1, btn2, btn3, btn4;
String msgStr, msgStr2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView) findViewById(R.id.txt1);
txt2 = (TextView) findViewById(R.id.txt2);
edittxt1 = (EditText) findViewById(R.id.edit1);
edittxt2 = (EditText) findViewById(R.id.edit2);
btn1 = (Button) findViewById(R.id.btn);
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
btn3 = (Button) findViewById(R.id.btn3);
btn3.setOnClickListener(this);
btn4 = (Button) findViewById(R.id.btn4);
btn4.setOnClickListener(this);
registerForContextMenu(edittxt1);
registerForContextMenu(edittxt2);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
msgStr = edittxt1.getText().toString();
msgStr2 = edittxt2.getText().toString();
switch (item.getItemId()) {
case R.id.bold_item:
edittxt1.setText(beautify(msgStr, "BOLD"));
edittxt2.setText(beautify(msgStr2, "BOLD"));
break;
case R.id.italic_item:
edittxt1.setText(beautify(msgStr, "ITALIC"));
edittxt2.setText(beautify(msgStr2, "ITALIC"));
break;
case R.id.red_item:
edittxt1.setTextColor(color.background_dark | Color.RED); // red
edittxt2.setTextColor(color.background_dark | Color.RED); // red
break;
case R.id.blue_item:
edittxt1.setTextColor(color.background_dark | Color.BLUE); // blue
edittxt2.setTextColor(color.background_dark | Color.BLUE); // blue
break;
case R.id.yellow_item:
edittxt1.setTextColor(color.background_dark | Color.YELLOW); // yellow
edittxt2.setTextColor(color.background_dark | Color.YELLOW); // yellow
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
msgStr = edittxt1.getText().toString();
msgStr2 = edittxt2.getText().toString();
switch (item.getItemId()) {
case R.id.bold_item:
edittxt1.setText(beautify(msgStr, "BOLD"));
edittxt2.setText(beautify(msgStr2, "BOLD"));
break;
case R.id.italic_item:
edittxt1.setText(beautify(msgStr, "ITALIC"));
edittxt2.setText(beautify(msgStr2, "ITALIC"));
break;
case R.id.red_item:
edittxt1.setTextColor(color.background_dark | Color.RED); // red
edittxt2.setTextColor(color.background_dark | Color.RED); // red
break;
case R.id.blue_item:
edittxt1.setTextColor(color.background_dark | Color.BLUE); // blue
edittxt2.setTextColor(color.background_dark | Color.BLUE); // blue
break;
case R.id.yellow_item:
edittxt1.setTextColor(color.background_dark | Color.YELLOW); // yellow
edittxt2.setTextColor(color.background_dark | Color.YELLOW); // yellow
break;
default:
break;
}
return super.onContextItemSelected(item);
}
private CharSequence beautify(String originalText, String selectedStyle) {
// TODO Auto-generated method stub
Spanned answer = null;
if (selectedStyle.equals("BOLD"))
answer = Html.fromHtml("<b>" + originalText + "</b>");
else if (selectedStyle.equals("ITALIC"))
answer = Html.fromHtml("<i>" + originalText + "</i>");
else if (selectedStyle.equals("NORMAL"))
answer = Html.fromHtml("<normal>" + originalText + "</normal>");
return answer;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String value;
switch (v.getId()) {
case R.id.btn:
value = edittxt1.getText().toString();
txt2.setText(value);
break;
case R.id.btn2:
// value = edittxt1.getText().toString();
Intent i = new Intent(this, ListPage.class);
//i.putExtra("msgFromFirstActivity", value);
startActivity(i);
break;
case R.id.btn3:
Intent ii = new Intent();
ii.setType("image/pictures/*");
ii.setAction(Intent.ACTION_GET_CONTENT);
startActivity(ii);
break;
case R.id.btn4:
// get back message from second activity
Intent iii = new Intent(this, SecondActivity.class);
startActivityForResult(iii, 1);
default:
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (null != data) {
String returnMSG = data.getStringExtra("MESSAGE");
Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
} else
Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
SecondActivity.java
/*get back the result from the list page and display it in the edit
text box then after clicking show the result in the first activity as
toast
*/
package com.devleb.intentmenudemoapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity {
TextView txt;
EditText edittxt;
String result;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
/*
Intent iList = new Intent(this, SecondActivity.class);
startActivityForResult(iList, 1);
*/
edittxt = (EditText) findViewById(R.id.edit_txt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result = edittxt.getText().toString();
Intent i = new Intent();
i.putExtra("MESSAGE", result);
setResult(1, i);
finish();
}
});
}
/*#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1) {
if (null != data) {
String returnMSG = data
.getStringExtra("MessageFromsecondActivity");
// Toast.makeText(this, returnMSG, Toast.LENGTH_SHORT).show();
edittxt.setText(returnMSG);
} else
Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
ListPage.java
package com.devleb.intentmenudemoapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ListPage extends ListActivity {
TextView txt;
private static final String[] items = { "item1", "item2", "item3", "item4",
"item5", "item6", "item7", "item8", "item9" };
Button btn;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
txt = (TextView) findViewById(R.id.txt_list);
btn = (Button) findViewById(R.id.btn_list);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*
result = txt.getText().toString();
Intent i = new Intent();
i.putExtra("MessageFromsecondActivity", result);
setResult(1, i);
finish();
*/
}
});
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
txt.setText(items[position]);
super.onListItemClick(l, v, position, id);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
}
until now this what i did from here how to continue or what i need to add and change to make my tasks
What here you can do is,
To pass data from first activity to second activity
Intent in = new Intent(Youractivity.this, secondactivity.class);
in.putExtra("value", valuetopass);
startActivity(in);
Now, get data in second activity
Intent in = getIntent();
String valuetoset = in.getStringExtra("value");
And then set this value to edittext
youredittext.setText(valuetoset);
Try this..
ListPage.java
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// TODO Auto-generated method stub
String val = ((TextView) v).getText().toString().trim();
txt.setText(items[position]);
Intent i = new Intent(ListPage.this, SecondActivity.class);
i.putExtra("value", val);
startActivity(in);
}
SecondActivity.java
Intent i = getIntent();
String val = "";
if(i.hasExtra("value"))
val = i.getStringExtra("value");
edittxt = (EditText) findViewById(R.id.edit_txt);
edittxt.setText(val);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result = edittxt.getText().toString();
Intent i = new Intent();
i.putExtra("MESSAGE", result);
startActivity(in);
}
});
MainActivity.java
Intent i = getIntent();
String value = "";
if(i.hasExtra("MESSAGE")){
value = i.getStringExtra("MESSAGE");
Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show();
}

having multiple choice game that will calculate the score of the user but the system crush out

i have a sample android application that will display flag of coutry and multiple choice using radio button and each time the user check one of the answers the system will check if true and add 25 points to its score using intent to pass from activity to another with transferring the data mean the score until the user play 4 times to get to the end where the system will display his score .
until now i did the MainActivity that pass the data to the second but the problem is that the system force to close and in the log cat this what its display:
can anyone help me to solve this problem ????
logcat
11-27 22:15:53.609: E/AndroidRuntime(4834): Caused by: java.lang.NullPointerException
11-27 22:15:53.609: E/AndroidRuntime(4834): at com.devleb.flagology.SecondActivity.onCreate(SecondActivity.java:56)
MainActivity.java
package com.devleb.flagology;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnS;
RadioGroup rdgS;
RadioButton rdS1, rdS2, rdS3, rdS4;
private int score =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rdgS = (RadioGroup) findViewById(R.id.rdg1);
rdgS.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (rdS1.isChecked() || rdS2.isChecked() || rdS3.isChecked()
|| rdS4.isChecked()) {
btnS.setVisibility(View.VISIBLE);
}
}
});
rdS1 = (RadioButton) findViewById(R.id.rd_s1);
rdS2 = (RadioButton) findViewById(R.id.rd_s2);
rdS3 = (RadioButton) findViewById(R.id.rd_s3);
rdS4 = (RadioButton) findViewById(R.id.rd_s4);
btnS = (Button) findViewById(R.id.btn_s);
btnS.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, SecondActivity.class);
switch (rdgS.getCheckedRadioButtonId()) {
case R.id.rd_s1:
i.putExtra("score", score);
break;
case R.id.rd_s2:
i.putExtra("score", score);
break;
case R.id.rd_s3:
i.putExtra("score", score + 25);
break;
case R.id.rd_s4:
i.putExtra("score", score);
break;
default:
break;
}
startActivity(i);
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.hint_icon:
ShowAlertDialog();
break;
case R.id.about_icon:
Toast.makeText(this, "developed by Georges Matta",
Toast.LENGTH_SHORT).show();
default:
break;
}
return super.onOptionsItemSelected(item);
}
public void ShowAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hint")
.setMessage("The famouse sport is Bullfighting")
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
SecondActivity.java
package com.devleb.flagology;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class SecondActivity extends Activity {
Button btnI;
RadioGroup rdgI;
RadioButton rdI1, rdI2, rdI3, rdI4;
// TextView txt_result;
private int score = 0;
int finalScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int value = extras.getInt("score");
finalScore = score + value;
rdgI = (RadioGroup) findViewById(R.id.rdg2);
rdgI.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (rdI1.isChecked() || rdI2.isChecked()
|| rdI3.isChecked() || rdI4.isChecked()) {
btnI.setVisibility(View.VISIBLE);
}
}
});
rdI1 = (RadioButton) findViewById(R.id.rd_I1);
rdI2 = (RadioButton) findViewById(R.id.rd_I2);
rdI3 = (RadioButton) findViewById(R.id.rd_I3);
rdI4 = (RadioButton) findViewById(R.id.rd_I4);
btnI = (Button) findViewById(R.id.btn_s);
btnI.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(SecondActivity.this,
ThirdActivity.class);
switch (rdgI.getCheckedRadioButtonId()) {
case R.id.rd_I1:
i.putExtra("score", finalScore);
break;
case R.id.rd_I2:
i.putExtra("score", finalScore);
break;
case R.id.rd_I3:
i.putExtra("score", finalScore);
break;
case R.id.rd_I4:
i.putExtra("score", finalScore);
break;
default:
break;
}
startActivity(i);
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
ThirdActivity.java
package com.devleb.flagology;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class ThirdActivity extends Activity {
TextView txt_result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
txt_result = (TextView) findViewById(R.id.txtResult);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = String.valueOf(extras.getInt("score"));
txt_result.setText(value);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true;
}
}
I would venture to guess that your problem comes from this line:
btnS = (Button) findViewById(R.id.btn_s);
Most likely you made changes to your main activity layout (R.layout.activity_main) and either renamed or removed the <Button> element from it. If my guess is correct, then this line will fail to find the view in the inflated layout, and your btnS object will be null, leading to the actual exception you see.

onActivityResult Is not responding when choosing from a list

Hi Guys I am new with android programming and Java. I am trying to make a travel app. My issue is that how do I pass data from the selected item from the list to the editText filed. This is out I have done so far.. Pls. be kind I am newbee
The activity class that uses the ListActivity
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SelectStationActivity extends ListActivity {
String stations[] = { "Acton Main Line","Ealing","Great Western","Albany Park"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter <String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stations);
setListAdapter(adapter);
}
protected void OnListItemClick(ListView l, View v, int position, long id) {
String values = stations[(int) id];
Intent result = new Intent().putExtra("SELECTED_STATION_NAME", values);
setResult(RESULT_OK, result);
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.select_station, menu);
return true;
}
The main class
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TravelActivity extends Activity {
// The request code for the selectChkInBt
protected static final int SelectChkin_REQUEST_CODE = 1;
// The request code for the selectChkOutBt
protected static final int SelectChkout_REQUEST_CODE = 2;
private String checkinStation;
private String checkoutStation;
private Button checkinButton, checkoutButton, selectChkInButton, selectChkOutButton;
private EditText checkinEditText, checkoutEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_travel);
checkinButton = (Button) findViewById(R.id.btCkIn);
checkoutButton = (Button) findViewById(R.id.BtChkOut);
selectChkInButton = (Button) findViewById(R.id.btselectChkIn);
selectChkOutButton = (Button) findViewById(R.id.btselectChkOut);
checkinEditText = (EditText) findViewById(R.id.EditTxtChkIn);
checkoutEditText = (EditText) findViewById(R.id.EditTxtChkOut);
checkinButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkinEditText.getText().toString().equals("")) {
Toast.makeText(TravelActivity.this,
"Enter Check-in Station", Toast.LENGTH_LONG).show();
} else {
checkinStation = checkinEditText.getText().toString();
// Enable the check-out EditText and Button
checkinEditText.setEnabled(false);
checkinEditText.setTextColor(Color.CYAN);
checkinButton.setEnabled(false);
// Disable the check-in- EdiText AND Button
checkoutEditText.setEnabled(true);
checkoutButton.setEnabled(true);
checkoutEditText.requestFocus();
}
}
});
checkoutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkoutEditText.getText().toString().equals("")) {
Toast.makeText(TravelActivity.this,
"Enter Check-Out station", Toast.LENGTH_LONG)
.show();
} else {
checkoutStation = checkoutEditText.getText().toString();
// Clear edit Text
checkoutEditText.setText("");
// Enable the check-in EditText and Button
checkoutEditText.setEnabled(false);
checkoutButton.setEnabled(false);
/*
* Disable the check-out EditText and Button, to allow the
* eternal cycle of checking in and out to commence once again */
checkinEditText.setText("");
checkinEditText.setEnabled(true);
checkinButton.setEnabled(true);
checkinEditText.requestFocus();
Toast.makeText(getApplicationContext(),
"Travel information added!", Toast.LENGTH_SHORT)
.show();
}
}
});
I could implement a inner class for the two select buttons but i have diffculties to do it.Pls. provide some help how I could implement it thanks.
selectChkInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TravelActivity.this,
SelectStationActivity.class);
startActivityForResult(intent, SelectChkin_REQUEST_CODE);
}
});
selectChkOutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TravelActivity.this,SelectStationActivity.class);
startActivityForResult(intent, SelectChkout_REQUEST_CODE);
}
});
}
The onActivityResult is not responding here
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode== RESULT_OK){
if (requestCode == 1) {
checkinEditText.setText(data.getStringExtra("SELECTED_STATION_NAME"));
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Recipt");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
public boolean OnMenuItemSelected(int featureId, MenuItem item) {
String Recipt = "Recipt" + item.getItemId() + "\nCheck-in: "
+ checkinStation + "\nCheck-Out: " + checkoutStation;
Toast.makeText(getApplicationContext(), Recipt, Toast.LENGTH_LONG)
.show();
return true;
}
protected void OnSaveInstanceState(Bundle outState) {
outState.putString("lAST_CHECKIN", checkinStation);
outState.putString("lAST_CHECKOUT", checkoutStation);
}
protected void onRestoreInstance(Bundle savedInstanceState) {
checkinStation = savedInstanceState.getString(checkinStation);
checkoutStation = savedInstanceState.getString(checkoutStation);
}
}
You Should use this
checkinEditText.setText(data.getStringExtra("SELECTED_STATION_NAME"));
as
checkinEditText.setText(data.getStringExtra("SELECTED_STATION_NAME").toString());

when switching intents I lose data

When switching intents I lose data from the previous onActivityResult I need it to keep both numbers it gets from the user, currently it will keep one number, then when the next is entered it loses the previous, here's code:
package com.eric.theworks;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button width, height, calc;
TextView area;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
width = (Button) findViewById(R.id.button1);
height = (Button) findViewById(R.id.button2);
calc = (Button) findViewById(R.id.button3);
area = (TextView) findViewById(R.id.textView1);
width.setOnClickListener(this);
height.setOnClickListener(this);
calc.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, Numbers.class);
switch (v.getId()) {
case R.id.button1:
// width
i.putExtra("numbers", "width");
startActivityForResult(i, 1);
break;
case R.id.button2:
// height
i.putExtra("numbers", "height");
startActivityForResult(i, 1);
break;
case R.id.button3:
// calc
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data.getExtras().containsKey("widthInfo")){
width.setText(data.getStringExtra("widthInfo"));
}
if (data.getExtras().containsKey("heightInfo")){
height.setText(data.getStringExtra("heightInfo"));
}
}
}
package com.eric.theworks;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Numbers extends Activity implements OnClickListener {
EditText number;
Button sendInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.numbers);
number = (EditText) findViewById(R.id.editText1);
sendInfo = (Button) findViewById(R.id.button1);
sendInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = number.getText().toString();
Intent i = getIntent();
String msg = i.getStringExtra("numbers");
if (msg.contentEquals("width")) {
i.putExtra("widthInfo", s);
setResult(RESULT_OK, i);
finish();
}
if (msg.contentEquals("height")) {
i.putExtra("heightInfo", s);
setResult(RESULT_OK, i);
finish();
}
}
}
You can use static variable to store the previous data.
Declare static string globally.
static String widthInfo="";
static String heightInfo="";
Also Give different Requestcode.
case R.id.button1:
// width
i.putExtra("numbers", "width");
startActivityForResult(i, 1);
break;
case R.id.button2:
// height
i.putExtra("numbers", "height");
startActivityForResult(i, 2);
Then use it in your onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1): {
if (data.getExtras().containsKey("widthInfo")){
widthInfo=data.getStringExtra("widthInfo")
width.setText(data.getStringExtra("widthInfo"));
} else {
height.setText(heightInfo);
width.setText(widthInfo);
}
}
break;
case (2):
{
if (data.getExtras().containsKey("heightInfo")){
heightInfo=data.getStringExtra("heightInfo")
height.setText(data.getStringExtra("heightInfo"));
}else {
height.setText(heightInfo);
width.setText(widthInfo);
}
}
break;
}
}
you can use Bundle to pass data from one activity to another rather than the intent directly. for example:
Bundle b = new Bundle();
b.putString("SingleClick",a );
b.putString("LongClick", "no");
i.putExtras(b);
and to get the data in another activity use
Bundle bundle = getIntent().getExtras();
String admin = bundle.getString("LongClick");
String s = number.getText().toString();
Intent i = getIntent();
String msg = i.getStringExtra("numbers");
if (msg.contentEquals("width")) {
i.putExtra("widthInfo", s);
setResult(RESULT_OK, i);
finish();
}
make sure the string s has any values....

Text To Speech error

Can any let me know what I am doing wrong. Trying to get Text To Speech to work onClick for a Text View.
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener
{
private TextToSpeech tts;
private static final int MY_DATA_CHECK_CODE = 1234;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_24);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
((Button)findViewById(R.id.btnClear)) .setOnClickListener(clearbutton);
}
public void onClick(View v)
{
TextView textSpeak = (TextView) findViewById(R.id.mainText);
tts.speak(textSpeak.getText(), TextToSpeech.QUEUE_FLUSH, null);
}
public void onInit( int i)
{
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MY_DATA_CHECK_CODE)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
tts = new TextToSpeech(this, this);
}
else
{
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onDestory()
{
if (tts != null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
OnClickListener clearbutton = new OnClickListener()
{
public void onClick(View v)
{
TextView mainText = (TextView)findViewById(R.id.mainText);
mainText.setText("");
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.select8icons: setContentView(R.layout.layout_8);
break;
case R.id.select24icons: setContentView(R.layout.layout_24);
break;
case R.id.select63icons: setContentView(R.layout.layout_63);
break;
}
return true;
}
}
If anyone can help or has a better way to run the code, I love to hear your ideals. I try to research but every way I try gave me a error or something. Thanks in advance.
Your button will not work until TextToSpeech calls onInit. So you should disable btnSpeak until then.
Your TTS stuff looks fine. However, I dont see where you are binding any button to your onClick method and neither does your Activity seem to implement the onClickListener interface. You would need to do one or the other to get that onClick method to run.
So basically, I dont see any way your onClick method will ever get called.
Assuming you have some view/button in your layout the user is supposed to click to hear the tts, I think you want something like:
Button btnSpeak = (Button)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView textSpeak = (TextView) findViewById(R.id.mainText);
tts.speak(textSpeak.getText(), TextToSpeech.QUEUE_FLUSH, null);
}
});

Categories

Resources