Arraylist value changes to last entered - android

In my kotlin project there is an arraylist in another one.When i enter values to array list it changes to last entered value . My code and o/p given below.
Question.class
`data class Question(
var id: Int,
var question: String,
var Answers:ArrayList<String>,
var correct: Int
)`
Main activity
if(it!!.exists())
{
for (h in it.children){
var ans : ArrayList<String> = ArrayList()
for (g in h.children)
{
when (g.key) {
"id" -> {
id = g.value.toString().toInt()
}
"question" -> {
ques = g.value.toString()
}
"correct" -> {
correct = g.value.toString().toInt()
}
"Answers" -> {
answers.clear()
for (i in g.children) {
answers.add(i.value.toString())
}
ans = answers
}
}
}
Log.d("12345",id.toString()+ques+correct+ans.toString())
var card = Question(id,ques,ans,correct)
questions.add(card)
Log.d("12345",questions.toString())
Output :
2023-01-12 11:27:30.716 27073-27073/com.example.firebase D/12345: 1Who was your favorite teacher ?1[pooja, anjali, rohit, jose]
2023-01-12 11:27:30.717 27073-27073/com.example.firebase D/12345: [Question(id=1, question=Who was your favorite teacher ?, Answers=[pooja, anjali, rohit, jose], correct=1)]
2023-01-12 11:27:30.718 27073-27073/com.example.firebase D/12345: 2What is one of your favorite smells?2[rose, lotus, sunflower]
2023-01-12 11:27:30.718 27073-27073/com.example.firebase D/12345: [Question(id=1, question=Who was your favorite teacher ?, Answers=[rose, lotus, sunflower], correct=1), Question(id=2, question=What is one of your favorite smells?, Answers=[rose, lotus, sunflower], correct=2)]
2023-01-12 11:27:30.719 27073-27073/com.example.firebase D/12345: 3What is your fav song?3[maroon, bts, back street biys]
2023-01-12 11:27:30.719 27073-27073/com.example.firebase D/12345: [Question(id=1, question=Who was your favorite teacher ?, Answers=[maroon, bts, back street biys], correct=1), Question(id=2, question=What is one of your favorite smells?, Answers=[maroon, bts, back street biys], correct=2), Question(id=3, question=What is your fav song?, Answers=[maroon, bts, back street biys], correct=3)]
2023-01-12 11:27:30.722 27073-27073/com.example.firebase D/12345: 4If you were a vegetable, what vegetable would you be?4[onion, pottato, brinjal, tomato]
2023-01-12 11:27:30.723 27073-27073/com.example.firebase D/12345: [Question(id=1, question=Who was your favorite teacher ?, Answers=[onion, pottato, brinjal, tomato], correct=1), Question(id=2, question=What is one of your favorite smells?, Answers=[onion, pottato, brinjal, tomato], correct=2), Question(id=3, question=What is your fav song?, Answers=[onion, pottato, brinjal, tomato], correct=3), Question(id=4, question=If you were a vegetable, what vegetable would you be?, Answers=[onion, pottato, brinjal, tomato], correct=4)]
Reference : ArrayList elements changed to last entry every time
How move first item of arraylist to last position?

Related

Room - Embedded object and the where clause with LiveData

This is sort of a continuation from my previous one, but I finally figured that one out (got rid of the duplication issue).
Android Room Relationship duplicating information
Customer table
#Entity(tableName = "customer_table")
public class Customer {
#ColumnInfo(name = "Customer_Serial", index = true)
#PrimaryKey
private int customerSerial;
#ColumnInfo(name = "Customer_Sort", index = true)
private String customerSort;
#ColumnInfo(name = "Customer_Name")
private String customerName;
public Customer(int customerSerial, String customerName) {
this.customerSerial = customerSerial;
this.customerName = customerName;
this.customerSort = String.format(Locale.ENGLISH, "%d-%d", new Date().getTime(), customerSerial);
}
}
Invoice table
#Entity(tableName = "invoice_table")
public class Invoice {
#ColumnInfo(name = "Invoice_Number", index = true)
#PrimaryKey
private int invoiceNumber;
#ColumnInfo(name = "Customer_Serial")
private int customerSerial;
#ColumnInfo(name = "Invoice_Sort", index = true)
private String invoiceSort;
#ColumnInfo(name = "Delivery_Status")
private int deliveryStatus;
public Invoice(int invoiceNumber, int customerSerial) {
this.invoiceNumber = invoiceNumber;
this.customerSerial = customerSerial;
this.invoiceSort = String.format(Locale.ENGLISH, "%d-%d", new Date().getTime(), invoiceNumber)
}
public void setDeliveryStatus(int deliveryStatus) {
this.deliveryStatus = deliveryStatus;
}
public int getDeliveryStatus() { return deliveryStatus; }
}
CustomerInvoice relation
public class CustomerInvoice {
#Embedded public Customer customer;
#Relation(
parentColumn = "Customer_Serial",
entityColumn = "Customer_Serial"
entity = Invoice.class
)
public List<Invoice> invoices;
}
DAO
public abstract class InvoiceDao {
#Transaction
#Query("SELECT * FROM invoice_table " +
"JOIN customer_table " +
"ON invoice_table.Debtor_Ser_No = customer_table.Customer_Serial " +
"WHERE invoice_table.Delivery_Status = :deliveryStatus " +
"GROUP BY customer_table.Customer_Serial " +
"ORDER BY customer_table.Customer_Sort, invoice_table.Invoice_Sort")
abstract public LiveData<List<CustomerInvoices>> getCustomerInvoices(int deliveryStatus);
abstract public void insert(Invoice... invoice);
#Insert(onConflict = OnConflictStrategy.IGNORE)
abstract public void insertCustomer(Customer... customer);
}
ViewModel
public LiveData<List> getCustomerInvoices(int deliveryStatus) { return dao.getCustomerInvoices(); }
Test
Invoice invoice1 = new Invoice(1234, 1);
Invoice invoice2 = new Invoice(1235, 1);
Invoice invoice3 = new Invoice(2468, 2);
Invoice invoice4 = new Invoice(2469, 2);
Customer customer1 = new Customer(1, "Customer 1");
Customer customer2 = new Customer(2, "Customer 2");
dao.insertCustomer(customer1);
dao.insertCustomer(customer2);
dao.insert(invoice1);
dao.insert(invoice2);
dao.insert(invoice3);
dao.insert(invoice4);
invoice1.setDeliveryStatus(0);
invoice2.setDeliveryStatus(0);
invoice3.setDeliveryStatus(0);
invoice4.setDeliveryStatus(0);
viewModel.getCustomerInvoices2(0).observe(getViewLifeCycleOwner(), list -> { ... });
If I debug the output of the observer, it returns correctly, 2 customers with 2 invoices each.
However, if I do
Test2
invoice1.setDeliveryStatus(1);
viewModel.getCustomerInvoices2(1).observe(getViewLifeCycleOwner(), list -> { ... });
It returns 1 customer with 2 invoices, instead of 1 customer with 1 invoice, as the 2nd invoice for that customer still has a delivery status of 0.
I realise the problem is in the CustomerInvoice relation where it is ignoring the where clause for the invoice_table itself (It still does the customer where clause perfectly).
However I just can't seem to wrap my head around to fix it.
I have Google searched for quite a while now, and I know it is because it is basically just doing 'Get customer where they have at least 1 invoice with the correct delivery status', then it is doing 'Get all invoices for this customer', just that pretty much everything I can find gives basic samples that don't involve LiveData at all, and I need it to be using LiveData.
One of the many attempts I tried to make it work, was to do a lot of the legwork in the viewmodel itself.
DAO
#Query("SELECT * FROM customer_table " +
"JOIN invoice_table " +
"ON customer_table.Customer_Serial = invoice_table.Debtor_Ser_No " +
"WHERE invoice_table.Delivery_Status = :deliveryStatus " +
"GROUP BY customer_table.Customer_Serial ORDER BY customer_table.Customer_Sort")
abstract public Maybe<List<Customer>> getCustomersByDeliveryStatus(int deliveryStatus);
#Query("SELECT * FROM invoice_table " +
"WHERE invoice_table.Debtor_Ser_No = :debtorSerial " +
"AND invoice_table.Delivery_Status = :deliveryStatus " +
"ORDER BY invoice_table.Invoice_Sort")
abstract public Single<List<Invoice>> getCustomerInvoicesByDeliveryStatus(int debtorSerial, int deliveryStatus);
ViewModel
public LiveData<List<Map<Customer, List<Invoice>>>> getCustomerInvoices2(int deliveryStatus) {
MutableLiveData<List<Map<Customer, List<Invoice>>>> liveCustomerInvoices = new MutableLiveData<>();
List<Map<Customer, List<Invoice>>> listCustomerInvoices = new ArrayList<>();
mInvoiceDao
.getCustomersByDeliveryStatus(deliveryStatus)
.subscribeOn(Schedulers.io())
.subscribe(
(customers) -> {
for (Customer customer : customers) {
mInvoiceDao.getCustomerInvoicesByDeliveryStatus(
customer.getCustomerSerial(),
deliveryStatus
).subscribeOn(Schedulers.io())
.subscribe(
(invoices) -> {
listCustomerInvoices.add(Collections.singletonMap(customer, invoices));
}
);
}
liveCustomerInvoices.postValue(listCustomerInvoices);
}, throwable -> Log.e("Error", "Error")
);
return liveCustomerInvoices;
}
While it does work (to a varying degree, the LiveData isn't updated instantly, so sometimes it shows nothing or sometimes it shows 1 thing only until I refresh the display), and my recyclerview shows exactly what I need it to show, it doesn't maintain the order based on 'Customer_Sort' and 'Invoice_Sort' which has to be maintained.
I understand why on that too, it's because 'map' doesn't guarantee order.
First issue I believe is that when you have #Embedded and then #Relation the #Embedded is considered the parents (Customers). That is Room basically ignores (at first) the children (Invoices).
You appear to be considering this from an Invoice perspective when Room considers it, as instructed by the #Embedded/#Relation, from the Customer perspective.
Once Room has (in theory) obtained the parents (customers) it then considers this from the object perspective and obtains ALL children (invoices), irrespective of SQL (e.g. WHERE ORDER) that affects the children retrieved and builds complete objects (all children for the parent).
The WHERE and ORDER are only affectual if it changes the number of parents.
This basically a convenience approach.
To affect the children (Invoices), prune them, sort them if using the Customer(#Embedded)Invoice(#Realtion) POJO needs a means of overriding Rooms handling.
Another issue is that your testing code changes the Invoice objects (e.g. invoice1.setDeliveryStatus(0);) but does not apply that change to the database. So if you extract from the database then those changes will not have been applied.
Without changing the CustomerInvoice class. Consider the following:-
Getters and setters added to the Customer and Invoice classes.
InvoiceDao transformed to be :-
#Dao
public abstract class InvoiceDao {
/*
#Transaction
#Query("SELECT * FROM invoice_table " +
"JOIN customer_table " +
"ON invoice_table.Debtor_Ser_No = customer_table.Customer_Serial " +
"WHERE invoice_table.Delivery_Status = :deliveryStatus " +
"GROUP BY customer_table.Customer_Serial " +
"ORDER BY customer_table.Customer_Sort, invoice_table.Invoice_Sort")
abstract public LiveData<List<CustomerInvoice>> getCustomerInvoices(int deliveryStatus);
*/
#Insert(onConflict = OnConflictStrategy.IGNORE)
abstract public void insert(Invoice... invoice);
#Insert(onConflict = OnConflictStrategy.IGNORE)
abstract public void insertCustomer(Customer... customer);
#Update(onConflict = OnConflictStrategy.IGNORE)
abstract public void updateInvoice(Invoice... invoices);
#Query("SELECT customer_table.* " + /* Room does not use the invoice table columns, they are not needed */
"FROM customer_table " +
"JOIN invoice_table ON invoice_table.customer_serial = customer_table.Customer_Serial " +
"WHERE invoice_table.Delivery_Status = :deliveryStatus " +
"GROUP BY customer_table.Customer_Serial ORDER BY customer_table.Customer_Sort, invoice_table.Invoice_Sort")
abstract public List<Customer> getApplicableCustomers(int deliveryStatus);
#Query("SELECT * FROM invoice_table WHERE delivery_status=:deliveryStatus AND customer_serial=:customerSerial ORDER BY invoice_sort" )
abstract List<Invoice> getApplicableInvoicesForCustomer(int deliveryStatus, int customerSerial);
#Transaction /* do in a single transaction */
#Query("") /* trick room so it applies transaction processing logic*/
public List<CustomerInvoice> getCustomerInvoices(int deliveryStatus) {
ArrayList<CustomerInvoice> rv = new ArrayList<>();
for(Customer c: getApplicableCustomers(deliveryStatus)) {
CustomerInvoice ci = new CustomerInvoice();
ci.customer = c;
ci.invoices = getApplicableInvoicesForCustomer(deliveryStatus,c.getCustomerSerial());
rv.add(ci);
}
return rv;
}
}
getCustomerInvoices method commented out
updateInvoice method added
getApplicableCustomers method added
similar to getCustomerInvoices but only gets the Customer fields/column as Room doesn't use the Invoice (the SQL could well be trimmed accordingly).
Instead of Debtor_Ser_No customer_serial hard coded.
getApplicableInvoicesForCustomer method added (for getting the appropriate invoices)
getCustomerInvoices method replaced using a method with body that gets the Customers, as Room does, but then gets the required children (invoices). This combining the getApplicable?? methods and returning the List of CustomerInvoice objects.
To demonstrate a modified Test, that a) updates the invoice delivery_status values in the database and b) uses a method to log the returned CustomerInvoices which allows the required delivery status to be passed:-
Note for brevity the mainThread is used.
public class MainActivity extends AppCompatActivity {
TheDatabase db;
InvoiceDao dao;
private static final String TAG = "DBINFO";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = TheDatabase.getInstance(this);
dao = db.getInvoiceDao();
Invoice invoice1 = new Invoice(1234, 1);
Invoice invoice2 = new Invoice(1235, 1);
Invoice invoice3 = new Invoice(2468, 2);
Invoice invoice4 = new Invoice(2469, 2);
Customer customer1 = new Customer(1, "Customer 1");
Customer customer2 = new Customer(2, "Customer 2");
dao.insertCustomer(customer1);
dao.insertCustomer(customer2);
dao.insert(invoice1);
dao.insert(invoice2);
dao.insert(invoice3);
dao.insert(invoice4);
logCustomerInvoices(0,"_R1");
logCustomerInvoices(1,"_R2");
invoice1.setDeliveryStatus(0);
invoice2.setDeliveryStatus(0);
invoice3.setDeliveryStatus(0);
invoice4.setDeliveryStatus(0);
dao.updateInvoice(invoice1);
dao.updateInvoice(invoice2);
dao.updateInvoice(invoice3);
dao.updateInvoice(invoice4);
logCustomerInvoices(0,"_R3");
logCustomerInvoices(1,"_R4");
invoice1.setDeliveryStatus(1);
invoice2.setDeliveryStatus(0);
invoice3.setDeliveryStatus(0);
invoice4.setDeliveryStatus(0);
dao.updateInvoice(invoice1);
dao.updateInvoice(invoice2);
dao.updateInvoice(invoice3);
dao.updateInvoice(invoice4);
logCustomerInvoices(0,"_R5");
logCustomerInvoices(1,"_R6");
}
void logCustomerInvoices(int deliveryStatus, String tagSuffix) {
for(CustomerInvoice ci: dao.getCustomerInvoices(deliveryStatus)) {
Log.d(TAG+tagSuffix,"Customer is " + ci.customer.getCustomerName() +
" Serial is " + ci.customer.getCustomerSerial() +
" Sort is " + ci.customer.getCustomerSort() + " There are " + ci.invoices.size() + " Invoices. They are ");
for (Invoice i: ci.invoices) {
Log.d(TAG+tagSuffix,"\n\tInvoice # is " + i.getInvoiceNumber() + " CustSerial is " + i.getCustomerSerial() + " DlvrStatus is " + i.getDeliveryStatus() + " Sort is " + i.getInvoiceSort());
}
}
}
}
When run 6 sets (3 pairs) of results may be output to the log. The output being:-
2023-02-04 06:53:16.867 D/DBINFO_R1: Customer is Customer 1 Serial is 1 Sort is 1675453996772-1 There are 2 Invoices. They are
2023-02-04 06:53:16.868 D/DBINFO_R1: Invoice # is 1234 CustSerial is 1 DlvrStatus is 0 Sort is 1675453996766-1234
2023-02-04 06:53:16.868 D/DBINFO_R1: Invoice # is 1235 CustSerial is 1 DlvrStatus is 0 Sort is 1675453996771-1235
2023-02-04 06:53:16.869 D/DBINFO_R1: Customer is Customer 2 Serial is 2 Sort is 1675453996772-2 There are 2 Invoices. They are
2023-02-04 06:53:16.869 D/DBINFO_R1: Invoice # is 2468 CustSerial is 2 DlvrStatus is 0 Sort is 1675453996771-2468
2023-02-04 06:53:16.869 D/DBINFO_R1: Invoice # is 2469 CustSerial is 2 DlvrStatus is 0 Sort is 1675453996771-2469
2023-02-04 06:53:16.887 D/DBINFO_R3: Customer is Customer 1 Serial is 1 Sort is 1675453996772-1 There are 2 Invoices. They are
2023-02-04 06:53:16.887 D/DBINFO_R3: Invoice # is 1234 CustSerial is 1 DlvrStatus is 0 Sort is 1675453996766-1234
2023-02-04 06:53:16.887 D/DBINFO_R3: Invoice # is 1235 CustSerial is 1 DlvrStatus is 0 Sort is 1675453996771-1235
2023-02-04 06:53:16.887 D/DBINFO_R3: Customer is Customer 2 Serial is 2 Sort is 1675453996772-2 There are 2 Invoices. They are
2023-02-04 06:53:16.887 D/DBINFO_R3: Invoice # is 2468 CustSerial is 2 DlvrStatus is 0 Sort is 1675453996771-2468
2023-02-04 06:53:16.887 D/DBINFO_R3: Invoice # is 2469 CustSerial is 2 DlvrStatus is 0 Sort is 1675453996771-2469
2023-02-04 06:53:16.906 D/DBINFO_R5: Customer is Customer 1 Serial is 1 Sort is 1675453996772-1 There are 1 Invoices. They are
2023-02-04 06:53:16.906 D/DBINFO_R5: Invoice # is 1235 CustSerial is 1 DlvrStatus is 0 Sort is 1675453996771-1235
2023-02-04 06:53:16.906 D/DBINFO_R5: Customer is Customer 2 Serial is 2 Sort is 1675453996772-2 There are 2 Invoices. They are
2023-02-04 06:53:16.906 D/DBINFO_R5: Invoice # is 2468 CustSerial is 2 DlvrStatus is 0 Sort is 1675453996771-2468
2023-02-04 06:53:16.906 D/DBINFO_R5: Invoice # is 2469 CustSerial is 2 DlvrStatus is 0 Sort is 1675453996771-2469
2023-02-04 06:53:16.911 D/DBINFO_R6: Customer is Customer 1 Serial is 1 Sort is 1675453996772-1 There are 1 Invoices. They are
2023-02-04 06:53:16.911 D/DBINFO_R6: Invoice # is 1234 CustSerial is 1 DlvrStatus is 1 Sort is 1675453996766-1234
R1 (as delivery status's are all 0 returns 2 Customers with 2 Invoices each)
R2 returns nothing as there are no Customers with invoices with a delivery status of 1
The invoice updates do nothing as the status is already 0, so:-
R3 returns all
R4 nothing
As 1 invoice is changed to have a delivery status of 1 then
R5 returns 2 Customer but the first with 1 invoice (as expected) and the other with 2 invoices as expected
R6 returns 1 Customer with 1 Invoices who's status is 1
Customers and Invoices sorted accordingly.

Dart Comparison of Objects within a list

You will see I am trying that when I pass him three objects, he only stays with the objects that do not repeat themselves, that is, regarding the following:
Person p = Person("nombre", "apellido");
Person p2 = Person("nombre", "apellido");
Person p3 = Person("nombre2", "apellido2");
var list = [ p, p2, p3 ];
Set<Persona> set2 = {...list};
var i = 1;
filteredList.forEach((element) {
print("Person " + i.toString() + ": " + element.toString());
i++;
});
It should return something like this:
Person 1: Person{nombre: nombre, apellido: apellido}
Person 3: Person{nombre: nombre2, apellido: apellido2}
The Person class for now is like this:
Person 1: Person{nombre: nombre, apellido: apellido}
Person 2: Person{nombre: nombre, apellido: apellido}
Person 3: Person{nombre: nombre2, apellido: apellido2}
La clase Persona por aora esta tal que asi:
class Person {
String nombre;
String apellido;
Person(this.nombre, this.apellido);
#override
bool operator ==(other) {
return (other is Person)
&& other.nombre == nombre
&& other.apellido == apellido;
}
#override
String toString() {
return 'Person{nombre: $nombre, apellido: $apellido}';
}
}
Two objects that should be equal are not getting detected by the set implementation, so what's going on here?
The Set documentation gives us our first clue:
The default Set implementation, LinkedHashSet, considers objects indistinguishable if they are equal with regard to operator Object.==.
== is implemented correctly here, though. Lets see what LinkedHashSet has to say about equality:
The elements of a LinkedHashSet must have consistent Object.== and Object.hashCode implementations. This means that the == operator must define a stable equivalence relation on the elements (reflexive, symmetric, transitive, and consistent over time), and that hashCode must be the same for objects that are considered equal by ==.
While == is implemented in your class, hashCode is not. This is an issue, because as we can see, the default Set implementation relies on it.
Luckily, Dart 2.14 makes your job quite easy:
int get hashCode => Object.hash(nombre, apellidio);

Android Studio Sqlite same name only 1 time listing

Trying to learn Android studio. And I expect your help on this.I am adding and listing data with sqlite.
for example;
id - name - value
1 - john - 100
2 - mark - 200
3 - john - 150
4 - john - 200
5 - adam - 400
what I want to do, list only names one time.
1 - john
2 - mark
3 - adam
private void showlist() {
ArrayList<DataListItems> contactList = new ArrayList<DataListItems>();
contactList.clear();
String query = "SELECT * FROM data ";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
DataListItems contactListItems = new DataListItems();
contactListItems.setid(c1.getString(c1
.getColumnIndex("id")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setValue(c1.getString(c1
.getColumnIndex("value")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
DataListAdapter contactListAdapter = new DataListAdapter(
SiteList.this, contactList);
lvCustomList.setAdapter(contactListAdapter);
}
You can use the GROUP BY name to select only one name. However, the id selected would be an arbitrary id for each group (name).
your code could use String query = "SELECT * FROM data GROUP BY name";
If you wanted the first/lowest id per name then you could use min(id) in conjunction with GROUP BY NAME.
your code could use String query = "SELECT min(id) AS id, name, value FROM data GROUP BY name";
You say that your expected result should be
1 - john
2 - mark
3 - adam
That would be more complicated (and perhaps of little use) as the id for adam is 5 not 3 (I assume that is simply a typing error).

List from model Kotlin not neatly arranged

I try to get list from mutable list and parse it to array become spinner array, here is my code;
fun getListLeague(context: Context): MutableList<League> {
val league: MutableList<League> = mutableListOf()
val leagueName = context.resources.getStringArray(R.array.leagueName)
val leagueId = context.resources.getStringArray(R.array.leagueId)
league.clear()
for (i in leagueName.indices) {
league.add(League(leagueName[i], leagueId[i]))
}
return league
}
Then I call it in my spinner in fragment, here is the code;
private lateinit var leagues: List<League>
leagues = getListLeague(requireContext())
val spinAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, leagues)
Log.e("LIST LEAGUE PREV", leagues.toString())
I try to see the list from Log above, and I expect the result is like below;
E/LIST LEAGUE PREV: English Premier League, English League Championship, Scottish Premier League, German Bundesliga, Italian Serie A, French Ligue 1, Spanish La Liga, so on :)
Then I try to run the app, but the log result is appear like below:
E/LIST LEAGUE PREV: [League(leagueName=English Premier League, leagueId=4328), League(leagueName=English League Championship, leagueId=4329), League(leagueName=Scottish Premier League, leagueId=4330), League(leagueName=German Bundesliga, leagueId=4331), League(leagueName=Italian Serie A, leagueId=4332), League(leagueName=French Ligue 1, leagueId=4334), League(leagueName=Spanish La Liga, leagueId=4335), League(leagueName=Greek Superleague Greece, leagueId=4336), League(leagueName=Dutch Eredivisie, leagueId=4337), League(leagueName=Belgian Jupiler League, leagueId=4338), League(leagueName=Turkish Super Lig, leagueId=4339), League(leagueName=Danish Superliga, leagueId=4340), League(leagueName=Portuguese Primeira Liga, leagueId=4344), League(leagueName=American Major League Soccer, leagueId=4346), League(leagueName=Swedish Allsvenskan, leagueId=4347), League(leagueName=Mexican Primera League, leagueId=4350), League(leagueName=Brazilian Brasileirao, leagueId=4351), League(leagueName=Ukrainian Premier League, leagueId=4354), League(leagueName=Russian Football Premier League, leagueId=4355), League(leagueName=Australian A-League, leagueId=4356), League(leagueName=Eliteserien, leagueId=4358), League(leagueName=Chinese Super League, leagueId=4359)]
The result is under my expectation, so where have I gone wrong?
You are getting both leagueID and leagueName in your getListLeague
val leagueId = context.resources.getStringArray(R.array.leagueId)
league.add(League(leagueName[i], leagueId[i]))
And you are returning both attributes,
return league
This is why you are getting leagueID in your Log.
Secondly, you are calling this
leagues = getListLeague(requireContext())
Log.e("LIST LEAGUE PREV", leagues.toString())
so you get your result as
League(leagueName=English Premier League
My suggestion is to understand what you're doing first before making changes to your codes.

How to array listing index from Firebase on Android

0 --> David
1 --> Alice
2 --> Jayden
3 --> Nicole
4 --> David
this sequence shows 0 and 4 David's records. I want to find David's records and find index values.
find --> David
Situated shows list --> data to recyclerview listing
i want to do
My Firebase ;
"Node" : {
"-L1pFRQg8anT4_-4V60B" : {
"name" :"David"
"note" : " firs note"
}, "-L1pFRQg8anT4_-4V2342" : {
"name" :"Alice"
"note" :"note values"
}, "-L1pFRQg8anT4_-65656" : {
"name" :"Jayden"
"note" :"note values"
}, "-L1pFRQg8anT4_-sajkfhsaj" : {
"name" :"Nicole"
"note" :"note values"
}, "-L1pFRQg8anT4_-jwquh24" : {
"name" :"David"
"note" :"second note"
},
I know how to read and print data with Firebase. I know how to use FirebaseRecyclerview. But I have to find out in what order David is in the order of number. So if I called "David", it would be easy to show Davids in Recyclerview. I can do that. It is difficult to print the rank value of "David" among other data. etc 0-4
Query query = FirebaseDatabase.getInstance ()
.getreferen by ()
.child ( "node")
.orderbychild ( "name");
When I sort by name, I want to get "David" in position number and print it as "note" data and "position number".
I want to do index values

Categories

Resources