This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
URL url = new URL("https://jsonplaceholder.typicode.com/posts"); | |
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); | |
httpURLConnection.setRequestMethod("GET"); | |
httpURLConnection.setReadTimeout(3000); | |
httpURLConnection.setConnectTimeout(5000); | |
httpURLConnection.connect(); | |
if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { | |
throw new IOException("HTTP error code" + httpURLConnection.getResponseCode()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.android.synthetic.main.activity_main.* | |
class KotlinActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
studentName.text = "Harshit" //That's it! | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/studentName" | |
android:padding="8dp" | |
android:textSize="24sp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CourseViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) { | |
fun bindView (course: Course) { | |
itemView.studentName.text = s.name | |
itemView.studentSubject.text = s.subject | |
itemView.studentCenter.text = s.center | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity extends AppCompatActivity{ | |
TextView studentName, studentSubject, studentCenter; | |
//Since I might need them later on outside of my onCreate method | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
studentName = findViewById(R.id.studentName), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun startIntent(s : Student){ | |
val intent = Intent(this,HomeActivity::class.java) | |
with(intent){ | |
putExtra(NAME,name) // No need to use intent object anymore | |
putExtra(SUBJECT,subject) | |
putExtra(IMAGE,image) | |
} | |
startActivity(intent) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun bind(student: Student) { | |
with(student) { | |
studentName.text = name //the property in Student called directly instead of calling student.name or student.getName() | |
studentSubject.text = subject | |
Picasso.with(itemView.context).load(image).into(studentImage) | |
} | |
itemView.setOnClickListener { Toast.makeText(itemView.context, "Student's name : " + student.name, Toast.LENGTH_SHORT).show() } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void bind(final Student student) { | |
studentName.setText(student.getName()); | |
studentSubject.setText(student.getBatch()); | |
Picasso.with(itemView.getContext()).load(student.getImage()).into(studentImage); | |
itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Toast.makeText(v.getContext(), "Student's name : " + student.getName(), Toast.LENGTH_SHORT).show(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Student(val name: String, val subject: String, val image: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
//Notice that I didn't need to call findViewById(), thanks to Synthetic Binding! | |
btndownload.setOnClickListener { downloadData() } | |
} | |
fun downloadData() { |