populate custom listview from audio files inside a folder - android

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
}

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

I m Adding ArrayList in FirebaseAppIndex but not able to search all list items from Google Search

I want to search app content from Google. I m adding ArrayList in FirebaseAppIndex but not able to search all list items from Google Search, I can able to search only the last item of arraylist.
ArrayList<String> titleList = new ArrayList<>();
titleList.add("ABC");
titleList.add("DEF");
titleList.add("GHI");
titleList.add("KLM");
ArrayList<Indexable> indexableNotes = new ArrayList<>();
for (int i = 0; i < titleList.size(); i++) {
Indexable noteToIndex = Indexables.noteDigitalDocumentBuilder()
.setName(titleList.get(i) + " Note")
.setText("Pierogi")
.setUrl("http://recipe-app.com/recipe/pierogi-poutine")
.build();
Log.d("onHandleIntent", "update");
indexableNotes.add(noteToIndex);
}
for (int i = 0; i < indexableNotes.size(); i++) {
Indexable[] notesArr = new Indexable[indexableNotes.size()];
notesArr = indexableNotes.toArray(notesArr);
Log.d("update", notesArr.toString());
FirebaseAppIndex.getInstance().update(notesArr);
}
}
it is always the same URL, so they overwrite each other and only the last one wins.
(you can also use the firebase appindexing debugging UI to look at the indexed results)

Add custom object in 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

how to show Selected Value In Spinner using array Adapter?

As i am newbie in android i want to show my saved spinner value at the time of view of saved form
how can i show database saved value at the time of view for spinner
here is my code
Java activity file
Spinner spnAECust;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ae_view_edit_sales);
spnAECust = (Spinner) findViewById(R.id.spnAECust);
/* Get Customer List and Add Select Default */
cust = con.getAllCustomers();// from database getting list
List<Customer> custList = new ArrayList<Customer>();
Customer c = new Customer();
c.setId(Constants.Common.ZERO);
c.setNm("Select Customer");
custList.add(c);
custList.addAll(cust);
// Create and fill an ArrayAdapter with a bunch of "Customer" objects
ArrayAdapter<Customer> custArrayAdapter = new ArrayAdapter<Customer>(this, android.R.layout.simple_spinner_item,
custList.toArray(new Customer[custList.size()]));
// Tell the spinner about our adapter
spnAECust.setAdapter(custArrayAdapter);
sa = con.getAirSalesActivityDetails(Integer.parseInt(saId));// get details from Sqlite
Customer cust = new Customer();
cust.setId(sa.getCustomerId());
spnAECust.setSelection(custArrayAdapter.getPosition(cust));// to set value saved in db
}
at tried setSelection but it maches index value rather than id value so i get abstract value to b selected please show me correct way to implement ...Thnks in advance
Here i got answer by my own
/* Get Customer List and Add Select Default */
cust = con.getAllCustomers();
List<Customer> custList = new ArrayList<Customer>();
Customer cst = new Customer();
cst.setId(Constants.Common.ZERO);
cst.setNm(Constants.Common.CUSTOMER_HINT);
custList.add(cst);
custList.addAll(cust);
/* Get Commodity List and Add Select Default */
comm = con.getAllCommodities();
List<Commodity> commList = new ArrayList<Commodity>();
Commodity cm = new Commodity();
cm.setId(Constants.Common.ZERO);
cm.setNm(Constants.Common.COMMODITY_HINT);
commList.add(cm);
commList.addAll(comm);
// Create and fill an ArrayAdapter with a bunch of "Customer" objects
ArrayAdapter<Customer> custArrayAdapter = new ArrayAdapter<Customer>(this, android.R.layout.simple_spinner_item,
custList.toArray(new Customer[custList.size()]));
int custIndex = 0;
// to set selected item
for (int r = 0; r < custArrayAdapter.getCount(); r++) {
if (sa.getCustomerId() == custArrayAdapter.getItem(r).getId()) {
custIndex = r;
break;
}
}
// Tell the spinner about our adapter
spnAECust.setAdapter(custArrayAdapter);
spnAECust.setSelection(custIndex);
// Create and fill an ArrayAdapter with a bunch of "Commodities" objects
ArrayAdapter<Commodity> commArrayAdapter = new ArrayAdapter<Commodity>(this, android.R.layout.simple_spinner_item,
commList.toArray(new Commodity[commList.size()]));
int commIndex = 0;
// to set selected item
for (int r = 0; r < commArrayAdapter.getCount(); r++) {
if (sa.getCommodityId() == commArrayAdapter.getItem(r).getId()) {
commIndex = r;
break;
}
}
// Tell the spinner about our adapter
spnAEComodity.setAdapter(commArrayAdapter);
spnAEComodity.setSelection(commIndex);
used for loop to get index for saved value
int commIndex = 0;
// to set selected item
for (int r = 0; r < commArrayAdapter.getCount(); r++) {
if (sa.getCommodityId() == commArrayAdapter.getItem(r).getId()) {
commIndex = r;
break;
}
}
and add index to
spnAEComodity.setSelection(commIndex);

Checked items in Multiple Choice Dialog

I'm displaying a List of Objects in a MultipleChoiceDialog. Another List contains all Objects who are already checked.
My Lists:
List<Participant> participants = datasourceParticipant.getAllParticipants();
List<Participant> participantsConference = datasourceParticipant.getAllParticipants(conference.getId());
In order to display them in the MultipleChoiceDialog, I build my List like this:
participantsNames = new ArrayList<String>();
for(int i = 0; i < this.participants.size(); i++) {
participantsNames.add(i, participants.get(i).getFirstname() + " " + participants.get(i).getLastname());
}
participantConferenceNames = new ArrayList<String>();
for(int i = 0; i < this.participantsConference.size(); i++) {
participantConferenceNames.add(i, participantsConference.get(i).getFirstname() + " " + participantsConference.get(i).getLastname());
}
Afterwards, I create the necessary String array ...
final CharSequence[] items = participantsNames.toArray(new CharSequence[participantsNames.size()]);
to display it in the MultipleChoiceDialog
builder.setMultiChoiceItems(items, null, null);
How do I add the checkedItems to the MultipleChoiceDialog. Or is there a much easier way to do it?
You have to pass in a boolean[] instead of null with the values that you want checked. The most straightforward way to accomplish this is to use a set:
Set<Participant> set = new HashSet();
set.addAll(datasourceParticipant.getAllParticipants(conference.getId()));
boolean[] checked = new boolean[participants.size()];
for (int i =0; i < participants.size(); i ++) {
checked[i] = set.contains(participants.get(i));
}
....
builder.setMultiChoiceItems(items, checked, null);
For that to work your Participant class must implement hashCode();

Categories

Resources