Android - Send int index from a ListView to multiple activities - android

Im still new to Android and got a huge problem...
I have a ListViewActivity class with a String[] array of shops around me. When the user clicks on any of these, I want the ListViewActivity to send the int index to another activity (InfoActivity). However, before starting the InfoActivity, the user is taken to the OptionsActivity and once they click on the button Info they're directed to the InfoActivity. That index will be used to access an item from another string[] array in this activity.
I have tried the following:
ListViewActivity:
Bundle newbundle = new Bundle();
newbundle.putInt("key", position);
Intent intent = new Intent(ListViewActivity.this, InfoActivity.class);
intent.putExtras(newbundle);
startActivity(intent);
startActivity(new Intent(ListViewActivity.this, OptionsActivity.class));
InfoActivity:
public class InfoActivity extends Activity{
String[] price1 = {"£46", "£44", "£45", "£43", "£47", "£45", "£48", "£42", "£46", "£43"};
int key;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Bundle bundle = getIntent().getExtras();
key = bundle.getInt("key", 0);
TextView price1 = (TextView)findViewById(R.id.hairprice11);
price1.setText(hair_stylists_price1[key]);
...
Any ideas??

Try this instead..
Intent intent = new Intent(ListViewActivity.this, InfoActivity.class);
intent.putExtra("key", position);
startActivity(i);
Then pull it out..
int index = getIntent().getIntExtra("key");

Related

Android Passing values from one activity to other activity (string) or from one class to another

I have two class Profile.class and Details.class,
In profile class i have used a spinner with values like (ATM,Banking,Personal,Others etc)
and a button (OK).
on clicking ok button it will go to next activity that is details activity where i will be taking some details like-name,description etc.
after filling the details i have given a button (save).
on clicking button save i will be saving the name and description in database but i want to save the profile name also along with details. i am unable to transfer selected spinner text from Profile.class to Details.class
how to transfer?
create.class code
public class Create extends Activity {
public ArrayList<String> array_spinner;
Button button4;
String spinnertext;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
array_spinner=new ArrayList<String>();
array_spinner.add("ATM");
array_spinner.add("Bank");
array_spinner.add("Mail");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, array_spinner);
adapter.setNotifyOnChange(true);
spinner.setAdapter(adapter);
spinner.setLongClickable(true);
spinner.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}}
);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent4 = new Intent(view.getContext(), Details.class);
startActivityForResult(myIntent4, 0);
myIntent4 .putExtra("key", array_spinner.getSelectedItem().toString());
startActivity(myIntent4);
}
});
}}
details.class code
public class Details extends Activity {
EditText editText4,editText5,editText6;
Button button8,button9,button10;
TextView textView7;
String et4,et5,et6;
//SQLite Database db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
String spinnervalue = getIntent().getExtras().getString("Key");
please kindly explain me what is this "key"?
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
example
this is what you have to write in your first activity
Intent i = new Intent(getApplicationContext(), Product.class);
i.putExtra("productname", ori);
i.putExtra("productcost", position);
i.startActivityForResult(i,0);
then in your next activity you need to have this code
String productname,productcost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product);
tv1= (TextView)findViewById(R.id.tv1);
tv2= (TextView)findViewById(R.id.tv2);
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
position = extras.getString("position"); // get the value based on the key
tv1.setText(productname);//use where ever you want
productname = extras.getString("productname"); // get the value based on the key
tv2.setText(productname);
}
First of all take a spinner and provide value to them what you want and then the selected spinner value change it to string value and this string variable will be used in OK button to pass value through use of Intent or Shared preference to take this value to another activity and through there you can use it in database to display this value.
If you want to send data to another activity, you can do it using intent.
Bundle bund = new Bundle();
bund.putString("myKey",name);
Intent intent = new Intent(Profile.this, Detail.class);
intent.putExtras(bund);
startActivity(intent);
Now in Detail class, receive this data in onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
.......
String nameReceived = getIntent().getExtras().getString("myKey");
}
I have given the example of passing String to another activity however, you can pass boolean, int, double etc to another activity. See the full list on here

get a view from previous activity

