Failed to parse the row anko kotlin - android

i dont know why this code can't parse the row. please help.
private var dataCards :MutableList<DataCard> = mutableListOf()
btn_read.setOnClickListener {
showAllCardData()
var data = dataCards
tvResult.text = ""
for (i in 0..(data.size - 1)) {
tvResult.append(data.get(i).id.toString() + " " +
data.get(i).title + " " +
data.get(i).instruction + " " +
data.get(i).level + " " +
data.get(i).rewardPoint + " " +
data.get(i).rewardExp + " " +
data.get(i).inputdate + "\n")
}
}
private fun showAllCardData(){
missionDB.use {
val result = select(DataCard.TABLE_USERCARD)
val cards = result.parseList(classParser<DataCard>())
dataCards.addAll(cards)
}
}
Any reference or suggestion ?, the error is "failed to parse the row" please help.

row you get from showAllCardData function not match with DataCard,
example row you get from database (field1,field2)
but your Datacard constructor parameter (field1,field2,field3,field4)

Related

No way to get nextToken when using custom GraphQL query?[Amplify]

//MovieList function Defination
private fun getMovieList(): GraphQLRequest<MovieDBFinal2> {
val document = ("query getMovieList { "
+ "listMovieDBFinal2s(limit: 1000, filter: {Genre1: {eq: \"Animation\"}}) { "
+ "nextToken "
+ "items { "
+ "IMDB_title "
+ "} "
+ "} "
+ "} ")
return SimpleGraphQLRequest(
document,
Collections.emptyMap(),
TypeMaker.getParameterizedType(QueryResponse::class.java, MovieDBFinal2::class.java),
GsonVariablesSerializer())
}
internal class QueryResponse<T> {
private val listMovieDBFinal2s: QueryList<T>? = null
val moviesList: QueryList<T>?
get() = listMovieDBFinal2s
}
internal class QueryList<T> {
val items: Iterable<T>? = null
val nextToken: String? = null
}
//MovieList function call
Amplify.API.query(
getMovieList(),
{ response ->
if (response.hasData()) {
Log.e("MovieList", "$response")
}
},
{ failure ->
Log.e("MovieList", "Query failed.", failure)
}
)
I tried this type in my schema not working.Github issue
PaginationResult not working.Iterable also not giving any token. Only solution is String.class but that is difficult to serialize. The workaround is make the same request two times once with PaginationResult passing the nextToken as input and second make the same request with string.Class type.

Reading Transaction sms from the device not working for one plus devices

