Ich habe zwei Klassen. Die erste ist Aktivität, die zweite ist ein Fragment, in dem ich einige EditText
. In activity habe ich eine Unterklasse mit async-task und in method doInBackground
Ich erhalte ein Ergebnis, das ich in einer Variablen speichere. Wie kann ich diese Variable von der Unterklasse "Meine Aktivität" an dieses Fragment senden?
Antworten
Zu viele Anzeigen?
Rahul Rokade
Punkte
17
Kotlin : Daten senden Aktivität zu Fragment-Seite
Schritt 1: Aktivität
class SurveyInfoActivity : AppCompatActivity(){
private lateinit var surveyPageOneFragment: SurveyPageOneFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySurveyInfoBinding.inflate(layoutInflater)
setContentView(binding.root)
init()
}
private fun init() {
context = this
binding.viewPager.isUserInputEnabled = false
surveyPageOneFragment = SurveyPageOneFragment.newInstance(false)
}
/**
* then getting your data any side to put on this method
* ***/
surveyPageOneFragment.setFetchedActivityToFragOne(firstName,middleName,lastName, getMobileNum, getGender, bDay, bMonth, bYear, getAge, getBloodGroup)}
Schritt 2: Fragment eins
fun setFetchedActivityToFragOne(
gFirstName: String?,
gMiddleName : String?,
gLastName: String?,
gMobile: String?,
gGender: String?,
dd: String?,
mm: String?,
yyyy: String?,
gAge: Int?,
gBloodGroup: String?
){
this.gFirstName = gFirstName
this.gMiddleName = gMiddleName
this.gLastName = gLastName
this.gMobile = gMobile
this.gender = gGender
this.bDay = dd
this.bMonth = mm
this.bYear = yyyy
this.niceAge = gAge
this.bloodGroup = gBloodGroup
Log.d("TAG", "Activity get First name: $gFirstName")
Log.d("TAG", "Activity get middle name: $gMiddleName")
Log.d("TAG", "Activity get last name: $gLastName")
Log.d("TAG", "Activity get mobile: $gMobile")
Log.d("TAG", "Activity get Gender: $gGender")
Log.d("TAG", "Activity get birthDay: $dd")
Log.d("TAG", "Activity get birthMonth: $mm")
Log.d("TAG", "Activity get birthYear: $yyyy")
Log.d("TAG", "Activity get age: $gAge")
Log.d("TAG", "Activity get bloodGroup: $gBloodGroup")
getActivityFetchData(gFirstName,gMiddleName, gLastName, gMobile, gGender,dd,mm,yyyy,gAge,gBloodGroup)
}
Schritt 3: Daten und Kontrollwert abrufen und Text oder Kontrollkästchen setzen
private fun getActivityFetchData(
gName: String?,
gMName: String?,
gLName: String?,
gMobile: String?,
gGender: String?,
gdd: String?,
gmm: String?,
gyyyy: String?,
gAge: Int?,
gBloodGroup: String?
){
binding.edtYourName.setText(gName)
binding.edtMiddleName.setText(gMName)
binding.edtLastName.setText(gLName)
binding.edtPhoneCall.setText(gMobile)
when (gGender) {
male -> {
binding.cbMale.isChecked = true
}
female -> {
binding.cbFemale.isChecked = true
}
other -> {
binding.cbTransgender.isChecked = true
}
}
binding.autoDay.setText(gdd)
binding.autoMonth.setText(gmm)
binding.autoYear.setText(gyyyy)
binding.txtAge.text = gAge.toString()
when (gBloodGroup) {
posA -> {
binding.cbPositiveA.isChecked = true
}
posB -> {
binding.cbPositiveB.isChecked = true
}
posAB -> {
binding.cbPositiveAb.isChecked = true
}
posO -> {
binding.cbPositiveO.isChecked = true
}
negA -> {
binding.cbNegativeA.isChecked = true
}
negB -> {
binding.cbNegativeB.isChecked = true
}
negAB -> {
binding.cbNegativeAb.isChecked = true
}
negO -> {
binding.cbNegativeO.isChecked = true
}
}
}
Dv Keerthi
Punkte
15
- See previous answers
- Weitere Antworten anzeigen