I am doing a quiz app in which there are Act1 and Act2. Act1 shows the view for each question answers to select.
public class ACT1 extends Activity
{
EditText question=null;
RadioGroup choices = null;
-------
------
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
/* //---get the Bundle object passed in---
Bundle bundle = getIntent().getExtras();
//---get the data using the getInt() method---
int qId = bundle.getInt("questionIndex");
//dont know what to do here
question = (EditText) findViewById(R.id.question);
RadioGroup questionLayout = (RadioGroup)findViewById(R.id.answers);
------
this.getQuestionView(questionNo);
FrameLayout quizLayout = (FrameLayout) findViewById(R.id.quizLayout);
quizLayout.setVisibility(android.view.View.VISIBLE);
}
and in the method getQuestionView() the rest of code for getting questions and answers next submit buttons everything is there.
private void getQuestionView(questionNo)
{
------
------
//next and previous buttons OnClicklisteners
------
private OnClickListener finishListener = new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(Act1.this,Act2.class);
}
}
And Act2 shows a view for results which includes a table for questionlinks. on clicking the question link the respective question view shall be shown which is from Act1 and on back button clicked it goes back to Act2. i am new to android so anybody please help.
public class Act2 extends Activity {
--------
-------
TableLayout questionsTable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
int totalQues = Act1.getQuestions().length;
questionsTable =(TableLayout)findViewById(R.id.questions);
-------
-------
for(int i=0;i<totalQues;i++)
{
------
--------
TableRow tr = new TableRow(this);
TextView queText = new TextView(this);
tr.addView(queText,LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
tr.setClickable(true);
tr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,Act1.class);
//---use a Bundle object to add new key/values pairs---
Bundle extras = new Bundle();
//here i wanna check whether 2nd question is displaying
extras.putInt("questionIndex",2 );
//---attach the Bundle object to the Intent object---
intent.putExtras(extras);
startActivity(intent);
}
});
thanks in advance.
If I'm not mistaken, you need is to pass some data from one activity to another. This is done through the Intent class, it can contain "extras" which are actually simply key-value pairs which can be written by the calling activity and later read by the called activity.
For example, I can write the code like this:
public static final String EXTRA_QUESTION = "question";
// When you need to create the intent:
Intent intent = new Intent(this, Act2.class);
// questionId is whatever identifies the question in your code
intent.putExtra(EXTRA_QUESTION, questionId);
And in the other activity you write:
Intent intent = getIntent();
// In this example questionId is int, but it could be something else
int questionId = intent.getIntExtra(Act1.EXTRA_QUESTION, 0);

How do I pass an integer to an instance of a class and use it to call the method of another class?

here's activity 1 which is a listview. when the user clicks on an item, I want the item clicked to launch an instance of a class and pass an int value to it, which will later be used in a switch.
#Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
switch(position){
case 0:
Intent g = new Intent(books.this, SpecificBook.class);
Bundle b = new Bundle();
b.putInt("dt", 0);
g.putExtras(b);
books.this.startActivity(g);
break;
case 1:
Intent ex = new Intent(books.this, SpecificBook.class);
Bundle b1 = new Bundle();
b1.putInt("dt", 1);
ex.putExtras(b1);
books.this.startActivity(ex);
break;
//etc.
here's activity 2, which is supposed to retrieve the int value and call the appropriate method from the database helper class.
public class SpecificBook extends Activity {
private DatabaseHelper Adapter;
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
int dt = myBundle.getInt("dt");
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listy);
ListView lv = (ListView)findViewById(R.id.listview);
Adapter = new DatabaseHelper(this);
Adapter.open();
Cursor cursor = null;
switch(dt){
case 0:cursor = DatabaseHelper.getbook1Data(); break;
case 1:cursor = DatabaseHelper.getbook2Data(); break;
//etc.
}
startManagingCursor(cursor);
etc.
The database methods are queries.
Basically, I want each item in the book class listview to run it's own query based on the item selected and display the results.
I get a "source not found" and runtimeexception error. where am i going wrong? is there a better way to go about this?
I have already tried the "getter and setter" way to no avail. I've also tried the "putextra" method on the instance of the intent but that didn't work.
The earliest you can access an Intent is in onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listy);
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
int dt = myBundle.getInt("dt");
You should check if the Intent or Bundle is null and if you only want the one item from your Intent you can use:
Intent myLocalIntent = getIntent();
if(myLocalIntent != null) {
int dt = myLocalIntent.getIntExtra("dt", -1); // -1 is an arbitrary default value
}
Lastly, you don't need to create a new Bundle to pass values in an Intent and it looks like you simply want to pass the position... So you can drastically shorten your onItemClick() method:
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent g = new Intent(books.this, SpecificBook.class);
g.putInt("dt", position);
books.this.startActivity(g);
}

getting the id from the listview when click on item , in another activity

The code below give generate the ListView
public class MyList extends ListActivity {
static final String[] COUNTRIES = new String[] {LONG LIST OF COUNTRIES};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,R.layout.main,COUNTRIES));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Intent i=new Intent(MyList.this,Another.class);
Bundle b = new Bundle();
b.putInt("id", (int)id);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
Another activity is
public class Another extends Activity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mainseocnd);
Bundle b=new Bundle();
int value= b.getInt("id",0);
TextView tv=(TextView)findViewById(R.id.text);
tv.setText(""+value);
}
}
Now when i click on the any list item say id-5 it always display 0
I want to get the listview item id like if user click second item in list the another acivity should display 1(b/c start with 0).
please correct me where it is going wrong.
Thanks in advance!!
In second activity instead of
Bundle b=new Bundle();
int value= b.getInt("id",0);
use
int value = icicle.getInt("id",0);
This will give you a solution... :)
Your way of retrieving the value is wrong:
Bundle b=new Bundle();
int value= b.getInt("id",0);
You create a new bundle and try to get a value from it, when there is none (it's new).
You have to get the extras supplied with the launch intent for that activity instead. Try
int value = getIntent().getIntExtra("id", 0);
instead.

Android one activity to to popup window

My requirement is : List all sales route & highlight default, It should be first option. If the salesperson select other option(not selected default), then popup window should come. The popup window contain one form, it talk to DB.After submit the popup window form , it return previous screen , It need to allow to go other position.
See my code
This initial list Activity:
ArrayList<Object> routeList = getWmRoute();
ArrayList<String> routhPath = new ArrayList<String>();
for(int i = 0; i<routeList.size();i++){
routhPath.add(((WMRoute) routeList.get(i)).getDescription());
}
ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice,routhPath);
setListAdapter(ad);
final ListView list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setItemsCanFocus(true);
list.setTextFilterEnabled(true);
list.setItemChecked(0,true);
list.setSelection(0);
This is listener method
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if(position !=0 ){
Bundle bundle = new Bundle();
int postion = position;
String aString = Integer.toString(postion);
bundle.putString("positon", aString);
Intent showContent = new Intent(getApplicationContext(),SalesRouteDevitionActivity.class);
// startActivityForResult(showContent,0);
startActivity(showContent);
}
}
This is my SalesRouteDevitionActivity class
array_spinner=new String[2];
array_spinner[0]="Rain";
array_spinner[1]="Floods";
Bundle bundle = this.getIntent().getExtras();
param1 = bundle.getString("param1");
Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
s.setAdapter(adapter);
final Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(SalesRouteDevitionActivity.this, "Beep Bop", Toast.LENGTH_SHORT).show();
Intent showContent = new Intent(getApplicationContext(),SalesRouteActivity.class);
Bundle bundle = new Bundle();
bundle.putString("position", param1);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
startActivity(showContent);
}
});
This is my manifest file
<activity android:theme="#android:style/Theme.Dialog" android:name=".SalesRouteDevitionActivity"
android:label="Sales Route Diviation">
</activity>
After finish pop window work , how we can go to previous activity that particular place?
Please help me...
Thanks in advance
from wherever you start the popup window call it using intent throught call this startActivityForResult(your_intent,requestCode)
it will start the activity and in the popup activity do like this way
Bundle bundle = new Bundle();
bundle.putString("position", param1);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
so the popup will finish his activity and go back to previous activity where it will invoked with the result
and in your activity override the onActivityResult like this way
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1){
if(resultCode==RESULT_OK)
Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
}
}
You could create a global variable for the position of the selected item. Then when you return to the previous activity, you call listView1.smoothScrollToPosition(int position).
Do you want listview focus on a position which is not default?If so, you only need return intent with position and at onActivityForResult() you set focus at that position.If you have any problems, please add my yahoo or skype:fsoft_duonghv and we discuss .
Instead of starting a new activity for a popup window you can create a AlertDialog. You can call dismiss() to return your activity then.

Categories

Resources