Android studio not deleting my items in Adapter? - android

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;
}

Related

How can I sort array of numbers higher to lower (reverse/decreasing order)?

How can I sort array by numbers higher to lower.
Array
ArrayList<TestClass> array1 = new ArrayList<>();
Class
public class TestClass{
public boolean type;
public int counter;
public TestClass(boolean type, int counter) {
this.type = type;
this.counter = counter;
}
}
I tried do this
Collections.sort(array1);
But I got error
reason: no instance(s) of type variable(s) T exist so that TestClass conforms to Comparable
Assuming you don't have any accessory methods, you can use
array1.sort(Comparator.comparing(a -> a.counter));
The sorting order you asked for is reverse order, there are couple of ways to achieve this.
You can simple do a reverse of the previous sort like
array1.sort(Comparator.comparing(a -> a.counter));
array1.sort(Collections.reverseOrder());
If you can't user Comparator.comparing, you can do as follows
Collections.sort(array1, (item1, item2) -> Integer.compare(item2.counter, item1.counter));
The above statement can be explained as below.
Collections.sort() is provided from Java collections framework.
First argument specifies which collection needs to be sorted.
Second argument depicts on how each object in the collection should
be evaluated with other object in comparison. So for every pair of objects, in your case integers here, the condition returns true if the second element is greater than the first one. Which will pull the entire list to appear from higher to lower
arrayList.sort(new Comparator<TestClass>() {
#Override
public int compare(TestClass testClass, TestClass t1) {
return Integer.compare(t1.counter, testClass.counter);
}
});

Firebase recyclerAdapter duplicate objects into arrayList

final FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter(Service.class, R.layout.browse_service_detail, ServiceHolder.class, mReference){
#Override
protected void populateViewHolder(ServiceHolder serviceHolder, Service service, int position) {
serviceHolder.setServiceName(service.getName());
serviceHolder.setInfo("От " + service.getPrice1());
service.setQuantitySelected(service.getQuantityEnabled());
if (Order.getInstance().getServices() != null) {
for (Service serviceFromSingleton : Order.getInstance().getServices()) {
if (serviceFromSingleton.getName() == serviceHolder.getServiceName().getText().toString()) {
serviceHolder.getServiceName().setSelected(true);
serviceHolder.getServiceName().setTextColor(getResources().getColor(R.color.yellow));
}
}
}
//add item to array
servicesList.add(service);
}
}
};
When I run this activity, it records the visible list objects to an array, but when I scroll down and go back up, it duplicates the first elements again into the array. How to fix it? For an item to be added only once.
I don't think there is any issue in RecyclerAdapter..I think the list only inserting same data multiple times.
why not you check whether the list is empty or not before adding data into it and clear the data if its not empty and then add new.
if(servicesList.isEmpty())
servicesList.add(service);
//else clear and add data
else{
servicesList.clear();
servicesList.add(service);
}
To handle data duplicacy, you can use a Set which will ignore duplicate inserts on scrolling.
servicesList.add(service);
Set<Service> mSet= new HashSet<Service>();
mSet.addAll(servicesList);
servicesList.clear();
servicesList.addAll(mSet);
OR use Set other than ArrayList
little clumsy but will work for you.

ArrayList Length gets 0 in Singleton

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.

Dynamic Allocation of Array doesn't work for ArrayAdapter while assigning it for Spinner in Android

