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....
Related
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 ....
Pease help me with my code.
When i click any button, button1, button2 or button3 opens new activity, but layout is empty, without any text's and others.
Code of activity from calling the new activity:
package com.novator.inweld;
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;
public class TentsCatalog extends Activity implements OnClickListener
{
private Button button1;
private Button button2;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tents_catalog);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
if(view == button1)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "1");
startActivity(intent);
}
if(view == button2)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "2");
startActivity(intent);
}
if(view == button3)
{
Intent intent = new Intent(this, TentPage.class);
intent.putExtra("tentId", "3");
startActivity(intent);
}
}
}
Code of my new activity:
package com.novator.inweld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TentPage extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String tentId = intent.getStringExtra("tentId");
if(tentId == "1")
{
setContentView(R.layout.tent1);
}
if(tentId == "2")
{
setContentView(R.layout.tent2);
}
if(tentId == "3")
{
setContentView(R.layout.tent3);
}
}
}
use equals(), not ==
if(tentId.equals("1"))
change onClick method with:
#Override
public void onClick(View v)
{
Intent intent = new Intent(this, TentPage.class);
switch (v.getId()) {
case R.id.button1:
intent.putExtra("tentId", "1");
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("tentId", "2");
startActivity(intent);
break;
case R.id.button3:
intent.putExtra("tentId", "3");
startActivity(intent);
break;
default:
break;
}
and use tentId.equals(""); for String check you must use equals() method and for number value use == like:
if(tentId.equals("1"))
{
setContentView(R.layout.tent1);
}
if(tentId.equals("2"))
{
setContentView(R.layout.tent2);
}
if(tentId.equals("3"))
{
setContentView(R.layout.tent3);
}
Java is a b*%$#ch... Use equals():
if(tentId.equals("1")) ...
Also, according to this answer, Android supports Java 1.7's strings in switch statements, meaning you can rewrite your code in a tidier fashion:
switch(tendId) {
case "1": ...
case "2": ...
case "3": ...
}
Keep in mind that the simplest solution would be to pass tendId as an int and not a string:
intent.putExtra("tendId", 1);
int tendId = intent.getIntExtra("tendId");
Replace your TentPage like:
package com.novator.inweld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TentPage extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String tentId = intent.getStringExtra("tentId");
if(tentId.equals("1"))
{
setContentView(R.layout.tent1);
}
if(tentId.equals("2"))
{
setContentView(R.layout.tent2);
}
if(tentId.equals("3"))
{
setContentView(R.layout.tent3);
}
}
}
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();
}
I Have an activty in my app which uses putextra(int) method to pass the value 0 first when the aactivty is started. Then on pressing a next button it passes 6,Subsequently 12 and so on. But the trouble is on pressing the next second time I found that the value of index received using getextras method is 0.Is it cause i am calling the activity from itself . This is the code snippet:
package com.movie;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class movielist extends Activity implements OnClickListener{
Button bm[] = new Button[6];
Button nxt,prev;
String namesdb[]=new String[50];
databaseconnect db;
String lang;
int start,index,end;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mallu_list);
db = new databaseconnect(this);
db.open();
start=this.getIntent().getExtras().getInt("index");
lang =this.getIntent().getExtras().getString("lang");
Toast.makeText(this, start+"", Toast.LENGTH_SHORT).show();
Log.i("info",start+"");
if(!db.isdata(lang))
{
Toast.makeText(this, "No data in Database", Toast.LENGTH_SHORT).show();
finish();
return;
}
end=start+6;
bm[0] = (Button) findViewById(R.id.bm1);
bm[1] = (Button) findViewById(R.id.bm2);
bm[2] = (Button) findViewById(R.id.bm3);
bm[3] = (Button) findViewById(R.id.bm4);
bm[4] = (Button) findViewById(R.id.bm5);
bm[5] = (Button) findViewById(R.id.bm6);
nxt = (Button) findViewById(R.id.nxt);
prev = (Button) findViewById(R.id.prev);
if(start==0)
{
// prev.setBackgroundResource(0);
// prev.setText("");
}
else
prev.setOnClickListener(this);
namesdb = db.getmovie("",lang);
for (int i = 0; start+i < namesdb.length && i<6; i++) {
bm[i].setText(namesdb[start+i]);
bm[i].setOnClickListener(this);
}
int flag=0;
int i=namesdb.length;
Log.i("info",i+"");
Toast.makeText(this, i+"", Toast.LENGTH_SHORT).show();
for(int j=5;j>=i-start;j--)
{
bm[j].setBackgroundResource(0);
flag=1;
}
if(flag==1)
{
// nxt.setBackgroundResource(0);
// nxt.setText("");
}
else
nxt.setOnClickListener(this);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent myint2 = new Intent(this, list.class);
startActivity(myint2);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bm1:
call(namesdb[start],start,lang);
break;
case R.id.bm2:
call(namesdb[start+1],start,lang);
break;
case R.id.bm3:
call(namesdb[start+2],start,lang);
break;
case R.id.bm4:
call(namesdb[start+3],start,lang);
break;
case R.id.bm5:
call(namesdb[start+4],start,lang);
break;
case R.id.bm6:
call(namesdb[start+5],start,lang);
break;
case R.id.nxt:
int i=namesdb.length;
Intent myint2 = new Intent(this,movielist.class);
myint2.putExtra("index",end);
myint2.putExtra("lang",lang);
startActivity(myint2);
case R.id.prev:
if(start==0)
{
prev.setBackgroundResource(0);
}
else
{
myint2 = new Intent(this,movielist.class);
myint2.putExtra("index",start-6);
myint2.putExtra("lang",lang);
startActivity(myint2);}
break;
}
}
public void call(String name,int start2,String lang) {
Intent myint = new Intent(this, detail.class);
myint.putExtra("nameid", name);
myint.putExtra("index", start2);
myint.putExtra("lang", lang);
startActivity(myint);
}
}
Here's your problem. This part of the switch statement:
case R.id.nxt:
int i=namesdb.length;
Intent myint2 = new Intent(this,movielist.class);
myint2.putExtra("index",end);
myint2.putExtra("lang",lang);
startActivity(myint2);
is missing a break at the end. So it falls through to the next case, which starts the activity with different extras.
I'm trying to use a barcode scanner and then take that input and use in another activity to open with a url. I've been able to get the data to return, just not in another activity and haven't seen any projects exactly like this. I'm not sure if it has to do with intent or how I'm calling the string. The webview in the second java works but doesn't take the string. Thanks for the help!
Scanner.java (which works okay)
package com.pangolin.rollin.ts;
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.TextView;
import android.widget.Toast;
public class Scanner extends Activity {
TextView tvStatus;
TextView tvResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
Button websku = (Button) findViewById(R.id.btnsku);
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
startActivity(myintent);
}
});
tvStatus = (TextView) findViewById(R.id.tvStatus);
tvResult = (TextView) findViewById(R.id.tvResult);
Button scanBtn = (Button) findViewById(R.id.btnScan);
// in some trigger function e.g. button press within your code you
// should add:
scanBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG)
.show();
}
}
});
}
// In the same activity you’ll need the following to retrieve the results:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}
And websku.java (doesn't work, supposed to take results from previous activity.
package com.pangolin.rollin.ts;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Websku extends Activity {
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String sku = intent.getStringExtra("SCAN_RESULT");
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_websku);
WebView webView = (WebView) findViewById(R.id.webview_sku);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.title_activity_websku);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://m.radioshack.com/radioshack/catalog/searchList.do?categoryId=&keyword="+sku);
};
}
You don't set any extra to Websku intent:
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
startActivity(myintent);
}
});
Should be:
websku.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myintent = new Intent(Scanner.this, Websku.class);
myintent.putExtra("somename", somevalue);
startActivity(myintent);
}
});
You don't set the extras for the websku Activity. Save the intent returned from the scanner:
private Intent mWebskuIntent;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
mWebskuIntent = intent;
// more of your code
Then when you start the websku Activity make a copy of the saved intent which will copy also the extras returned from the scanner:
Intent myintent = new Intent(mWebskuIntent);
myintent.setClass(Scanner.this, Websku.class);
startActivity(myintent);
You might want to check for mWebskuIntent being null as well.