In my Android app I have multiple activities using different array resources and I want to create one that has all of them. Is there an easy way to do this so that when I add an item to the string array it is automatically added to the "All" activity?
You need to create a generalized array in a class called ConstantCodes.java and declare your all arrays in it as follows,
public class ConstantCodes
{
public static String[][] arrayCollection = { { "A11","A22","A33" },
{ "B11","B22","B33" },
{ "C11","C22","C33" }
};
}
Calling FirstActivity.java
private String[] firstActivityArray = ConstantCodes.arrayCollection[0]; // this will return first stored array on 0th position.
Calling SecondActivity.java
private String[] secondActivityArray = ConstantCodes.arrayCollection[1]; // this will return first stored array on 1st position.
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 want to generate random number without duplicate, and i get this code
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random randomGenerator = new Random();
int random = randomGenerator.nextInt(16)+1;
if (!numbers.contains(random))
{
numbers.add(random);
}
I want to use this code to generate an random id to choose which question to be displayed from database. When i answer the question, it should be generate a new random id. But if I used all the code above, the array becomes a new one, and it will not know what id has been generated before.
If I put the ArrayList<Integer> numbers = new ArrayList<Integer>(); on onCreate and the other codes in a public int random(), the numbers in cannot read the array that i have created on onCreate.
I want to ask, can I create the ArrayList<Integer> as a public array, so i just should declare it once on onCreate method and the whole class can use this array.
You can declare the ArrayList outside the onCreate method and initialize it inside that method.
Then happily use it in your activity.
So, this:
ArrayList<Integer> numbers = new ArrayList<Integer>();
goes UP in your code, after the class declaration (after the first { in your class)
and this:
Random randomGenerator = new Random();
int random = randomGenerator.nextInt(16)+1;
if (!numbers.contains(random))
{
numbers.add(random);
}
Can be put in your onCreate method
Declare your array separately from the handling it, for instance as the global class one.
public class ClassName {
ArrayList<Integer> name;
#Override
public void onCreate(...){
name = new ArrayList<Integer>();
}
public void yourMethod(...){
//your operations
}
}
In that way you can keep your array. I wouldn't create a public array, because if you define it as global and private, you have an access to it from the whole class. If you want to get access from other classes in a package, just create usual method:
public ArrayList<Integer> getArrayList(){
return name;
}
btw, I'd use Set<Integer> (here are docs) to solve that problem, because it doesn't allow you add duplicated items, so you may get rid of redundant if statement.
For this example I have an array:
String[] books = new String[x];
I would like to store the id and title in each location:
books[0]=>id:0, title:"book title1"
books[0]=>id:1, title:"book title2"
books[0]=>id:2, title:"book title3"
books[0]=>id:3, title:"book title4"
I want to store the id since it may change. I'm getting the id and title from a database. Getting the info isn't the issue. I want to store it this way so in my other functions this returns to I can use something like:
btn.setText(regions[i].title)
Any suggestion on how to handle this would be great.
Do one thing, first create a bean class like BookBean.
Under this declare two variables ID and Title. and declare getters and setters (If u are using eclipse u can easily do this by (Source -> generate getters and setters.. option)
and then declare a ArrayList to store BookBean vale as of follow.
ArrayList<BookBean> bookArrayList=new ArrayList<BookBean>();
for(int i=0;i<=urSize;i++)
{
// create a object for BookBean
BookBean book =new BookBean();
book.setID("what ever");
book.setTitle("what ever");
bookArrayList.ass(book)
}
It is better to use Arraylist with custom class.
see this
class Book
{
String id,title;
/* Cunstructor to store data */
public Book(String id,String title)
{
this.id = id;
this.title = title;
}
}
//declare arraylist
ArrayList<Book> bookList = new ArrayList<Book>();
bookList.add("1","book1");
bookList.add("2","book2");
bookList.add("3","book3");
bookList.add("4","book4");
btn.setText(bookList.get(i).title)
I think you have several options
Use a HashMap where you can use your id as key and value title
Define a class and keep id and title as attributes , define get and set methods.
Keep the objects of the class in a ArrayList
I'm sorting an array of custom objects (ListData[]) on two fields. I want it to be sorted by theme, and them by name. I thought i made a nice comparator in the custom object class and that i could use Arrays.sort(ld) to make my code working and sorting my array. But apparently im doing something wrong...
my custom object:
public class ListData implements Comparable<ListData>{
public int venueID;
public String name;
public String photoUrl;
public String tip;
public String theme;
#Override
public int compareTo(ListData ld0) {
return this.venueID- ld0.venueID;
}
public static Comparator<ListData> ListDataThemeAndNameComparator = new Comparator<ListData>() {
#Override
public int compare(ListData ld1, ListData ld2) {
String compareTheme1 = ld1.theme.toUpperCase();
String compareTheme2= ld2.theme.toUpperCase();
String compareName1 = ld1.name.toUpperCase();
String compareName2= ld2.name.toUpperCase();
//ascending
int comp = compareTheme1.compareTo(compareTheme2); // comp themes
if(comp==0){ // same theme
comp= compareName1.compareTo(compareName2); // compare names
}
return comp;
}
};
}
And in my main activity i have:
ListData ld[]= new ListData[jsonResponse.size()];
(some code filling my ListData array)
Arrays.sort(ld, ListData.ListDataThemeAndNameComparator); // compare by theme and then by name
Does anyone know what i'm doing wrong?
I edited my code But still it fails, now on a nullpointerexception on the compareTheme1 = ld1.theme.toUpperCase();. But i am sure my array is not empty, i logged it the line before sorting it and its filled with about 500 items.
Your ListData object should implements Comparable not Comparator interface.
EDIT:
To make things clear, you can sort an array by Array.sort(). To make custom sort, you can specify your comparator in Array.sort(), if you don't do that, array will be sorted in natural order which you can define by implementing Comparable interface. So you have two options how to custom sort:
by using custom comparator and specifying it in Array.sort()
by implementing Comparable interface to your items
I would suggest you to go with implementing Comparable. You save memory by not creating new comparator objects and Comparator is useful if you are comparing objects of different types which is not your case.
How i can access ArrayList from one Activity to another and also clear ArrayList value?
you can use setter/getter method for it.
public class MySetGet
{
private ArrayList aList = null;
public void setList ( ArrayList aList )
{
this.aList = a.List;
}
public ArrayList getList ()
{
return aList;
}
}
Now you can set its value from any Activity/Class and get its value from any Activity/Class.
The most simply and possible way to do the desired::
1.Create a simple public class say Data..
Now create a public static your Array list object.
Now access any where..
Data.listObj
2.Create the List object as public static in one activity and use in another via,
SecondActivity.listObj.clear();