Activity missing data on return - android

So I have my main activity which holds a list view and has a map which holds all my data for the list. Upon clicking on an item you are taken to a details display. When I press the back button to get back to the main activity from the detail activity, if I set a break point, my map keys are still intact, but all the strings in the objects are "" and the ints are -1. Here is what my main activity looks like:
public class MainActivity extends Activity {
private Map<String, Stunt> stunts = new LinkedHashMap<String, Stunt>();
private StuntsDao stuntsDao;
private ListAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stuntsDao = new StuntsDao(getApplicationContext());
stunts = stuntsDao.getAllStunts();
listAdapter = new ListAdapter(this, R.layout.list_layout, new ArrayList<Stunt>(stunts.values()));
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String stuntName = (String)((ViewHolder)view.getTag()).stuntName.getText();
Intent myIntent = new Intent(getApplicationContext(), StuntDetails.class);
getStuntDetailsIfNeeded(stuntName);
Stunt stunt = stunts.get(stuntName);
myIntent.putExtra("STUNT", stunt);
startActivity(myIntent);
}
});
}
...
}
Why would the objects in my map be basically empty?

onCreate() doesn't get called when you click back. You need to init your data again in onStart or onResume. See: http://developer.android.com/training/basics/activity-lifecycle/starting.html

Related

Pass Values to ListView by Clicking a Button

I want to pass values to list view by clicking a button. Problem is I want to make a list by gaining values from different activities with several buttons.
For Example :
In EnglandActivity if I click Button Visit I want to pass "England" to ListView in MainActivity,
In MalaysiaActivity pass "Malaysia" to ListView in MainActivity.
I dont know how to do that, Can you help me??
First Of All, You should write this on your onCreate() method of MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String data;/*For Storing Country Name*/
ListView listview = (ListView)findViewById(R.id.listView); /*Finding ListView From Layout*/
ArrayList<String> list = new ArrayList<String>(); /*ArrayList To Store All The Data Of ListView*/
ArrayAdapter adapter= new ArrayAdapter<String>(this,R.layout.android.R.layout.simple_list_item_1,list);/*Defining ArrayAdapter For ListView*/
listview.setAdapter(adapter); /*Setting Adapter To ListView*/
Bundle intentExtras = getIntent().getExtras(); /*Getting The Intent Extras Sent By The Activity Which You Had Navigated From*/
if(intentExtras != null) {/*Checking For Null*/
data= intentExtras.getString("countryName");/*Extracting The Data From The Intent Extras*/
list.add(0,data);/*You Can Replace 0 With The Position Of Your Wish*/
} else {
data=null;
}
}
Now Write This In The OnClickListener() Method Of Button On Each CountryNameActivity.java
btn.setOnClickListener(new OnClickListener() {/*Setting The Click Listener*/
#Override
public void onClick(View v) {
Intent intent = new Intent(this,MainActivity.this)/*Defining The Intent*/
intent.putExtra("countryName","CountryName");/*Putting The Data To Pass To The Next Activity*/
startActivity(intent);/*Starting The Activity*/
}
});
Make an object shared by all of your activity, put your data there and read them when you must put data into your ListView's adapter
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent listIntent = new Intent(getApplicationContext(), YourActivity.class);
listIntent.putExtra("country", yourList.get(position));
startActivity(listIntent);
}
});
in next activity onCreate:
String country;
Bundle extras = getIntent().getExtras();
if(extras == null) {
country= null;
} else {
country= extras.getString("country");
}

How to send data from one activity to another listview?