I have added a code to upload transaction sms from the device to the server, It works perfectly when tested on devices like -: POCO M2 Pro, Xiaomi K20 Pro, Samsung galaxy M30, LG Nexus 5 , Realme 1. But when tried to test on One Plus Devices like - 5T, Nord CE, Nord CE 2, the same filter doesn't work.
I have added filter code below please take a look
Thread(Runnable {
val smsDataList = arrayListOf<SmsData>()
val cr = ctx.contentResolver
val likeFilter = "address REGEXP '[a-zA-Z0-9][a-zA-Z0-9]-[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]' AND body LIKE '%credited%' "
val notLikeFilter1 = " AND body NOT LIKE '%credited to your wallet%' "
val notLikeFilter2 = " AND body NOT LIKE '%shopping%' "
val notLikeFilter3 = " AND body NOT LIKE '%paytm first games%' "
val notLikeFilter4 = " AND body NOT LIKE '%big bazaar wallet%' "
val notLikeFilter5 = " AND body NOT LIKE '%settlement%' "
val notLikeFilter6 = " AND body NOT LIKE '%personal loan%' "
val notLikeFilter7 = " AND body NOT LIKE '%get loan%' "
val notLikeFilter8 = " AND body NOT LIKE '%reward pts%' "
val notLikeFilter262 = " AND body NOT LIKE '%Waiting for Salary%' "
val notLikeFilter263 = " AND body NOT LIKE '%Get Salary%' "
val notLikeFilter264 = " AND body NOT LIKE '%you are eligible%'"
val notLikeFilter265 = " AND body NOT LIKE '%eligible for%'"
val notLikeFilter266 = " AND body NOT LIKE '%earn cashback%'"
val notLikeFilter267 = " AND body NOT LIKE '%Get Salary%'"
val notLikeFilter268 = " AND body NOT LIKE '%Auto Pay facility%'"
val notLikeFilter269 = " AND body NOT LIKE '%win upto%'"
val notLikeFilter270 = " AND body NOT LIKE '%order%'"
val notLikeFilter271 = " AND body NOT LIKE '%jiomart%' "
val notLikeFilter272 = " AND body NOT LIKE '%additional benefit%'"
val notLikeFilter273 = " AND body NOT LIKE '%mamapay%'"
val notLikeFilter274 = "AND body NOT LIKE '%credited within%'"
val notLikeFilter275 = "AND body NOT LIKE '%airtel wallet%'"
val filter = likeFilter +
notLikeFilter1 +
notLikeFilter2 +
notLikeFilter3 +
notLikeFilter4 +
notLikeFilter5 +
notLikeFilter6 +
notLikeFilter7 +
notLikeFilter8 +
notLikeFilter263+
notLikeFilter264+
notLikeFilter265+
notLikeFilter266+
notLikeFilter267+
notLikeFilter268+
notLikeFilter269+
notLikeFilter270+
notLikeFilter271+
notLikeFilter272+
notLikeFilter273+
notLikeFilter274+
notLikeFilter275
val c = cr.query(Telephony.Sms.Inbox.CONTENT_URI, null, filter, null, "date DESC")
var totalSMS = 0
if (c != null) {
totalSMS = c.count
if (c.moveToFirst()) {
for (j in 0 until totalSMS) {
try {
val smsData = SmsData()
smsData.setAddress(c.getString(c.getColumnIndexOrThrow("address")))
smsData.setBody(c.getString(c.getColumnIndexOrThrow("body")))
smsData.setDate(c.getString(c.getColumnIndexOrThrow("date")))
smsData.setMessageId(c.getInt(c.getColumnIndexOrThrow("_id")))
smsDataList.add(smsData)
} catch (e: Exception){
Log.e("SMS JOB", e.toString())
}
c.moveToNext()
}
}
c.close()
} else {
Log.e("SMS JOB", "No SMS found")
}}).start()
Please note that when I removed all the filters the sms did get uploaded from one plus device too.

Room nested query and subquery

I use Room in my Android project and want to write a complex query. I search about it and there is some answers witch says use #Embedded like this:
class TripAndListsAndListItems {
#Embedded
var trip: Trip? = null
#Relation(parentColumn = "creatorId", entityColumn = "remoteId", entity = User::class)
var user: List<User>? = null
#Relation(parentColumn = "remoteId", entityColumn = "tripId", entity = PlanitiList::class)
var lists: List<ListAndListItems>? = null
}
Here is complete article.
But then i have to figure it out in my code to extract my result using loops and so on.
I wrote my query in #Query with nested query and match columns with entity fields by using "as" like this:
Here is the ViewModel class:
class ServiceCard(
val id: Int,
val customerInfo: String,
val time: String,
val oilFilter: Boolean,
val airFilter: Boolean,
val gasFilter: Boolean,
val oil: Boolean
)
and #Doa has a #Query method like this:
#Dao
interface ServiceCardDao :ICommonDao<ServiceCard>{
#Query("SELECT s.services_id as id, " +
"s.user_mobile_no as customerInfo, " +
"( " +
"SELECT count(*) " +
"FROM service_detail as sd " +
"WHERE sd.services_id = s.services_id and sd.service_type_id = 1 " +
") as oilFilter, " +
"( " +
"SELECT count(*) " +
"FROM service_detail as sd " +
"WHERE sd.services_id = s.services_id and sd.service_type_id = 2 " +
") as airFilter, " +
"( " +
"SELECT count(*) " +
"FROM service_detail as sd " +
"WHERE sd.services_id = s.services_id and sd.service_type_id = 3 " +
") as gasFilter, " +
"( " +
"SELECT count(*) " +
"FROM service_detail as sd " +
"WHERE sd.services_id = s.services_id and sd.service_type_id = 4 " +
") as oil, " +
"s.service_date as time " +
"FROM services as s ")
fun selectAllServicesWithDetail(): LiveData<List<model.ServiceCard>>
}
Is there any advantage or disadvantage between these 2 ?
Both have advantages over the other.
The coding in the Dao is more complex for one, the other is more complex to code in the Entity but the greater complexity in the Dao is greater than the complexity difference of the Entities. So the simpler Dao may well win favour with some.
One would be more efficient as there is no post data retrieval looping to obtain the counts and additionally that SQLite is compiled C code rather than JVM bytecode which has to be interpreted so SQlite is typically very efficient. However, efficiency may well well be what some are willing to forsake for the sake of simpler coding and perhaps for what one is used to.
Some would perhaps consider alternatives such as DatabaseViews which combines Dao and Class as one.

