Add custom object in Android - android

I am new to Android. I am trying to add a Custom object in a list. Below is my code.
GridItem items[];
if (motorList.length > 0){
for (int item:motorList) {
GridItem aItem = new GridItem(item,"no_image");
items.add(aItem);
}
}
How to achieve this?

There are a two big problems with your code:
You didn't initialize items but you are trying to use it
You can't call .add(...) on an array
-> You can either initialize an array with the size of motorlist and add the items via the index:
if (motorlist != null && motorlist.size() > 0) {
GridItem[] items = new GridItem[motorlist.size()];
for (int i = 0; i < motorlist.size(); i++) {
items[i] = new GridItem(motorlist.get(i), "no_image");
}
}
Or you could create a List instead of an array:
if (motorlist != null && motorlist.size() > 0) {
List<GridItem> items = new ArrayList<>();
for (int item : motrolist) {
GridItem aItem = new GridItem(item,"no_image");
items.add(aItem);
}
}
I'd recommend the second option.
Please note that both options assume that motorlist is a List

Related

How to compare two different lists and get the difference of both list?

I have two different lists, one is List<MainCategoriesAPI.Datum> datumList = new ArrayList<>(); and second is List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();.
What I want is to compare these two lists and get those ids which are not present in datumList. And then, I want to delete the data regarding these ids from SubCategoryEnt.
If you are targeting above Android N
List<String> a1 = Arrays.asList("2009-05-18", "2009-05-19", "2009-05-21");
List<String> a2 = Arrays.asList("2009-05-18", "2009-05-18", "2009-05-19", "2009-05-19", "2009-05-20", "2009-05-21","2009-05-21", "2009-05-22");
List<String> result = a2.stream().filter(elem -> !a1.contains(elem)).collect(Collectors.toList());
or you can use the Collection interface's removeAll method.
// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
add("apple");
add("orange");
}};
Collection secondList = new ArrayList() {{
add("apple");
add("orange");
add("banana");
add("strawberry");
}};
// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Remove all elements in firstList from secondList
secondList.removeAll(firstList);
// Show the "after" list
System.out.println("Result: " + secondList);
You have to use for loop to find the similar ids and the again use for loop to remove ids from datumList;
List<MainCategoriesAPI.Datum> datumList = new ArrayList<>();
List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();
List<Integer> results = new ArrayList<Integer>();// this is for storing same ids
To get different ids
// to get same ids
if (datumList.size() == subCategoryEnts1.size()) {
for (int i=0; i<datumList.size();i++){
int datIds = datumList.get(i);
for (int j=0; j<subCategoryEnts1.size();j++){
int subId = subCategoryEnts1.get(j);
if (datIds!=subId){
results.add(subId);
break;
}
}
}
}
to remove ids
// to remove same id
for (int i=0; i<results.size();i++){
int datIds = results.get(i);
for (int j=0; j<datumList.size();j++){
int subId = datumList.get(j);
if (datIds==subId){
datumList.remove(j);
break;
}
}
}
Hope this will help you.
Check below to find missing items of subCategoryEnts1
List missingIds = new ArrayList<SubCategoryEnt>();
for (SubCategoryEnt subCategory : subCategoryEnts1) {
for (MainCategoriesAPI.Datum datam : datumList) {
if (datam.id == subCategory.id){
missingIds.add(subCategory);
break;
}
}
}
Now remove those from subCategoryEnts1
subCategoryEnts1.removeAll(missingIds);

how to show some newest item listview?

I created a listview that connects to a database. I want to show some of the newest items (maybe 10 newest items) that will add in a random time? How can I accomplish this? This what I have tried:
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
But this doesn't work.
Sorry but, this code block makes no sense.
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
To explain what you have did;
you are getting the list from your database
initializing the list adapter
setting adapter to list
then reverse your order of the list (but you are not notifying the adapter)
You are doing this in a loop exactly 5 times.
What you need to do is:
get your list from database
then reverse your order of the list
initialize the list adapter
set adapter to list
I should look like this:
mKondisiList = myDbHelper.getListKondisi();
Collections.reverse(mKondisiList);
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
If you want to show only limited number of items, you can do something like below:
ArrayList<Object> fullList = new ArrayList<>();
fullList = myDbHelper.getListKondisi();
Collections.reverse(fullList);
//Add first 10 item to your mKondisiList
for (int i = 0; i < 10; i++) {
mKondisiList.add(fullList.get(i));
}
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);

populate custom listview from audio files inside a folder

