У меня проблема с приложением в Android, которое я создаю. При создании операции с ее фрагментами данные не отображаются в операции. Я должен перезагрузить упражнение во второй раз, чтобы иметь данные на их месте. Затем, каждый раз, когда я меняю данные, мне приходится всегда перезагружать их, чтобы данные отображались правильно. Я понял, что должен обрабатывать данные перед созданием представления, но не знаю как. Это код основной деятельности:
package com.example.example
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Layout
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.example.ui.traffic.TrafficFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.json.JSONObject
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
companion object{
//ricarica pagina
var pagina: Int = 0
//controllo del login automatico
var checkLog: String? = String()
//url di example
var url: String? = String()
//variabili del login
var dominio: String? = String()
var token: String? = String()
var nome: String? = String()
var status: String? = String()
var email: String? = String()
//variabili traffic
var totalClicksTraffic: String? = String()
var totalBlockedTraffic: String? = String()
var totalClickAdwordsTraffic: String? = String()
var clickArray: List<TrafficClick> = arrayListOf()
var clicksNumber: Int = 0
var statusTraffic: String? = String()
//variabili change button filter
var changeDataTraffic: String? = String()
var changeDataSearchTerms: String? = String()
//variabili ipsblocked
var statusIpBlocked: String? = String()
var ipsNumber: Int = 0
var ipsArray: List<IpListBlock> = arrayListOf()
//variabili searchterms
var statusSearchTerms: String? = String()
var searchTermsLength: Int = 0
var searchArray: List<SearchTermsList> = arrayListOf()
var totalClicksSearch: String? = String()
var totalBlockedSearch: String? = String()
var totalClickAdwordsSearch: String? = String()
}
override fun onCreate(savedInstanceState: Bundle?) {
// try {
//dichiaro lo sharedpreferences
val sharedPreferences = this.getSharedPreferences("TOKEN_FILE", Context.MODE_PRIVATE)
//dichiato l'editor per modificare gli sharedpreferences
var editorMain = sharedPreferences.edit()
Log.d("Test", "Eccomi qui--- $changeDataTraffic")
editorMain.putString("changeDataTraffic", changeDataTraffic)
editorMain.apply()
//porto l'url e il token in variabili globali in modo da poterli richiamare in caso di mancato context
url = sharedPreferences.getString("URL", "")
token = sharedPreferences.getString("TOKEN", "")
//RICHIESTE PER LA DASHBOARD DI PAGINA TRAFFIC-------------------------------------------------------------
//richiesta da fare con il volley
val queue = Volley.newRequestQueue(this)
val endPointTrafficDashboard = "homeApp?"
val requestUrlTrafficDashboard = sharedPreferences.getString(
"URL",
""
) + endPointTrafficDashboard + "token=" + sharedPreferences.getString(
"TOKEN",
""
) + "&filter=" + changeDataTraffic
val stringRequest = StringRequest(
Request.Method.GET,
requestUrlTrafficDashboard,
Response.Listener<String> { response ->
Log.d("Test", "PERFETTO ---- $response")
val jsonObj: JSONObject = JSONObject(response.toString())
//prelevo le variabili del json array portandole in stringa
val status = jsonObj.getInt("status")
val clicks = jsonObj.getJSONArray("clicks")
val bloccati = jsonObj.getInt("bloccati")
val totalClicksAdwords = jsonObj.getInt("totalClicksAdwords")
statusTraffic = status.toString()
clicksNumber = clicks.length()
var clickString = clicks.toString()
Log.d("array", "$clickString")
Log.d("clicks", "$clicks")
for (i in 0 until clicks.length()) {
val item = clicks.getJSONObject(i)
Log.d("item", "$item")
Log.d("item", item.getString("id"))
var click = TrafficClick(
item.getString("id"),
item.getString("ip"),
item.getString("adwords"),
item.getString("browser"),
item.getString("keyword"),
item.getString("id_campaign"),
item.getString("os"),
item.getString("mobile"),
item.getString("guid"),
item.getString("referer"),
item.getString("from_referer"),
item.getString("organization"),
item.getString("data_creation"),
item.getString("data_update"),
item.getString("eliminato"),
item.getString("counter"),
item.getString("id_dominio"),
item.getString("id_geo_ip"),
item.getString("iso_code")
)
if (click.eliminato != null && click.eliminato != "") {
(clickArray!! as ArrayList).add(click)
}
}
Log.d("clicks", "$clickArray")
totalClicksTraffic = clicks.length().toString()
totalClickAdwordsTraffic = totalClicksAdwords.toString()
totalBlockedTraffic = bloccati.toString()
},
Response.ErrorListener { response ->
Log.d("Test", "ERRORE ---- $response")
}
)
//aggiunta al queue della stringa precedentemente formata
queue.add(stringRequest)
Log.d("Test", "URL ---- $requestUrlTrafficDashboard")
Log.d("Test", "CHANGEDATA ---- $changeDataTraffic")
//FINE DELLA DASHBOARD DI PAGINA TRAFFIC------------------------------------------------------------------- FINE FINE FINE
//RICHIESTE PER LA PAGINA IPBLOCKED------------------------------------------------------------------------
//richiesta da fare con il volley
val queueIpBlockedPage = Volley.newRequestQueue(this)
val endPointIpBlockedPage = "listIpBlocked?"
val requestUrlIpBlockedPage = sharedPreferences.getString(
"URL",
""
) + endPointIpBlockedPage + "token=" + sharedPreferences.getString("TOKEN", "")
val stringRequestIpBlocked = StringRequest(
Request.Method.GET,
requestUrlIpBlockedPage,
Response.Listener<String> { response ->
Log.d("TestIp", "PERFETTO IP ---- $response")
val jsonObj: JSONObject = JSONObject(response.toString())
//prelevo le variabili del json array portandole in stringa
val status = jsonObj.getInt("status")
val ips = jsonObj.getJSONArray("ips")
statusIpBlocked = status.toString()
ipsNumber = ips.length()
var ipsString = ips.toString()
Log.d("array", "$ipsString")
Log.d("ips", "$ips")
for (i in 0 until ips.length()) {
val item = ips.getJSONObject(i)
Log.d("item", "$item")
Log.d("item", item.getString("id"))
var ip = IpListBlock(
item.getInt("id"),
item.getInt("id_user"),
item.getInt("id_dominio"),
item.getInt("id_criterion"),
item.getInt("id_adwords_campaign"),
item.getInt("id_adwords_customer"),
item.getString("ip"),
item.getString("trigger"),
item.getString("created_at"),
item.getString("removal_date"),
item.getString("unlimited"),
item.getString("updated_at"),
item.getString("deleted_at"),
item.getInt("id_click"),
item.getString("organization"),
item.getString("iso_code")
)
(ipsArray!! as ArrayList).add(ip)
}
Log.d("ips", "$ipsArray")
},
Response.ErrorListener { response ->
Log.d("TestIp", "ERRORE IP ---- $response")
}
)
//aggiunta al queue della stringa precedentemente formata
queueIpBlockedPage.add(stringRequestIpBlocked)
Log.d("TestIp", "URL ---- $requestUrlIpBlockedPage")
//FINE PER LA PAGINA IPBLOCKED-----------------------------------------------------------------------------FINE FINE FINE
//RICHIESTE PER LA PAGINA SEARCH TERMS---------------------------------------------------------------------
//richiesta da fare con il volley
val queueSearchTermsPage = Volley.newRequestQueue(this)
val endPointSearchTermsPage = "reportApp?"
val requestUrlSearchTermsPage = sharedPreferences.getString(
"URL",
""
) + endPointSearchTermsPage + "token=" + sharedPreferences.getString(
"TOKEN",
""
) + "&filter=" + changeDataSearchTerms
val stringRequestSearchTerms = StringRequest(
Request.Method.GET,
requestUrlSearchTermsPage,
Response.Listener<String> { response ->
Log.d("TestSearchTerms", "PERFETTO SEARCH ---- $response")
val jsonObj: JSONObject = JSONObject(response.toString())
//prelevo le variabili del json array portandole in stringa
val statusSearchTerms = jsonObj.getInt("status")
val searchTerms = jsonObj.getJSONArray("searchTerms")
val clicks = jsonObj.getInt("clicks")
val bloccati = jsonObj.getInt("bloccati")
val totalClickAdwords = jsonObj.getInt("bloccati")
MainActivity.statusSearchTerms = statusSearchTerms.toString()
totalClicksSearch = clicks.toString()
totalBlockedSearch = bloccati.toString()
totalClickAdwordsSearch = totalClickAdwords.toString()
searchTermsLength = searchTerms.length()
var searchTermsString = searchTerms.toString()
Log.d("array", "$searchTermsString")
Log.d("search", "$searchTerms")
for (i in 0 until searchTerms.length()) {
val item = searchTerms.getJSONObject(i)
Log.d("item", "$item")
Log.d("item", item.getString("id"))
var searchTerm = SearchTermsList(
item.getInt("id"),
item.getInt("id_user"),
item.getInt("id_adwords_customer"),
item.getInt("id_adwords_campaign"),
item.getString("campaign"),
item.getString("search_term"),
item.getString("keyword"),
item.getInt("keyword_id"),
item.getInt("clicks"),
item.getInt("impressions"),
item.getInt("interactions"),
item.getString("interactions_type"),
item.getInt("conversions"),
item.getInt("cost_conv"),
item.getInt("total_conv"),
item.getInt("cost"),
item.getInt("avg_cpv"),
item.getString("currency"),
item.getString("device"),
item.getString("date"),
item.getString("created_at"),
item.getString("updated_at"),
item.getString("deleted_at")
)
(searchArray!! as ArrayList).add(searchTerm)
}
Log.d("search", "$searchArray")
},
Response.ErrorListener { response ->
Log.d("TesSearch", "ERRORE SEARCH ---- $response")
}
)
//aggiunta al queue della stringa precedentemente formata
queueSearchTermsPage.add(stringRequestSearchTerms)
Log.d("TestSearch", "URL ---- $requestUrlSearchTermsPage")
//FINE DELLA PAGINA SEARCH TERMS---------------------------------------------------------------------------FINE FINE FINE
//RICHIESTE PER LA PAGINA SETTINGS-------------------------------------------------------------------------
//FINE DELLA PAGINA SETTINGS-------------------------------------------------------------------------------FINE FINE FINE
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//appbar per la navigazione tra le pagine
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
AppBarConfiguration(
setOf(
R.id.navigation_traffic,
R.id.navigation_ipblocked,
R.id.navigation_search_terms,
R.id.navigation_settings
)
)
navView.setupWithNavController(navController)
}
}