part 29 : Custom ArrayList Adapter, mContext Instance Of

 CustomListView.java : 

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class CustomListview extends AppCompatActivity {
ListView listView2;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_listview);

mContext = this;


listView2 = (ListView) findViewById(R.id.listView2);

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

for(int i=0; i<100; i++){

list.add("Position : "+i + "# Size Number : "+(i+1));
}



CustomAdapter customAdapter = new CustomAdapter(mContext, R.layout.custom_list_item, list );

listView2.setAdapter(customAdapter);




}
}


CheckInstance.java

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class CheckInstance extends AppCompatActivity {

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

mContext = this;


listView2 = (ListView) findViewById(R.id.listView2);

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

for(int i=0; i<100; i++){

list.add("Instance Position : "+i + "# Instance Size Number : "+(i+1));
}



CustomAdapter customAdapter = new CustomAdapter(mContext, R.layout.custom_list_item, list );

listView2.setAdapter(customAdapter);




}
}




activity_custom_listview. 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>


custom_list_item : 

<?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="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">

<TextView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvOne"
android:textStyle="bold"
android:textSize="16dp"
android:text=";skdj;lkfa slk;dfja"
android:textColor="@color/purple_700"/>


<TextView
android:padding="10dp"
android:text="blah blah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvTwo"/>



</LinearLayout>

</LinearLayout>


CustomAdapter.java : 


package first.learn.androidlearningcourse.Listview_Simple;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import first.learn.androidlearningcourse.R;

public class CustomAdapter extends ArrayAdapter<String> {


ArrayList<String> list ;
Context mContext;

public CustomAdapter(@NonNull Context context, int resource, @NonNull ArrayList<String> objects) {
super(context, resource, objects);

this.list = objects;
this.mContext = context ;
}



@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View view ;

if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list_item, null);
}else {

view = convertView;
}

TextView tvOne = (TextView)view.findViewById(R.id.tvOne);
TextView tvTwo = (TextView)view.findViewById(R.id.tvTwo);

if(list.size()>position){

String s = list.get(position);
String [] ar = s.split("#");

if(ar.length>1) {
tvOne.setText(ar[0]);
tvTwo.setText(ar[1]);
}

tvOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "You Clicked On : "+ ar[0], Toast.LENGTH_SHORT).show();
}
});

tvTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mContext instanceof CustomListview) {

new AlertDialog.Builder(mContext)
.setTitle("You Clicked: ")
.setMessage(ar[1])
.setPositiveButton("Ok", null)
.create().show();
}

if(mContext instanceof CheckInstance) {

new AlertDialog.Builder(mContext)
.setTitle("You Clicked Instance Of: ")
.setMessage(ar[1])
.setPositiveButton("Cancel", null)
.create().show();
}

}
});


}



return view ;
}



}












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>




part 27 : String Array, ArrayList-String, List view, Array Adapter, String To Array ( String split), Extract Specific Character From String ( String replace ) .

MainActivity.java 

package first.learn.listviewproject;

import static first.learn.listviewproject.Common.showStaticCustomDialog;
import static first.learn.listviewproject.MainActivity2.Message;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


Button btnOne, btnTwo;
TextView tvOne;
ListView listView, listView2;
/*
Activity Starting :
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setTitle("Screen One ");



listView = (ListView)findViewById(R.id.listView);
listView2 = (ListView)findViewById(R.id.listView2);




ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

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 :
String s = "a#b#whatever#?value#ddddd#lksajdflakjsdf#kasd;lfa#";
String [] arb = s.split("#");


ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, arb );
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String a = arb[position];

new AlertDialog.Builder(MainActivity.this)
.setTitle("You Clicked")
.setMessage("Position : "+ position + " Item : "+ a)
.setPositiveButton("ok", null)
.create().show();

}
});



ArrayAdapter<String> adapter2 = new ArrayAdapter<>(MainActivity.this, 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) {

String a = list.get(position);

new AlertDialog.Builder(MainActivity.this)
.setTitle("You Clicked")
.setMessage("Position : "+ position + " Item : "+ a)
.setPositiveButton("ok", null)
.create().show();

}
});


//////////////////////////////


String [] ar = s.split("\\?");
System.out.println("Array Length: "+ ar.length);
System.out.println("Array : "+ ar[1]);




// Extract Specific Character From String :
String t = "noor@gmail.com";
String [] tx = t.split("@");
String found = tx[0];

System.out.println("Name : "+ found);

String hostProvider = tx[1].replace(".com", "");
System.out.println("Name Host : "+ hostProvider);


}


}


activity_main.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=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">




<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>




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



</LinearLayout>
















part 26 - Activity Lifecycle

MainActivity.java

package first.learn.listviewproject;

import static first.learn.listviewproject.MainActivity2.Message;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


Button btnOne, btnTwo;
TextView tvOne;
/*
Activity Starting :
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setTitle("Screen One ");

btnOne = (Button)findViewById(R.id.btnOne);
tvOne = (TextView)findViewById(R.id.tvOne);
btnTwo = (Button)findViewById(R.id.btnTwo);

btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
});


btnTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert ")
.setMessage("yes ")
.setPositiveButton("Ok", null)
.create().show();
}
});

System.out.println("Calling Method : Oncreate - Activity 1");

}
   @Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);

System.out.println("Calling Method : onWindowFocusChanged - Activity 1");
}

@Override
protected void onStart() {
super.onStart();

System.out.println("Calling Method : OnStart - Activity 1");

}



@Override
protected void onResume() {
super.onResume();
System.out.println("Calling Method : OnResume - Activity 1 ");
tvOne.setText(Message);
}


/*
Activity Closing :
*/

@Override
protected void onPause() {
super.onPause();
System.out.println("Calling Method : onPause - Activity 1");
}


@Override
protected void onStop() {
super.onStop();
System.out.println("Calling Method : onStop - Activity 1");
}

@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("Calling Method : onDestroy - Activity 1");
}
}

 

activity_main.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=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnOne"
android:text="Next Activity 2"/>


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnTwo"
android:text="Show Alert"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/tvOne"
/>

</LinearLayout>


ActivityMain2.Java

package first.learn.listviewproject;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;

public class MainActivity2 extends AppCompatActivity {


public static String Message = "";

EditText etOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setTitle("Screen 2");

etOne = (EditText)findViewById(R.id.etOne);
System.out.println("Calling Method : OnCreate - Activity 2");


new Handler().postDelayed(new Runnable() {
@Override
public void run() {

System.out.println("Calling Method : Post Delayed - Activity 2");

}
},500);

}

@Override
protected void onStart() {
super.onStart();
System.out.println("Calling Method : OnStart - Activity 2");

}

@Override
protected void onResume() {
super.onResume();
System.out.println("Calling Method : OnResume - Activity 2");
}


/*
Activity Closing :
*/

@Override
protected void onPause() {
super.onPause();
System.out.println("Calling Method : onPause - Activity 2");
}


@Override
protected void onStop() {
super.onStop();
System.out.println("Calling Method : onStop - Activity 2");
}

@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("Calling Method : onDestroy - Activity 2");
}

@Override
public void onBackPressed() {


String s = etOne.getText().toString();

Message = s ;



super.onBackPressed();
}
}

Activity_main2.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=".MainActivity2"
android:orientation="vertical"
android:layout_margin="16dp">


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etOne"
android:hint="Write"
android:inputType="number"/>


</LinearLayout>







Featured Post

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

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

Popular Posts