I have an ArrayList, which is defined in one function, and gets called in another one. But when I add public ArrayList<String> list = new ArrayList<String>(); at the beginning, then run a function that adds items to that list and then try to call list in another function, it overwrites the previous set and just returns an empty list.
But when I just have public ArrayList<String> list; at the beginning, again add items in one function and then try to use list, it throws a NullPointerException.
I also tried having ArrayList<String> list = new ArrayList<String>(); in the onCreate method, but that's a NPE too.
So one function is just
public void setList() {
for(int x=0; x<=5;x++){
list.add(Integer.toString(x));
}
}
(the Integer.toString(x) is currently just for testing)
And the Other one is
public void setList() {
Log.d("Log",list.toString());
}
At the very beginning after public class MainActivity extends FragmentActivity {
I have tried
public ArrayList<String> list; //this throws a NPE
and
public ArrayList<String> list = new ArrayList<String>(); //this overwrites the variable each time it's called
How do I fix that?
Could the issue be that I change the displayed Fragment after the set method and before the get method is called?
Thank you very much
Use 1 list object at the top of the code and do not create new ones in the functions. It should look like this.
public static ArrayList<String> list = new ArrayList<String>();
// onCreate....
public void addElement() {
for(int x=0; x<=5;x++){
list.add(Integer.toString(x));
}
}
public void showElements() {
Log.d("Log",list.toString());
}
Related
public void setSearch(ArrayList<Search> ListSearch){
search=ListSearch;
removeInActiveClasses(search);
notifyItemRangeChanged(0,search.size());
}
public void removeInActiveClasses(ArrayList<Search> data){
for(int i=0;i<data.size();i++){
boolean isActive=Boolean.parseBoolean(data.get(i).getActive());
System.out.println("The course at Not Removed "+search.get(i).getName()+" is set to "+search.get(i).getActive());
if(!isActive){
System.out.println("The course at Removed"+search.get(i).getName()+" is set to "+search.get(i).getActive());
search.remove(i);
}
}
}
A list is passed through as listSearch and it contains a list of courses, if the courses are set to active which is a string that either true or false, and parsed as a boolean, then the item should be removed. I am certain I did the parsing correctly so I am wondering what is going on here? How come it does not delete all the false courses?
You might wanna create another instance of ArrayList and set your search to that one because your are accessing and modifying your ArrayList at simultaneously.
Other notes:
Please use camelCase for your argument names. So instead of ListSearch, use searchList.
For your class variable, try adding m in front so you won't get confused. So instead of search, use mSearchList
Lastly, you are mixing some variables within one method. Try unifying them for better maintenance.
Here's the full code.
public void setSearchList(ArrayList<Search> searchList) {
mSearchList = removeInactiveClasses(searchList);
notifyDataSetChanged();
}
private ArrayList<Search> removeInactiveClasses(ArrayList<Search> data) {
ArrayList<Search> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++){
boolean isActive = Boolean.parseBoolean(data.get(i).getActive());
if (isActive){
list.add(data.get(i));
}
}
return list;
}
I am using a singleton for fetching data from a web service and storing the resulting data object in an ArrayList. It looks like this:
public class DataHelper {
private static DataHelper instance = null;
private List<CustomClass> data = null;
protected DataHelper() {
data = new ArrayList<>();
}
public synchronized static DataHelper getInstance() {
if(instance == null) {
instance = new DataHelper();
}
return instance;
}
public void fetchData(){
BackendlessDataQuery query = new BackendlessDataQuery();
QueryOptions options = new QueryOptions();
options.setSortBy(Arrays.asList("street"));
query.setQueryOptions(options);
CustomClass.findAsync(query, new AsyncCallback<BackendlessCollection<CustomClass>>() {
#Override
public void handleResponse(BackendlessCollection<CustomClass> response) {
int size = response.getCurrentPage().size();
if (size > 0) {
addData(response.getData());
response.nextPage(this);
} else {
EventBus.getDefault().post(new FetchedDataEvent(data));
}
}
#Override
public void handleFault(BackendlessFault fault) {
EventBus.getDefault().post(new BackendlessFaultEvent(fault));
}
});
}
public List<CustomClass> getData(){
return this.data;
}
public void setData(List<CustomClass> data){
this.data = data;
}
public void addData(List<Poster> data){
this.data.addAll(data);
}
public List<CustomClass> getData(FilterEnum filter){
if(filter == FilterEnum.NOFILTER){
return getData();
}else{
// Filtering and returning filtered data
}
return getData();
}
}
The data is fetched correctly and the list actually contains data after it. Also, only one instance is created, as intended. However, whenever I call getData later, the length of this.data is 0. Because of this I also tried it with a subclass of Application holding the DataHelper object, resulting in the same problem.
Is there a good way of debugging this? Is there something like global watches in Android Studio?
Is there something wrong with my approach? Is there a better approach? I am mainly an iOS developer, so Android is pretty new to me. I am showing the data from the ArrayList in different views, thus I want to have it present in an the ArrayList as long as the application runs.
Thanks!
EDIT: Example use in a list view fragment (only relevant parts):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filter = FilterEnum.NOFILTER;
data = DataHelper.getInstance().getData(filter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
customClassListAdapter = new customClassListAdapter(getActivity(), data);}
EDIT2: Added code where I fetch the data from Backendless, changed reference of DataHelper to reference of data in first EDIT
EDIT3: I usa a local EventBus for notifying the list view about the new data. This looks like this and works (initially the data gets populated, but after e.g. applying a filter, the ArrayList I get with getData is empty):
#Subscribe
public void onMessageEvent(FetchedDataEvent event) {
customClassListAdapter.notifyDataSetChanged();
}
Try instead of keeping reference to your DataHelper instance, keeping reference to your list of retrieved items. F.e. when you first fetch the list (and it's ok as you say), assign it to a class member. Or itarate through it and create your own array list of objects for future use.
Okay I finally found the problem. It was not about the object or memory management at all. Since I give the reference on getData to my ArrayAdapter, whenever I call clear (which I do when changing the filter) on the ArrayAdapter, it empties the reference. I basically had to create a copy of the result for the ArrayAdapter:
data = new ArrayList<>(DataHelper.getInstance().getData(filter));
I was not aware of the fact that this is a reference at all. So with this the data always stays in the helper entirely. I only did this because this:
customClassListAdapter.notifyDataSetChanged();
does hot help here, it does not call getData with the new filter again.
Thanks everyone for your contributions, you definitely helped me to debug this.
It is likely that getData does get called before the data is filled.
A simple way to debug this is to add (import android.util.Log) Log.i("MyApp.MyClass.MyMethod", "I am here now"); entries to strategic places in fetchData, addData and getData and then, from the logs displayed by adb logcat ensure the data is filled before getData gets called.
public class UnitListAdapter extends ArrayAdapter<Unit> {
private Context context;
private ArrayList<Unit> units;
private MeasurementType type;
private static HashMap<String, Double> unitValues;
public void setMeasurementType(MeasurementType measurementType) {
type = measurementType;
}
public void setUnitValues(HashMap<String, Double> newValues) {
unitValues.clear();
unitValues = newValues;
}
public void setUnits(ArrayList<Unit> newUnits) {
units = newUnits;
}
}
Above is the implementation of ArrayAdapter minus the getView() and getCount() methods. Now, in my activity I have this method:
public void updateUnitAdapter(ArrayList<Unit> units, final MeasurementType measurementType) {
//change the type
unitAdapter.setMeasurementType(measurementType);
//set the hashmap unit values
unitValues = new HashMap<String, Double>() {{
put(measurementType.getType(), DEFAULT_VALUE);
}};
//clear the current units for the previous measurement type
unitAdapter.clear();
//add the new units for the new measurement type
for(Unit u : units) {
unitAdapter.add(u);
}
//update the list view
unitAdapter.notifyDataSetChanged();
}
but when I step through in the debugger, it gets to the getView() method and when I check these variables, they are haven't changed to the new ones that I am setting them too, they stay the same...is there something I am not understanding about ArrayAdapter ?
ArrayAdapter has its own internal collection, which is modified when you call add() or clear(). In this case you are updating it, but (from your comments) it looks like you have also overriden getCount() and getView() to get those values from somewhere else (possibly the units member?).
If that's the case, you should update units intead of calling clear()/add().
Otherwise, remove the units field altogether and use getItem() to access the collection items.
I'm writing tests for a simple Android application (it's a school project) and I'm having trouble testing the activity ContactListActivity which extends Android's ListActivity.
What I would like to test
Clicking the first item in ContactListActivity's ListView and checking if the ContactDetailActivity was started.
Problem
The list data comes from an SQLite database. For testing, I'm loading test data into the ListView, so the test won't be working with live data. Loading the test data works fine. Watching the emulator while running the test, I can see the activity being started and the test data appearing in the list. However, trying to access the first (and only) list item fails.
Test method
#UiThreadTest
public final void testLoadContactDetail() {
ListView list = activity.getListView();
assertNotNull(list);
ContactsListAdapter adapter = new ContactsListAdapter(
getInstrumentation().getContext(),
createData() // Creates an ArrayList of test data
);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
// list.getAdapter().getCount() is expectedly 1
// list.getChildCount() is unexpectedly 0
assertNotNull(list.getChildAt(0)); // Assertion fails
// (...)
}
As can be seen, I'm annotating the test with #UIThreadTest to be able to manipulate view elements. A new ListAdapter is created with test data and set to the list. Then, adapter.notifyDataSetChanged() makes sure that the list knows about the new data.
Question
How can I load test data from within an ActivityInstrumentationTestCase2 into a ListView so that the data will not only be displayed on screen, but actually "be there", meaning the list item can be fetched with list.getChildAt(0) and be clicked?
Entire test case
public class ContactListActivityFunctionalTest extends
ActivityInstrumentationTestCase2<ContactListActivity> {
private ContactListActivity activity;
public ContactListActivityFunctionalTest() {
super(ContactListActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
activity = getActivity();
}
protected void tearDown() throws Exception {
super.tearDown();
}
#UiThreadTest
public final void testLoadContactDetail() {
ListView list = activity.getListView();
assertNotNull(list);
ContactsListAdapter adapter = new ContactsListAdapter(
getInstrumentation().getContext(),
createData()
);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
assertNotNull(list.getChildAt(0));
// Anything beyond this point is never executed,
// because the above assertion fails, and I have no idea
// if this test code is correct at all.
ActivityMonitor monitor = getInstrumentation().addMonitor(
ContactDetailActivity.class.getName(), null, false
);
TouchUtils.clickView(this, list.getChildAt(0));
ContactDetailActivity contactDetailActivity =
(ContactDetailActivity)monitor.waitForActivityWithTimeout(2000);
assertNotNull(contactDetailActivity);
assertTrue(getInstrumentation().checkMonitorHit(monitor, 1));
contactDetailActivity.finish();
}
private List<ContactInterface> createData() {
ContactInterface contact = new Contact();
contact.setId(1L);
contact.setName("Unit Test").setPhone("0123456789").setPosition(3);
List<ContactInterface> contacts = new ArrayList<ContactInterface>();
contacts.add(contact);
return contacts;
}
}
It looks like the listView.getChildAt method returns visible views. https://stackoverflow.com/a/6767006/693752
So, my guess is that the item is not visible yet. None are as getChildCount is returning 0. Maybe you should either :
wait a bit before asserting. Ok, it's dirty but UI testing needs it sometime.
post the assert inside a runnable on the ui thread so that it gets executed after the listview is executed. This will turn your test into something a bit more complex as you would have to synchronize the future runnable and the current testing thread a countDownLatch. And for this, you should consider not using #UIThreadTest.
I know I've asked how to load test data from within an ActivityInstrumentationTestCase2, but perhaps the answer to the question is to use ActivityUnitTestCase rather than ActivityInstrumentationTestCase2 in this particular case:
General activity behaviour is being tested, rather than interaction with other components
Well, it works...
Here is the rewritten, working test case that tests whether the ListView exists and whether the correct activity is started after a click on the list's first item.
public class ContactListActivityTest
extends ActivityUnitTestCase<ContactListActivity> {
private ContactListActivity activity;
public ContactListActivityTest() {
super(ContactListActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
Intent intent = new Intent(
getInstrumentation().getTargetContext(), ContactListActivity.class
);
startActivity(intent, null, null);
activity = getActivity();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public final void testItemClick() {
getInstrumentation().callActivityOnStart(activity);
getInstrumentation().callActivityOnResume(activity);
// Check if list exists
ListView list = activity.getListView();
assertNotNull("Intent was null", list);
// Load test data
ContactsListAdapter adapter = new ContactsListAdapter(
getInstrumentation().getContext(),
createData()
);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
assertEquals(2, adapter.getCount());
// Check if list has at least one item to click
View firstItem = list.getAdapter().getView(0, null, null);
assertNotNull(firstItem);
// Perform a click on the first item
list.performItemClick(
firstItem,
0,
list.getAdapter().getItemId(0)
);
// Check if the contact details activity got started
Intent intent = getStartedActivityIntent();
assertNotNull(intent);
assertEquals(
ContactDetailActivity.class.getName(),
intent.getComponent().getClassName()
);
}
private List<ContactInterface> createData() {
List<ContactInterface> contacts = new ArrayList<ContactInterface>();
ContactInterface contact = new Contact();
contact.setId(1L);
contact.setName("Jane Doe").setPhone("0123456789").setPosition(1);
contacts.add(contact);
ContactInterface contact2 = new Contact();
contact2.setId(2L);
contact2.setName("John Doe").setPhone("0234567890").setPosition(2);
contacts.add(contact2);
return contacts;
}
}
Very simple error I'm sure, just having a mental block for some reason!
I have an object which contains an ArrayList of other objects. When I try to initialise the list in the constructor it does not seem to initialise and I get a nullpointer when I attempt to access the list within the code.
Order class variables:
private int covers;
private int table;
private ArrayList<MenuItem> items;
Here is the Order class:
public Order(int covers, int table) {
super();
this.covers = covers;
this.table = table;
this.items = new ArrayList<MenuItem>();
}
Here is the code within the MainActivity causing me problems:
order = new Order();
order.setCovers(2);
order.setTable(1);
order.addToOrder(new MenuItem("Item 1", 12.99));
Toast.makeText(getApplicationContext(), "order size: " + order.getItems().size(), Toast.LENGTH_SHORT).show();
I would expect the Toast to display "1". However when I ran debugger I noticed the order object ArrayList attribute was equal to null.
Any idea why? Thanks in advance!
EDIT: addToOrder method:
public void addToOrder(MenuItem m){
items.add(m);
}
You have failed to initialize items. Use
vorder = new Order(int1, int2);
instead of
vorder = new Order();
items is initialized inside
public Order(int covers, int table) {
}
not on the default constructor.