I am really facing a problem here to create a Spinner widget in Android. The goal is to populate a Spinner with data that i will dynamically retrieve from a source.
Now I am able to create a spinner with a data source that is implicitly declared in the program. But when ever i am trying to fetch the data from a dynamically created array, the apps throws a Force Close.
I will paste some demo examples to explain my problem here!
String[] SSID = new String[15];
String[] Data = {"Captain","America","Hulk","Ironman","Thor"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
addDevDialogue = new Dialog(this);
addDevDialogue.setContentView(R.layout.popup);
concat();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogue();
}
});
}
public void concat()
{
for(int i=0;i<5;i++)
{
SSID[i]=Data[i];
}
}
public void dialogue()
{
addDevDialogue.setTitle("Movies List");
addDevDialogue.setCancelable(true);
addDevDialogue.show();
spinList2 = (Spinner)addDevDialogue.findViewById(R.id.spinner2);
ArrayAdapter<String> listAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, SSID);
spinList2.setAdapter(listAdapter2);
}
The above code throws an error when ever I try to open the dialogue box.
I have tried this same sample with a pre-defined data source in place of "SSID" which yields a error free output!
I cannot understand why 'SSID[]' array doesnt work when I define it to the ArrayAdapter.
Any Insight will help!!!
You are call calling show() before populating adapter so call
addDevDialogue.show();
after
spinList2.setAdapter(listAdapter2);
UPDATE :
Once change size of SSID
String[] SSID = new String[Data.length];
Hope this will helps you.
Your string array String[] Data = {"Captain","America","Hulk","Ironman","Thor"}; is declared with 5 elements (index 0-4)
In your loop you loop 6 times
public void concat()
{
for(int i=0;i<5;i++)
{
SSID[i]=Data[i];
}
}
Which probably causes an Index out of bounds exception. Change your loop to this
public void concat()
{
for(int i=0;i<4;i++)
{
SSID[i]=Data[i];
}
}
#swarna: You are allocating a fixed array of 15 elements, then populating only 5 elements. The array adapter is probably getting tripped with the other 10 elements which have not been initialized. Suggest you make your SSID array to have only 5 elements OR if this is a dynamically determined value, you could keep ArrayList and keep adding to it. Then, when setting up the adapter do this:
YourObjList.add("one")
YourObjList.add("two")
YourObjList.add("three")
String[] SSID = YourObjList.toArray(new YourObjList[YourObjList.size()]);
ArrayAdapter<String> listAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, SSID);
spinList2.setAdapter(listAdapter2);
This will allow variable list sizes.
Hope this helps you

Android : Search from Large Arraylist

I have a recordset of about 29,000 records. My Screen contains EditText Box for Search Criteria and Listview containing all 29,000 records.
By searching with the listed way it takes time and not giving flow less output as I need.
My EditText contains
final EditText txtSearchCity = (EditText) findViewById(R.id.edtCity);
txtSearchCity.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
aCountryIDTemp.clear();
aCityStateTemp.clear();
for (int i = 0; i < aCountryID.size(); i++) {
if (aCityState
.get(i)
.toLowerCase()
.contains(
txtSearchCity.getText().toString()
.toLowerCase())) {
aCountryIDTemp.add(aCountryID.get(i));
aCityStateTemp.add(aCityState.get(i));
}
}
BindList();
}
});
}
BindList() method is setting the arraylist aCityStateTemp to adapter.
Any Other way to Search and Create new ArrayList dynamically.
I would insist to use Lambdaj Library which is mostly used in such cases where you want to restrict loops for sorting and filtering Collections.
Here is a small example for using lambdaj for filtering ArrayList.
ArrayList<String> sortedArrayList = select(arrList, having(on(String.class),
Matchers.containsString("a");
This will return a complete filtered ArrayList with which you want to populate your ListView.
You can also filter Custom Classes - Java: What is the best way to filter a Collection?
UPDATE:
Above solution was case-sensitive so to work around you can add Multiple Matchers.
Like this you can add Multiple Matchers,
ArrayList<String> sortedArrayList = select(arrList, having(on(String.class),
(Matchers.anyOf(Matchers.containsString("a"),Matchers.containsString("A")))));
UPDATE:
Even better way is to use filter(Matcher<?> matcher, T...array)
Here is how you can do that,
ArrayList<String> sortedArrayList = filter(Matchers.anyOf(
Matchers.containsString("a"),Matchers.containsString("A")), arrList);
Also, if you are interested in using some of the methods/features of lambdaj, you can extract the source and get it working. I am adding the same for filter()
You can just download hamcrest-all-1.0.jar(63 kb) and add below code to get the filter() working
public static <T> List<T> filter(Matcher<?> matcher, Iterable<T> iterable) {
if (iterable == null)
return new LinkedList<T>();
else{
List<T> collected = new LinkedList<T>();
Iterator<T> iterator = iterable.iterator();
if (iterator == null)
return collected;
while (iterator.hasNext()) {
T item = iterator.next();
if (matcher.matches(item))
collected.add(item);
}
return collected;
}
}
So, you can just sort out the least from lambdaj source and integrate in your source.
You can use HashSet or LinkedHashSet(keeps insertion order) for fast searching.
With contains() method of that classes.
I would assume that you have passed aCityStateTemp to you Adapter as ArrayList while initializing the Adapter
Now after changing the contents of aCityStateTemp, you just need to call adapter.notifyDataSetChanged(). you don't need to set the aCityStateTemp to adapter as new ArrayList.
You can store all data in a sqlite database and retrieve the searched item using like query.

Categories

Resources