Kotlin coroutines: why can't I see logs until the end?

I'm new with kotlin coroutines and have some doubts. So I'm trying to download a list of fonts using a kotlin coroutine, and I added some logs to see when a font is downloaded, or a message when it already existed. I was expecting to see one log each time a font is accessed, however I see only the progressBar, and when it gets hidden, I see all the logs at once. Am I doing something wrong?
private fun init() {
val job = Job()
val bgScope = CoroutineScope(Dispatchers.IO + job)
bgScope.launch {
getStuff()
}
}
fun getStuff() {
val uiScope = CoroutineScope(Dispatchers.Main + Job())
uiScope.launch {
progressbar.visibility = View.VISIBLE
}
for (font in jsonObject.fontList) {
if (!font.exists()) {
downloadFile(font)
Timber.d("file " + font.id + " downloaded: " + font.exists())
} else {
Timber.d("file " + font.id + " already exists ")
}
}
uiScope.launch {
progressbar.visibility = View.GONE
}
That's because your
for (font in jsonObject.fontList) {
if (!font.exists()) {
downloadFile(font)
Timber.d("file " + font.id + " downloaded: " + font.exists())
} else {
Timber.d("file " + font.id + " already exists ")
}
}
runs in another thread, and delays the response.Therefore, you should modify you progress visibility after the downloadFile has finished.
You should start the coroutine inside your downloadFileMethod() along with the progress bar switching on/off.

Why LinkedTreeMap is generated after deserializing to another class using Gson?

I have JSON structure like following:
{
"status": 0,
"error_message": null,
"data": {
"1": {
"7": [
{
"id": "1",
"person_id": "3",
"friend_id": "2"
}
]
}
}
As you've noticed, the number of indexes(1, 7) are dynamic, I mean, they can be more. So, to parse this JSON, I decided to use Map. 7 can be used as a key, when id, person_id, friend_id can be a class named, for example, Model. So, firstly, I tried to get 1 from data in JSON and convert it to Map as I described above. Here is how I did it:
val data = myJson.getAsJsonObject("data").get("1")
val gson = Gson()
val type = object : TypeToken<Map<String, ArrayList<Model>>>() {
}.type
val myMap: Map<String, ArrayList<Model>> = gson.fromJson(data, type)
Everything was alright, until I referred to some field of my Model. Here is how I did it:
First of all I tried to refer to myMap as following:
As you see, instead of getting Model, there are list of LinkedTreeMap. So, when I refer to the field of Model, I got CastException.
So, how to solve this problem? I tried this not using TypeToken, but it didn't help.
you gave the wrong json one single closure "}" I think you've copied wrong
create Gson2Application class
class Gson2Application
val jj = "{\n" +
"\"status\": 0,\n" +
"\"error_message\": null,\n" +
"\"data\": {\n" +
"\"1\": {\n" +
" \"7\": [\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"person_id\": \"3\",\n" +
" \"friend_id\": \"2\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"}"
data class Model(val id: Long, val person_id: Long, val friend_id: Long)
fun main(args: Array<String>) {
var myJson = JsonParser().parse(jj).asJsonObject
val data = myJson.getAsJsonObject("data").get("1")
val type = object : TypeToken<Map<String, ArrayList<Model>>>() {}.type // the Map is interfacem then gson get default LinkedTreeMap extedn AbstractMap<K,V> implements Map
val fromMapDefault = Gson().fromJson<Map<String, List<Model>>>(data, type)
printResult(fromMapDefault)
// result
// {{7=[Model(id=1, person_id=3, friend_id=2)]} class com.google.gson.internal.LinkedTreeMap--> 1}
// {[Model(id=1, person_id=3, friend_id=2)] class java.util.ArrayList--> 1}
// {Model(id=1, person_id=3, friend_id=2) class pl.jac.gson2.Model --> 2}
// {1 long--> 3}
val typeHashMap = object : TypeToken<HashMap<String, ArrayList<Model>>>() {}.type // the HashMap is an implementation
val fromHashMap = Gson().fromJson<Map<String, List<Model>>>(data, typeHashMap)
printResult(fromHashMap)
//result
// {{7=[Model(id=1, person_id=3, friend_id=2)]} class java.util.HashMap--> 1}
// {[Model(id=1, person_id=3, friend_id=2)] class java.util.ArrayList--> 1}
// {Model(id=1, person_id=3, friend_id=2) class pl.jac.gson2.Model --> 2}
// {1 long--> 3}
}
private fun printResult(fromMapDefault: Map<String, List<Model>>) {
println("""
result
{${fromMapDefault} ${fromMapDefault?.javaClass}--> 1}
{${fromMapDefault["7"]} ${fromMapDefault["7"]?.javaClass}--> 1}
{${fromMapDefault["7"]?.get(0)} ${fromMapDefault["7"]?.get(0)?.javaClass} --> 2}
{${fromMapDefault["7"]?.get(0)?.id} ${fromMapDefault["7"]?.get(0)?.id?.javaClass}--> 3}
""".trimIndent())
}
}
private fun printResult(fromMapDefault: Map<String, List<Model>>) {
println("""
result
{${fromMapDefault} ${fromMapDefault?.javaClass}--> 1}
{${fromMapDefault["7"]} ${fromMapDefault["7"]?.javaClass}--> 1}
{${fromMapDefault["7"]?.get(0)} ${fromMapDefault["7"]?.get(0)?.javaClass} --> 2}
{${fromMapDefault["7"]?.get(0)?.id} ${fromMapDefault["7"]?.get(0)?.id?.javaClass}--> 3}
""".trimIndent())
}
i change to yours model ClassStatus
data class ClassStatus(val status: Int, val error_message: String?, val data: Map<String,Map<String, ArrayList<Model>>>)
data class Model(val id: Long, val person_id: Long, val friend_id: Long)
fun main(args: Array<String>) {
val fromJson = Gson().fromJson<ClassStatus>(jj, ClassStatus::class.java)
printResult(fromJson)
//result
//ClassStatus(status=0, error_message=null, data={1={7=[Model(id=1, person_id=3, friend_id=2)]}}) class pl.jac.gson2.not.ClassStatus--> 1
//{1={7=[Model(id=1, person_id=3, friend_id=2)]}} class com.google.gson.internal.LinkedTreeMap--> 2
//{7=[Model(id=1, person_id=3, friend_id=2)]} class com.google.gson.internal.LinkedTreeMap--> 3
//[Model(id=1, person_id=3, friend_id=2)] class java.util.ArrayList--> 4
//Model(id=1, person_id=3, friend_id=2) class pl.jac.gson2.not.Model--> 5
//1 long--> 6
}
private fun printResult(fromMapDefault: ClassStatus) {
println("""
result
${fromMapDefault} ${fromMapDefault.javaClass}--> 1
${fromMapDefault.data} ${fromMapDefault.data.javaClass}--> 2
${fromMapDefault.data["1"]} ${fromMapDefault.data["1"]?.javaClass}--> 3
${fromMapDefault.data["1"]!!["7"]} ${fromMapDefault.data["1"]!!["7"]!!.javaClass}--> 4
${fromMapDefault.data["1"]?.get("7")!![0]} ${fromMapDefault.data["1"]!!["7"]!![0].javaClass}--> 5
${fromMapDefault.data["1"]?.get("7")!![0].id} ${fromMapDefault.data["1"]!!["7"]!![0].id!!.javaClass}--> 6
""".trimIndent())
}
val jj = "{\n" +
"\"status\": 0,\n" +
"\"error_message\": null,\n" +
"\"data\": {\n" +
"\"1\": {\n" +
" \"7\": [\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"person_id\": \"3\",\n" +
" \"friend_id\": \"2\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"}"

Categories

Resources