I'm trying to get files from a folder and populate recyclerview based on the name of files using a custom adapter.
This is how I'm doing it:
In onBindViewHolder:
Product m = dataList.get(position);
//title
holder.title.setText(m.getTitle());
And :
void popList() {
Product product = new Product();
File dir = new File(mainFolder);//path of files
File[] filelist = dir.listFiles();
String[] nameOfFiles = new String[filelist.length];
for (int i = 0; i < nameOfFiles.length; i++) {
nameOfFiles[i] = filelist[i].getName();
product.setTitle(nameOfFiles[i]);
}
songList.add(product);
}
But the problem is, it just adds the first item.
I can't figure it out where should I loop to add it all.
You need to create separate product objects for items in loop and add it to list instead of creating a single Product object in list which will hold the last set data
void popList() {
Product product ;
File dir = new File(mainFolder);//path of files
File[] filelist = dir.listFiles();
String[] nameOfFiles = new String[filelist.length];
for (int i = 0; i < nameOfFiles.length; i++) {
// create product
product = new Product();
nameOfFiles[i] = filelist[i].getName();
product.setTitle(nameOfFiles[i]);
// add it to list
songList.add(product);
}
}
Your code walk through
void popList() {
Product product = new Product(); // one object
// ..code
for (int i = 0; i < nameOfFiles.length; i++) {
nameOfFiles[i] = filelist[i].getName();
product.setTitle(nameOfFiles[i]); // at the end of loop set last file name to object
}
songList.add(product); // one object in the list , end of story
}

Android: How to have a GridView filled with 2 adapters

How can I use two adapters on one GridView?
So I am trying to create a GridView, and fill it with two different types of info, so I tried using to adapters to do so, such as:
GridView gridView = (GridView) view.findViewById(R.id.gridView);
ArrayList<Grids> cellArray = new ArrayList<>();
GridAdapter cellAdapter = new GridAdapter(this.getActivity(), cellArray);
gridView.setAdapter(cellAdapter);
Grids arrayOfCells[] = new Grids[grid];
for (int j = 0; j < arrayOfCells.length; j++) {
arrayOfCells[j] = new Grids(false);
cellAdapter.add(arrayOfCells[j]);
}
ArrayList<Time> arrayOfTime = new ArrayList<>();
timeAdapter adapter = new timeAdapter(this.getActivity(), arrayOfTime);
gridView.setAdapter(adapter);
Time arrayGrid[] = new Time[grid];
for (int i = 0; i < arrayGrid.length; i++) {
arrayGrid[i] = new Time(arrayOfHours[i], arrayOfPeriod[i]);
adapter.add(arrayGrid[i]);
}
However, when I do so, only the second one is added to the gridView. So how would I go about doing this? Any help is greatly appreciated.
You can only set one Adapter instance on GridView. When you set the second adapter it's replacing the first adapter.
To solve this, you need to combine both data sources and put them inside of a single Adapter. One way to do it would be to create an ArrayList and add Grids and Time objects to it:
ArrayList<Object> adapterData = new ArrayList<Object>();
for (int j = 0; j < arrayOfCells.length; j++) {
adapterData.add(new Grids(false));
}
for (int i = 0; i < arrayGrid.length; i++) {
adapterData.add(new Time(arrayOfHours[i], arrayOfPeriod[i]));
}
TimeOrGridsAdapter adapter = new TimeOrGridsAdapter(this.getActivity(), adapterData);
gridView.setAdapter(adapter);
Note that inside of this new TimeOrGridsAdapter, you'll need to figure out whether the Object is a Time OR a Grids object and then create the view accordingly.

Adding a value to string array in android

i am trying enter link description here
as to add values to single as well as 2dim array dynamically,
but while adding values it shows null pointer ,
here is my code
Arr points1[];
points1 = new Arr[listItemList.size()];
for(int i=0;i<listItemList.size();i++)
{
ListItemReminderSummary listItem = listItemList.get(i);
Log.i("listItem.Car_Id", listItem.Car_Type);
points1[i].Car_Id = listItem.Car_Id;
points1[i].Car_Type = listItem.Car_Type;
}
for(int i=0;i<listItemList.size();i++)
{
System.out.println( points1[i].Car_Id + points1[i].Car_Type);
}
Null pointer at points1[i].Car_Id = listItem.Car_Id;
any suggestion,
thnx in advance.
initialize the items in Array...
for (int i = 0; i < listItemList.size(); i++) {
ListItemReminderSummary listItem = listItemList.get(i);
Log.i("listItem.Car_Id", listItem.Car_Type);
points[i] = new Arr();
points1[i].Car_Id = listItem.Car_Id;
points1[i].Car_Type = listItem.Car_Type;
}
You have to initialize cells of array.
for(int i=0;i<listItemList.size();i++){
points[i] = new Arr();
}
You have not Allocated memory to the Arr that is why you're trying to dereference an uninitialised pointer (i.e. writing to a random chunk of memory), which is undefined behaviour.
Change ur starting 2 lines
Arr points1[] = new Arr[listItemList.size()];
for (int i = 0; i < listItemList.size(); i++)
{
ListItemReminderSummary listItem = listItemList.get(i);
Log.i("listItem.Car_Id", listItem.Car_Type);
points1[i].Car_Id = listItem.Car_Id;
points1[i].Car_Type = listItem.Car_Type;
}
Did you make sure that listItemList.get(i) returns a value? Perhaps there is nothing returned from this.

Categories

Resources