I have a listview in my activity. I want to show the data of it when clicked on it on second activity. But is not able to do so. Help. This is the code. What to do so that my data is fetched from listview and is shown in next activity?
ListActivity.java
package com.example.task;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends Activity implements OnItemClickListener{
ListView list1;
LoginDataBaseAdapter loginDataBaseAdapter;
ArrayList<HashMap<String, String>> datalist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
list1=(ListView)findViewById(R.id.list);
datalist=loginDataBaseAdapter.getAllAnimals();
ArrayAdapter<HashMap<String, String>> adapter=new ArrayAdapter<HashMap<String,String>>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,datalist);
list1.setAdapter(adapter);
list1.setOnItemClickListener(this);
}
#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;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
you can simple use this:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String mydata= datalist.get(arg2).toString();
// pass this data to your second activity
Intent n = new Intent(YourActivityName.this ,SecondActivity.class);
n.putExtra("key", mydata);
startActivity(intent);
}
Now retrieve in your SecondActivity on oncreate() method:
Intent n= getIntent();
String data = intent.getStringExtras("key");
String yourdata;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
yourdata= list1.getItemAtPosition(arg2).toString();
// pass this data to your second activity
}
I think you are trying to achieve like this..
String DATA;
#Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long arg3) {
DATA= YOUR_LIST.getItemAtPosition(pos).toString();
Intent intent = new Intent(getApplicationContext(),SECOND_ACTIVITY.class);
intent.putExtra("DATA",DATA);
startActivity(intent);
}
Hope it will help you..!!
Well apart from passing data by intents, you can also use Global Variables.
You can check out an example here.
You can set your global variable/s to the value that is applicable when list item is selected (using your onItemClick listner) and then access it in other activity.
Eg:
CLASS MyApp
class MyApp extends Application
{ public String whatever;}
CODE: ACTIVITY 1
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
MyApp as = ((MyApp)getApplicationContext());
as.whatever = position + "Clicked";
}
CODE: ACTIVITY 2
class Blah extends Activity
{
#Override
public void onCreate(Bundle b){
...
MyApp as = ((MyApp)getApplicationContext());
Log.d(as.whatever);
...}
}
MainActivity
String data;
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
data = adapter.getItem(position);
Intent intent = new Intnet(MainActivity.this, NextActivity.class);
i.putExtra("data", data);
startActivity(intent);
}
to open your data on NextActivity
Intent intent = getIntent();
String passData= intent.getExtras().getString("data");
Related
The aim is that store the string in the normal listview and then user touch(click) it move to next page along with user selected item. Once user click "Reject button" in the Operation.java, the should go-off from the List.java activity. It is not happening. It shows "Unfortunately, System has stopped".
MainActivity.java
public static int loop_exute=0 // first page variable
Intent i = new Intent(getApplicationContext(), List.class);
startActivity(i);
List.java
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class List extends ActionBarActivity implements OnItemClickListener {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
Button buttonSum;
static String[] name;
ArrayList<String> planetList = new ArrayList<String>();
int pos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mainListView = (ListView) findViewById( R.id.mainListView );
Bundle extras = getIntent().getExtras();
if(MainActivity.loop_exute==0) // acces 1st page value because the string array value sholuld load only once in its life time
{
MainActivity.loop_exute=MainActivity.loop_exute+2;
name = new String[] { "AAA", "BBB", "CCC", "DDD"
};
planetList.addAll( Arrays.asList(name) );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
mainListView.setAdapter( listAdapter );
//mainListView.getChildAt(0).setBackgroundColor(Color.RED);
show();
}
else // second time call
{
Intent intent = getIntent();
if(intent.hasExtra("MESSAGE"))
{
Bundle bd = getIntent().getExtras();
if(!bd.getString("MESSAGE").equals(null))
{
String object=bd.getString("MESSAGE");
int pos=planetList.indexOf(object);
planetList.remove(pos);
listAdapter.notifyDataSetChanged(); //show();
}
}
}
}
public void show()
{
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Intent i = new Intent(getApplicationContext(), Operation.class);
i.putExtra("Value2",name[pos]);
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.list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
Operation.java
reject = (ImageButton) findViewById(R.id.imageButton2);
reject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), List.class);
i.putExtra("MESSAGE",value1); //send the list item value what we select in previous activity
startActivity(i);
}
});
In the setOnClickListener under Operation.java, you are trying to start another activity. Instead, insert code to manually destroy the activity. You can also use the finish() method. Here's a link to know more about it:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I'm working on an order management system for android.
On the order page where the user submits an order, i want it to be able to allow the user to submit multiple orders, then take the user to a page where it displays all the orders made.
What method can i use to accomplish that?
This is the order activity page which is made up of 3 spinners and one textfield. This page takes the user to a ViewOrder page which displays what the user has selected. Instead of that i would like the user to submit multiple orders first, then see the display of all orders submitted. Any help is much appreciated.
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.androidhive.R;
public class OrderActivity extends Activity {
public Button btnOrder;
public EditText txtCrates;
public Spinner sp1, sp2, sp3;
public List<String> list;
public static String crates;
public static final String EXTRA_MESSAGE = "com.example.myspinner.msg1";
public static final String EXTRA_MESSAGE2 = "com.example.myspinner.msg2";
public static final String EXTRA_MESSAGE3 = "com.example.myspinner.msg3";
public static final String EXTRA_MESSAGE4 = "com.example.myspinner.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp3 = (Spinner) findViewById(R.id.spinner3);
btnOrder = (Button) findViewById(R.id.btnOrder);
txtCrates =(EditText) findViewById(R.id.crates);
list = new ArrayList<String> ();
list.add("CocaCola");
list.add("Sprite");
list.add("Fanta Orange");
list.add("Fanta Pineapple");
list.add("Fanta Blackcurrant");
list.add("Fanta Passion");
list.add("Krest");
list.add("Stoney");
list.add("Dasani");
list.add("Minute Maid Apple");
list.add("Minute Maid Mango");
list.add("Minute Maid Orange");
final String[] str1={"300ml","500ml","1 Litre","1.25 Litres","2 Litres"};
ArrayAdapter<String> adp1 = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, list);
ArrayAdapter<String> adp2 = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, str1);
ArrayAdapter<CharSequence> adp3 = ArrayAdapter.createFromResource
(this, R.array.str2, android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp1);
sp2.setAdapter(adp2);
sp3.setAdapter(adp3);
btnOrder.setOnClickListener(new View.OnClickListener() {
/** Called when the user clicks the Submit Order button */
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),ViewOrder.class);
String msg1=sp1.getSelectedItem().toString();
String msg2=sp2.getSelectedItem().toString();
String msg3=sp3.getSelectedItem().toString();
String message = txtCrates.getText().toString();
intent.putExtra(EXTRA_MESSAGE, msg1);
intent.putExtra(EXTRA_MESSAGE2, msg2);
intent.putExtra(EXTRA_MESSAGE3, msg3);
intent.putExtra(EXTRA_MESSAGE4, message);
startActivity(intent);
}
});
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), list.get(arg2),
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), str1[arg2],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp3.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String item = sp3.getSelectedItem().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
Once try as follows take an List for multiple orders
ArrayList<String> orders=new ArrayList<String>();
as class variable in activity
then store a complete order as string with some special character as divider like
String msg1=sp1.getSelectedItem().toString();
String msg2=sp2.getSelectedItem().toString();
String msg3=sp3.getSelectedItem().toString();
String message = txtCrates.getText().toString();
orders.add(msg1+","+msg2+","+msg3+","+message);
then for intent add as follows
intent.putStringArrayListExtra("orders", orders);
Hope this will helps you.
I am extremely new in android. I am making a listview and every time i click on a list's item, it should open a new activity. In this case Settings activity. I have tried many ways but none have worked. Any kind of help would be appreciated. This is my code:
package com.alex.mylist;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class Settings extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ListView lstSettings = (ListView) findViewById(R.id.lstSettings);
lstSettings.getChildAt(1).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent mainIntent = new Intent(v.getContext(), Register.class);
startActivity(mainIntent);
}
});
}
}
Use listView.setOnItemClickListener to make listview items clickable
lstSettings.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Intent mainIntent = new Intent(Settings.this,
Register.class);
startActivity(mainIntent);
}
});
Set an ItemSelectedListener or ItemClickListener for your listview.
listView.setOnItemSelectedListener(new ListView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// call Activity
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// call activity
}
});
Friends, I am using AutoCompleteTextView. The Suggestion :
String[] recipes={ "Fish", "Chicken", "Mutton"};
How do I do this:
When I select one of the item from the dropdown list, it will go to another event?
For example, I type Fi, it will come out Fish from the dropdown list and then I select Fish, it will go to another Activity.
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.view.View;
import android.content.Intent;
public class AutoCompleteTextActivity extends Activity {
String[] recipes ={
"Nasi Lemak With Ikan Bilis",
"Steamed Cod Fish"
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, recipes);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtRecipes);
textView.setThreshold(3);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View view, int index, long id){
Intent i=new Intent(this,Activity2.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
});
}
}
You can use a class that implements TextWatcher and override following methods:
#Override
public void afterTextChanged(final Editable editable) {
// check if entered text is "fish" and if yes then start the new activity.
}
#Override
public void beforeTextChanged(final CharSequence string,
final int start, final int count, final int after) {
}
#Override
public void onTextChanged(final CharSequence string, final int start,
final int before, final int count) {
}
}
receipesBelow Snippet will help you.
autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> av, View view,
int index, long id)
{
//index will give you item which you selected
Now start another activity here
Intent i=new Intent(context,SecondActivity.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
})
SecondActivity.java
You can retrieve that value is onCreate like below
String selectedItem=getIntent().getStringExtra("item");
You should have used setOnItemClickListener instead of setOnItemSelectedListener.
i want to add two numbers using spinner view. here in my code two spinners .After i run the emulator it displays straight result only. it does not display spinner control and i'm not able to select the two numbers. Pls give one solution. Thanks in advance. Here code
package com.kk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.R.id;
public class TrckActivity extends Activity {
/** Called when the activity is first created. */
String[] a={"-select-","1","2"};
String[] b={"-select-","2","4"};
int first,second,f,s,c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> a1= new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,a);
final Spinner sp1=(Spinner)findViewById(R.id.spinner1);
sp1.setAdapter(a1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
first=sp1.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter<String> a2= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,b);
final Spinner sp2=(Spinner)findViewById(R.id.spinner1);
sp2.setAdapter(a2);
sp2.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
second=sp2.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
if(first==1)
{
f=1;
}
else if(first==2)
{
f=2;
}
if(second==1)
{
s=2;
}
else if(second==2)
{
s=3;
}
c=f+s;
TextView tv=new TextView(this);
tv.setText(""+c);
setContentView(tv);
}
}
This might be because the spinner's "onItemSelected" method gets called initially as soon as your code enters the onCreate method. Maybe you have to maintain flag values to do this.
These links might help you get started with it,
Spinner onItemSelected called erroneously (without user action)
Spinner onItemSelected() executes when it is not suppose to
Try to exchange
android.R.layout.simple_dropdown_item_1line
to
android.R.layout.simple_spinner_item