Dec 28, 2021

Inverted half pyramid

 * * * * *

* * * *

* * * 

* *

*


public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = rows; i >= 1; --i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}
1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = rows; i >= 1; --i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
public class Main {

  public static void main(String[] args) {
    int rows = 5, k = 0;

    for (int i = 1; i <= rows; ++i, k = 0) {
      for (int space = 1; space <= rows - i; ++space) {
        System.out.print("  ");
      }

      while (k != 2 * i - 1) {
        System.out.print("* ");
        ++k;
      }

      System.out.println();
    }
  }
}


* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for(int i = rows; i >= 1; --i) {
      for(int space = 1; space <= rows - i; ++space) {
        System.out.print("  ");
      }

      for(int j=i; j <= 2 * i - 1; ++j) {
        System.out.print("* ");
      }

      for(int j = 0; j < i - 1; ++j) {
        System.out.print("* ");
      }

      System.out.println();
    }
  }
}

Pyramid Number

 1

1 2
1 2 3
1 2 3 4
1 2 3 4 5
public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = 1; i <= rows; ++i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}

Pyramid and Pattern

*
* *
* * *
* * * *
* * * * *


 public class Main {


  public static void main(String[] args) {
    int rows = 5;

    for (int i = 1; i <= rows; ++i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}

Kotlin RecyclerView

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    implementation("androidx.recyclerview:recyclerview:1.2.1")
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'


APIService

interface APIService {

@GET("?results=5")
fun fetchUser(): Call<UserResponseNew>

}


RestClient


object RestClient {

private val BASE_URL = "https://randomuser.me/api/"
private var mRetrofit: Retrofit? = null

val client: Retrofit
get() {
if (mRetrofit == null) {
mRetrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return this.mRetrofit!!
}
}


ExampleAdapter


class ExampleAdapter(private val userList: List<ResultsItem>) :
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.item_row, parent,
false
)
return ExampleViewHolder(itemView)
}

override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = userList[position]
holder.textView.text = currentItem.email
Log.d("TAG", "DATA:: " + currentItem.location?.street?.name)
//holder.textView.text = currentItem.results?.get(0)?.email
Glide.with(holder.itemView.context).load(currentItem.picture?.thumbnail).into(holder.imageView);
holder.tvCity.text = currentItem.location?.street?.name
}

override fun getItemCount() = userList.size

class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val textView: TextView = itemView.findViewById(R.id.tv_item_name)
val imageView: ImageView = itemView.findViewById(R.id.pic)
val tvCity: TextView = itemView.findViewById(R.id.tv_item_city)
}
}
MainActivity
class MainActivity : AppCompatActivity() {

private val TAG = MainActivity::class.java.simpleName

private lateinit var recyclerView: RecyclerView
private var mApiService: APIService? = null


//private var userResponse = UserResponseNew()

private var mUserList: MutableList<ResultsItem> = ArrayList()


//private var categoryList: List<ResultsItem>? = null

//var list = ArrayList<ResultsItem>()
var list: List<ResultsItem> = listOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView = findViewById<RecyclerView>(R.id.recycler_view)

mApiService = RestClient.client.create(APIService::class.java)

userListAPI()
}

private fun userListAPI() {
val call = mApiService!!.fetchUser()

call.enqueue(object : retrofit2.Callback<UserResponseNew?> {
override fun onResponse(
call: Call<UserResponseNew?>, response: Response<UserResponseNew?>) {

val userResponse = response.body()!!
Log.d(TAG, "data_size:: " + userResponse.results?.size)

mUserList.addAll(userResponse.results!!)

//RecyclerView
recyclerView.layoutManager = LinearLayoutManager(
this@MainActivity,
RecyclerView.VERTICAL, false
)
val exampleAdapter = ExampleAdapter(mUserList)
recyclerView.adapter = exampleAdapter






val responseBody = response.body()!!

//Toast
Toast.makeText(this@MainActivity, "Success", Toast.LENGTH_SHORT).show()

//Single record print
Log.d(TAG, "email_1:: " + responseBody.results?.get(0)?.email)
//Log.d(TAG, "title_1:: "+responseBody.results?.get(0)?.name?.title)

//All record print using for loop
for (myData in responseBody.results!!) {
//if (myData != null) {
Log.d(TAG, "email:: " + myData.email)
//}
}
}

override fun onFailure(call: Call<UserResponseNew?>, t: Throwable) {
//Toast
Toast.makeText(this@MainActivity, "Fail", Toast.LENGTH_SHORT).show()
}
})
}

}

Jan 19, 2017

Custom List View

Open your activity_main and paste below code. Open your single_row and paste below code. Open your CustomListAdapter and paste below code. Open your MainActivity and paste below code.
Output :


Array vs ArrayList

*****Array*****

String item[]={"Cupcake","Donut",
            "Eclairs","Froyo","GingerBread","HoneyComb",
            "IceCreamSandWitch","Jellybean","Kitkat",
            "Lollipop", "Marshmallow", "Nougat"};



*****Aray List*****

ArrayList<String> list = new ArrayList<String>();

list.add(textview.getText().toString());
list.add("B");
list.add("C");



*****Array Adapter****

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);



*****Set Adapter*****
listview.setAdapter(adapter);

Dec 16, 2016

Simple List View

Open your activity_main and paste below code. Open your MainActivity and paste below code.
Output :