Hey guys I'm working on an app in android studio. I have a listview and when I make a selection i would like to add that selection to another listview in a different activity. What is the easiest/best way to do this? I've tried putExtra without any luck. Any examples or ideas would be great. Thank you guys.
Thanks for the examples everyone they've helped me understand a lot better the intent system. I've been trying the different examples everyone has posted and I've kind of gotten stuck. The goal is simply to have the items I select from the listview in the Walmart.java file to show up in the listview in GiftsSelected.java I have another place to open the activity so I don't need it to immediately open the new activity.
Here is my code:
This is Walmart.java
public class Walmart extends ActionBarActivity {
private String[]giftarray = {
"Apple" ,
"Bananas",
"Bed",
"Beef",
"Bottle",
"Bread",
"Broccoli",
"Carrots",
"Cat",
"Chicken",
"Chocolate",
"Computer",
"Cow",
"Crow",
"Dog",
"Dolphin",
"Dove",
"Drawer",
"Egg",
"Fish",
"Fork",
"Fridge",
"Giraffe",
};
Intent a = new Intent(Walmart.this,GiftsSelected.class);
private ListView giftListView;
private ArrayAdapter arrayAdapter;
ArrayList<String> list = new ArrayList<String>();
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){}
public boolean onContextItemSelected(MenuItem item){
return true;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_walmart);
getSupportActionBar().hide();
giftListView = (ListView) findViewById(R.id.gift_list1);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, giftarray );
giftListView.setAdapter(arrayAdapter);
giftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = "Item added to registry";
list.add(item2);
a.putStringArrayListExtra("list",list);
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
This is my GiftsSelected.java code:
public class GiftsSelected extends ActionBarActivity {
private ListView giftListView;
private ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> list = new ArrayList<String>();
//This makes my app crash which makes me think I did this wrong...
list = getIntent().getStringArrayListExtra("list");
String[] giftarray = new String[list.size()];
list.toArray(giftarray);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifts_selected);
getSupportActionBar().hide();
giftListView = (ListView) findViewById(R.id.gift_list1);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, giftarray);
giftListView.setAdapter(arrayAdapter);
giftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = "Item added to your registry";
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
this is the list that i use
ArrayList<String> list = new ArrayList<String>();
add elements in it like this
list.add("something");
in first Activity
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
i.putStringArrayListExtra("list",list);
startActivity(i);
in the second activity in the onCreate
list = getIntent().getStringArrayListExtra("list");
Let's say the first Activity is X and X holds a listview that updates another listview in Activity Y.
If X is tightly related to Y, that is to say Y launches X, gets data then immediately returns to Y, then you should use startActivityForResult from Y.
class ActivityY {
public static final int REQUEST_CODE = 2;
...
Intent i = new Intent(this, ActivityX.class);
startActivityForResult(i, REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String returndata = data.getStringExtra("rowdata");
//update your listView, do notifyDataSetChanged() etc;
}
}
}
}
class ActivityX {
// in listview onItemClickListener or elsewhere that listens to row click
Intent intent = new Intent();
returnIntent.putExtra("rowdata", rowdata); // whatever data you need to transfer
setResult(RESULT_OK,intent);
finish();
}
If X and Y are loosely related, that is to say X is not necessarily launched from Y, but goes to Y then you should just use the usual startActivity(intent).
if X and Y are completely independent, that is to say X is not necessarily launched from Y, may not go to Y or wanders other Activities before arriving in Y then you should cache the data. If the data size is small then the best way is to store it in Preferences. When you Y Activity starts, get the data, update your ListView, then if needed remove the cached data from Preferences.
a)You can make the listview item object parcelable and send it between activities via intent extra.
b)You can save the selected listview item in a global variable.(Not recommended)

Testing a click on an Android ListView with ActivityUnitTestCase

I have implemented a ListView that starts a new Activity when a list item is clicked. When I test it manually, it works perfectly. But when I try to do an automated test with ActivityUnitTestCase, I get a NullPointerException as though the ListView was empty.
MainActivity (partial):
public class MainActivity extends ListActivity {
#Override
protected void onResume() {
super.onResume();
String[] items = new String[] {"item 1", "item 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener () {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView clickedItem = (TextView) view;
CharSequence clickedItemText = clickedItem.getText(); // throws a NullPointerException when running testWithPerformItemClick()!
Intent intent = new Intent(MainActivity.this, ItemDisplayActivity.class);
intent.putExtra("parameter", clickedItemText);
startActivity(intent);
}
});
}
}
The test code that fails:
public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
ListView listView;
View child0;
public MainActivityTest() {
super(MainActivity.class);
}
private void setUpTest() {
MainActivity activity = startActivity(new Intent(), null, null);
getInstrumentation().callActivityOnStart(activity);
getInstrumentation().callActivityOnResume(activity);
listView = (ListView) activity.findViewById(android.R.id.list);
child0 = listView.getChildAt(0); // returns null!
}
public void testWithPerformClick() {
setUpTest();
child0.performClick(); // throws a NullPointerException!
Intent startedIntent = getStartedActivityIntent();
assertEquals("item 1", startedIntent.getStringExtra("parameter"));
}
public void testWithPerformItemClick() {
setUpTest();
long itemId = listView.getAdapter().getItemId(0);
listView.performItemClick(child0, 0, itemId); // throws a NullPointerException!
Intent startedIntent = getStartedActivityIntent();
assertEquals("item 1", startedIntent.getStringExtra("parameter"));
}
Both test methods fail because listView.getChildAt(0) returns null. Why is the ListView empty? How can I force it to update itself with the right children?
According to the documentation, If you prefer a functional test you should use ActivityInstrumentationTestCase2. To perform a click it works better. The #fewe answer is right, you have to wait the list to be drawn, to wait the fragment to be loaded you can use getInstrumentation().waitForIdleSync();
Try something like this.
public class ActivityTests extends
ActivityInstrumentationTestCase2<Activity>
final ListView list = (ListView) mActivity.findViewById(R.id.listId);
assertNotNull("The list was not loaded", list);
getInstrumentation().runOnMainSync(new Runnable() {
#Override
public void run() {
list.performItemClick(list.getAdapter().getView(position, null, null),
position, list.getAdapter().getItemId(position));
}
});
getInstrumentation().waitForIdleSync();
Then you cant test, for example, if a fragment was loaded.
mFragment frag = mActivity.getFragment();
assertNotNull("Fragment was not loaded", frag);
This is because the listview isn't drawn yet. You should wait for it to be drawn before trying to access its cells

Refresh ListView after updating in another Activity

I have two simple Activities -
Activity1: ListView from an Array
Activity2: EditText for editing the clicked row in Activity1
When I edit the value in Activity2 and returning to Activity1, the ListView doesn't reload the new value.
I want to refresh the ListView when I return from Activity2 or resume Activity1 or something that will update the list.
My code:
static ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Loading the values to list array
String[][] fulllist = loadArrays();
String[] list = new String[fulllist.length];
for(int i = 0; i<fulllist.length; i++) {
list[i] = fulllist[i][1];
}
// --------------------------------
dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
list);
setListAdapter(dataAdapter);
}
#Override
public void onResume() {
super.onResume();
// Loading the values to list array
String[][] fulllist = loadArrays();
String[] list = new String[fulllist.length];
for(int i = 0; i<fulllist.length; i++) {
list[i] = fulllist[i][1];
}
// --------------------------------
dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
list);
dataAdapter.notifyDataSetChanged();
}
loadArrays() is just method that converts from SharedPreferences to String Array. Activity2 saves the new data in SharedPreferences and than Activity1 can read it (with the new data).
If I return to the "main activity" (it is not Activity1) and than come back to Activity1 - the new data is shown, but I want this data will be updated when I return from Activity2 immediately.
loadArrays() method: pastebin.com/MHwNC0jK
Thanking you in advance!
On clicking the item in your first Activity, start your second Activity with startActivityForResult()
And then in Second Activity, after entering in EditText, probably there is a button. And in onClick of that button call,
intent.putExtra("edittextvalue", findViewById(R.id.edittext).getText().toString());
setResult(RESULT_OK, intent);
finish();
Now you come back to your first Activity and here you have to implement onActivityResult() callback. You can extract data from that intent's extras and set that respective item in your array and call notifyDataSetChanged().
This is ideally how you should be doing it.
If you want more info on how to use startActivityForResult() try this link - http://manisivapuram.blogspot.in/2011/06/how-to-use-startactivityforresult.html
1) Get reference ListView
mListView = (ListView)findViewById(R.id.auto_listview);
2) Create adapter One more time with changed values
MyAdapter myAdapter = new MyAdapter(getApplicationContext(),
R.layout.locations_list_item_layout,dataArray;
mListView.setAdapter(myAdapter);
setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int i, long l) {
myAdapter = new MyAdapter(getApplicationContext(),
//pass changed values vlues array R.layout.locations_list_item_layout,dataArray;
mListView.setAdapter(myAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare fulllist as Globle Variable and used static arraylist .

Android listview

I have created an android application. In that application when I click the listview item it should display in the another listview in the same layout.
Is this possible in android?
Well, quick snippet:
public Activity1 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
// ListView event
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedString", listView.getItemAtPosition(position));
startActivity(intent);
}
});
}
}
public Activity2 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
String valueFromActivity1 = getIntent().getString("SelectedString");
// ok now, u've got value from Activity1, do whatever w/ it
}
}
No you have to make intent and pass the variables of the current selected item of the listview to that intent and display the dynamic listview for that Item

Categories

Resources