Part 28 - For Loop, Switch-Case, Transfer Activity to Another Project

 Simple_ListView.java

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class SimpleListview extends AppCompatActivity {


ListView listView2;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_listview);
setTitle("Simple ListView");

mContext = this;


listView2 = (ListView) findViewById(R.id.listView2);
listView2.setFastScrollEnabled(false);
listView2.setFastScrollAlwaysVisible(false);
listView2.setVerticalScrollBarEnabled(false);



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

for (int i = 0; i < 101; i++) {
if(i==50)
break;
list.add("Item : " + i);

}


if (list.size() > 4) {
System.out.println("Element List : " + list.get(4));
} else {
// Again Network Call :
System.out.println("Element List : " + list.get(3));
}


String[] array = {"apple", "Banana", "Cucumber", "Dragon"};

if (array.length > 4) {
System.out.println("Element : Array : " + array[4]);
} else {
System.out.println("Element : Array : " + "NetWork Call Again");
}


/// String To Array :


ArrayAdapter<String> adapter2 = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1, android.R.id.text1, list);
listView2.setAdapter(adapter2);

listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


// if (position == 0) {
// Toast.makeText(mContext, "Books Subject : " + position, Toast.LENGTH_SHORT).show();
//
// } else if (position == 1) {
// Toast.makeText(mContext, "Books Preface : " + position, Toast.LENGTH_SHORT).show();
//
// } else if (position==2) {
// Toast.makeText(mContext, "Books Chapter : " + position, Toast.LENGTH_SHORT).show();
//
// } else if (position==3) {
// Toast.makeText(mContext, "Introduction Two Modern World", Toast.LENGTH_SHORT).show();
//
// }else {
// Toast.makeText(mContext, "Introduction Two Modern World extra", Toast.LENGTH_SHORT).show();
//
// }


switch (position) {
case 0:
Toast.makeText(mContext, "Books Subject : " + position, Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(mContext, "Books Preface : " + position, Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(mContext, "Books Chapter : " + position, Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(mContext, "Introduction Two Modern World", Toast.LENGTH_SHORT).show();
break;
case 100:
Toast.makeText(mContext, "Introduction Two Modern World extra 100", Toast.LENGTH_SHORT).show();
break;

case 101:
Toast.makeText(mContext, "Introduction Two Modern World extra", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(mContext, "Introduction Two Modern World Default", Toast.LENGTH_SHORT).show();
break;

}


// String a = list.get(position);
//
// new AlertDialog.Builder(mContext)
// .setTitle("You Clicked")
// .setMessage("Position : " + position + " Item : " + a)
// .setPositiveButton("ok", null)
// .create().show();

}
});


}
}


Xml : 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Listview_Simple.SimpleListview"
android:orientation="vertical"
android:layout_margin="16dp">



<ListView
android:fastScrollAlwaysVisible="false"
android:scrollbars="none"
android:layout_marginTop="20dp"
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



</LinearLayout>




No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts