Skip to content

Instantly share code, notes, and snippets.

View the-dagger's full-sized avatar
🚀
To Boldly Go Where No Man Has Gone Before

Harshit Dwivedi the-dagger

🚀
To Boldly Go Where No Man Has Gone Before
View GitHub Profile
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());
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!
}
<?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"
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
}
}
@the-dagger
the-dagger / MainActivity.java
Last active December 28, 2017 13:25
Kotlin synthetic binding blog
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),
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)
}
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() }
}
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();
}
});
data class Student(val name: String, val subject: String, val image: String)
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() {