part - 35 - Search ListView - Spannable Concate

 MainActivity.java : 

package first.learn.listviewsearch;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

EditText editText;
ListView listView2;

ArrayList<String> arrayList ;
SearchAdapter mSearchAdapter ;
Context mContext ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mContext = this ;

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

arrayList = new ArrayList<>();


for (int i = 0; i<101; i++){
arrayList.add("item : "+ i);

}



mSearchAdapter = new SearchAdapter(mContext, R.layout.list_item, R.id.alquran_text, arrayList);

listView2.setAdapter(mSearchAdapter);





editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

System.out.println("EditText text beforeTextChanged: "+ s);
mSearchAdapter.getFilter().filter(s);


}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mSearchAdapter.getFilter().filter(s);
System.out.println("EditText text onTextChanged: "+ s);


}

@Override
public void afterTextChanged(Editable s) {

System.out.println("EditText text afterTextChanged: "+ s);

mSearchAdapter.getFilter().filter(s);
}
});



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

System.out.println("Show Position new: "+ position);
System.out.println("Show String new: "+ arrayList.get(position));

int originalPosition =(int) mSearchAdapter.getItemId(position);

System.out.println("Show Position Original : "+ originalPosition);
System.out.println("Show String Original : "+ arrayList.get(originalPosition));


}
});




}
}

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">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"/>



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



</LinearLayout>


SearchAdapter.java : 

package first.learn.listviewsearch;

/**
* Created by bismillah on 11/1/2017.
*/

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import androidx.annotation.NonNull;

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


public class SearchAdapter extends ArrayAdapter<String> {
private final LayoutInflater mInflater;
private final Context mContext;
private final int mResource;
private List<String> mObjects;
private int mFieldId = 0;
private ArrayList<String> mOriginalValues;
private ArrayFilter mFilter;
private final Object mLock = new Object();
private String mSearchText; // this var for highlight




public SearchAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
mContext = context;
mInflater = LayoutInflater.from(context);
mResource = resource;
mObjects = Arrays.asList(objects);
mFieldId = textViewResourceId;
}

public SearchAdapter(@NonNull Context context, int resource,int textViewResourceId, @NonNull List<String> objects) {
super(context, resource,textViewResourceId, objects);
mContext = context;
mInflater = LayoutInflater.from(context);
mResource = resource;
mObjects = objects;
mFieldId = textViewResourceId;
}

@NonNull
@Override
public Context getContext() {

return mContext;
}

@Override
public int getCount() {
return mObjects.size();
}

@Override
public String getItem(int position) {
return mObjects.get(position);
}

@Override
public int getPosition(String item) {
return mObjects.indexOf(item);
}

String TAG = "searchOnubad";

@Override
public long getItemId(int position) {
int itemID;

if (mOriginalValues == null)
{
itemID = position;
}
else
{
itemID = mOriginalValues.indexOf(mObjects.get(position));
}
return itemID;

}

@NonNull
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}


private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();

int j =0;

if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<>(mObjects);
}
}

if (prefix == null || prefix.length() == 0) {
mSearchText = "";
ArrayList<String> list;
synchronized (mLock) {
list = new ArrayList<>(mOriginalValues);
}
results.values = list;
results.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();

mSearchText = prefixString;

ArrayList<String> values;

synchronized (mLock) {
values = new ArrayList<>(mOriginalValues);
}

final int count = values.size();
final ArrayList<String> newValues = new ArrayList<>();

for (int i = 0; i < count; i++) {
final String value = values.get(i);
final String valueText = value.toLowerCase();

// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString) || valueText.contains(prefixString)) {
newValues.add(value); j++;
} else {
final String[] word = valueText.split(" ");
final int wordCount = word.length;

// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {

if (word[k].startsWith(prefixString)|| word[k].contains(prefixString)) {
newValues.add(value);

break;
}
}
}
}

results.values = newValues;
results.count = newValues.size();
}

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
mObjects = (List<String>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}


public static CharSequence highlight(String searchText, String originalText) {

// String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();

int start = originalText.toLowerCase().indexOf(searchText.toLowerCase());
if (start < 0) {
// not found, nothing to to

return originalText;
} else {
// highlight each appearance in the original text
// while searching in normalized text
Spannable highlighted = new SpannableString(originalText);
while (start >= 0) {
int spanStart = Math.min(start, originalText.length());
int spanEnd = Math.min(start + searchText.length(), originalText.length());


System.out.println("similarityPercent : Start : "+start+" spanStart: "+spanStart+" spanEnd: "+spanEnd+" searchText.length(): "+searchText.length()+" originalText.length(): "+originalText.length());

ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
highlighted.setSpan(highlightSpan, spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = originalText.indexOf(searchText, spanEnd);
}

return highlighted;
}
}



@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
TextView textView;

if (convertView == null) {
view = mInflater.inflate(R.layout.list_item, null);
} else {
view = convertView;
}

try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
textView = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
textView = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}

// HIGHLIGHT...

String fullText = getItem(position);

if (mSearchText != null && !mSearchText.isEmpty()) {
assert fullText != null;
int startPos = fullText.toLowerCase().indexOf(mSearchText.toLowerCase());
int endPos = startPos + mSearchText.length();

if (startPos != -1) {

textView.setText(highlight(mSearchText, fullText));
} else {
textView.setText(fullText);
}
} else {
textView.setText(fullText);
}


return view;
}
}


list_item.xml : 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="false"
>

<!-- android:descendantFocusability="beforeDescendants"-->
<!-- android:focusableInTouchMode="false"-->

<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"

android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"

card_view:cardCornerRadius="5dp"
card_view:contentPadding="5dp"
card_view:contentPaddingTop="5dp"
card_view:cardElevation="10dp">

<TextView
android:id="@+id/alquran_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:padding="5dp"
android:text="This is Expl "
android:lineSpacingExtra="12dp"

/>







</androidx.cardview.widget.CardView>






</LinearLayout>






Part 34 : Spannable String And Global Style

 themes.xml : 

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.SpannableLearning" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>


<style name="new_text_view" parent="@android:style/Widget.TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">20dp</item>
</style>


</resources>


MainActivity.java : 

package first.learn.spannablelearning;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView txtForeground, txtBackground, txtSize, txtStyle, txtAppearance ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtForeground = (TextView) findViewById(R.id.txtForeground);
txtBackground = (TextView)findViewById(R.id.txtBackground);
txtSize = (TextView)findViewById(R.id.txtSize);
txtStyle = (TextView)findViewById(R.id.txtStyle);
txtAppearance = (TextView)findViewById(R.id.txtAppearance);

String one = "This is ForeGround Span";
Spannable spOne = new SpannableString(one);
spOne.setSpan(new ForegroundColorSpan(Color.RED),8,17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );

txtForeground.setText(spOne);


String two = "This is Background Color Span And Insert";

SpannableStringBuilder spTwo = new SpannableStringBuilder(two);

spTwo.setSpan(new BackgroundColorSpan(Color.BLUE), 8, 24, Spanned.SPAN_EXCLUSIVE_INCLUSIVE );
spTwo.setSpan(new ForegroundColorSpan(Color.WHITE), 8, 24, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spTwo.insert(25, "Blue");

txtBackground.setText(spTwo);


String three = "This is Size Span";
Spannable spThree = new SpannableString(three);

spThree.setSpan(new RelativeSizeSpan(1.5f), 8, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spThree.setSpan(new ForegroundColorSpan(Color.RED),8,12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );


txtSize.setText(spThree);



String four = "This is Style Span";
Spannable spFour = new SpannableString(four);
spFour.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 8, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

txtStyle.setText(spFour);


String five = "This is Text Appearance";
Spannable spFive = new SpannableString(five);
ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
TextAppearanceSpan span = new TextAppearanceSpan("serif", Typeface.BOLD, 70, blueColor, null );

spFive.setSpan(span, 8, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txtAppearance.setText(spFive);



}
}


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">

<TextView
style="@style/new_text_view"
android:text="ForeGround Span"
android:id="@+id/txtForeground"
/>


<TextView
style="@style/new_text_view"
android:text="BackGround Span"
android:id="@+id/txtBackground"
/>



<TextView
style="@style/new_text_view"
android:text="Size Span"
android:id="@+id/txtSize"
/>


<TextView
style="@style/new_text_view"
android:text="Style Span"
android:id="@+id/txtStyle"
/>



<TextView
style="@style/new_text_view"
android:text="Text Appearance Span"
android:id="@+id/txtAppearance"
/>

</LinearLayout>








Part 33 - Saving List To Shared Preferences

 MainActivity.java 

package first.learn.androidlearningcourse;

import androidx.appcompat.app.AppCompatActivity;

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

import first.learn.androidlearningcourse.AppBarAndMenuPackage.AppBarOptionsMenu;
import first.learn.androidlearningcourse.ButtonClick.ButtonClickActivity;
import first.learn.androidlearningcourse.ButtonClick.MaterialButtons;
import first.learn.androidlearningcourse.CalCulatorPackage.Calculator;
import first.learn.androidlearningcourse.EditeTextPack.EditTextCalculate;
import first.learn.androidlearningcourse.ExpandableRecyclerView.ExpandableRcycler;
import first.learn.androidlearningcourse.Listview_Simple.CheckInstance;
import first.learn.androidlearningcourse.Listview_Simple.CustomListview;
import first.learn.androidlearningcourse.Listview_Simple.ModelListView;
import first.learn.androidlearningcourse.Listview_Simple.SimpleListview;
import first.learn.androidlearningcourse.PassingAndGettingData.PassingDataActivity;
import first.learn.androidlearningcourse.SharedPreferencesPackage.SharedPreferenceActivity;

public class MainActivity extends AppCompatActivity {


String [] items = {
"Button Click - 4 System", // 0
"Expandable RecyclerView"//1
, "Edit Text Calculate"//2
, "Calculator"//3
, "Passing Data"//4
, "Material Buttons"//5
, "Shared Preferences" //6
, "AppBar Options Menu" //7
, "ListView Simple" //8
, "Custom ListView" // 9
, "Check Instance" // 10
, "Model ListView - All" //11
, "Model ListView - Only Name" //12
, "Model ListView - Only Address" //13

};

ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


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



ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items);

listView.setAdapter(adapter);

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

switch (position){
case 0 :
startActivity(new Intent(MainActivity.this, ButtonClickActivity.class));
break;
case 1 :
startActivity(new Intent(MainActivity.this, ExpandableRcycler.class));
break;
case 2:
startActivity(new Intent(MainActivity.this, EditTextCalculate.class));

break;
case 3:
startActivity(new Intent(MainActivity.this, Calculator.class));
break;

case 4:
Intent intent = new Intent(MainActivity.this, PassingDataActivity.class);
startActivity(intent);
break;
case 5:
startActivity(new Intent(MainActivity.this, MaterialButtons.class));
break;
case 6:
startActivity(new Intent(MainActivity.this, SharedPreferenceActivity.class));
break;
case 7:
startActivity(new Intent(MainActivity.this, AppBarOptionsMenu.class));
break;
case 8:
startActivity(new Intent(MainActivity.this, SimpleListview.class));
break;
case 9:
startActivity(new Intent(MainActivity.this, CustomListview.class));
break;
case 10 :
startActivity(new Intent(MainActivity.this, CheckInstance.class));
break;
case 11 :
startActivity(new Intent(MainActivity.this, ModelListView.class));
break;
case 12 :
startActivity(new Intent(MainActivity.this, ModelListView.class).putExtra("OnlyName", "OnlyName"));
break;

case 13 :
startActivity(new Intent(MainActivity.this, ModelListView.class).putExtra("OnlyAddress", "OnlyAddress"));
break;

}

}
});

}




}

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">


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



</LinearLayout>


ModelListView.java 
package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import first.learn.androidlearningcourse.R;

public class ModelListView extends AppCompatActivity {
ListView listView2;
Context mContext;
EditText etName,
etAge,
etAddress;
Button btnAdd, btnListSize, btnSave,
btnLoad;

ArrayList<Model> modelArrayList = null;


SharedPreferences settings ;
SharedPreferences.Editor editor ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_model_list_view);

mContext = this;

settings = PreferenceManager.getDefaultSharedPreferences(mContext);
editor = settings.edit();

etName= (EditText)findViewById(R.id.etName);
etAge= (EditText)findViewById(R.id.etAge);
etAddress= (EditText)findViewById(R.id.etAddress);

btnAdd = (Button)findViewById(R.id.btnAdd);
btnListSize = (Button)findViewById(R.id.btnListSize);

btnSave= (Button)findViewById(R.id.btnSave);
btnLoad= (Button)findViewById(R.id.btnLoad);


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

modelArrayList = new ArrayList<>();

String listString = settings.getString("PreviousList", null);

if(listString!=null){
Gson gson = new Gson();
Type type = new TypeToken<List<Model>>() {
}.getType();
modelArrayList = gson.fromJson(listString, type);

}else {

modelArrayList.add(new Model("Ahasanul Islam", "Rangpur", "38"));

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

Model m = new Model();
m.setName("Ruhul : " + i);
m.setAge(" : " + i);
m.setAddress("Munshi Ganj : " + i);

modelArrayList.add(m);
}

}

ModelAdapter adapter = null ;
if(getIntent().getStringExtra("OnlyName")!=null){
adapter = new ModelAdapter(mContext, R.layout.custom_model_list_item, modelArrayList, true, false , false);
}else if(getIntent().getStringExtra("OnlyAddress")!=null){
adapter = new ModelAdapter(mContext, R.layout.custom_model_list_item, modelArrayList, false, true , false);
}else {
adapter = new ModelAdapter(mContext, R.layout.custom_model_list_item, modelArrayList, false, false , true);
}

listView2.setAdapter(adapter);


ModelAdapter finalAdapter = adapter;
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String age = etAge.getText().toString();
String address = etAddress.getText().toString();

modelArrayList.add(new Model(name, address, age ));

finalAdapter.notifyDataSetChanged();

Gson gson = new Gson();
String listString = gson.toJson(modelArrayList);
editor.putString("PreviousList", listString);
editor.apply();

System.out.println("ListString Adding : "+ listString);



btnListSize.setText("List Size : "+modelArrayList.size());

new Handler().post(new Runnable() {
@Override
public void run() {
finalAdapter.notifyDataSetChanged();
}
});

listView2.setSelection((modelArrayList.size()-1));


listView2.post(new Runnable() {
@Override
public void run() {
listView2.setSelection((modelArrayList.size()-1));
}
});

}
});


btnListSize.setText("List Size : "+modelArrayList.size());
btnListSize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

new AlertDialog.Builder(mContext)
.setTitle("List Size")
.setMessage("List Size : "+ modelArrayList.size())
.create().show();
}
});


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

Model m = modelArrayList.get(position);



new AlertDialog.Builder(mContext)
.setTitle("Sure Delete : ")
.setMessage("Do You Really Want To Delete? - "+m.getName())
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

modelArrayList.remove(position);
finalAdapter1.notifyDataSetChanged();
btnListSize.setText("List Size : "+modelArrayList.size());


Gson gson = new Gson();
String listString = gson.toJson(modelArrayList);
editor.putString("PreviousList", listString);
editor.apply();

System.out.println("ListString Adding : "+ listString);



}
})
.setNeutralButton("Cancel", null)
.create().show();



}
});


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

int n = listView2.getFirstVisiblePosition();
if(getIntent().getStringExtra("OnlyName")!=null){
editor.putInt("OnlyNamePosition", n);
}else if(getIntent().getStringExtra("OnlyAddress")!=null){
editor.putInt("OnlyAddressPosition", n);
}else {
editor.putInt("ListViewPosition", n);
}
editor.apply();
Toast.makeText(mContext, "Reading Position has Saved", Toast.LENGTH_SHORT).show();

}
});

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


int n = - 1 ;

if(getIntent().getStringExtra("OnlyName")!=null){
n = settings.getInt("OnlyNamePosition", -1);
}else if(getIntent().getStringExtra("OnlyAddress")!=null){
n = settings.getInt("OnlyAddressPosition", -1);
}else {
n = settings.getInt("ListViewPosition", -1);
}


if(n!=-1){
listView2.setSelection(n);
}else {
Toast.makeText(mContext, "You haven't Save Reading Position", Toast.LENGTH_SHORT).show();
}


}
});

}




}


activity_model_list_view.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">

<EditText
android:hint="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etName"
/>

<EditText
android:hint="age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etAge"
/>

<EditText
android:hint="Address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etAddress"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add To List"
android:id="@+id/btnAdd"/>


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List Size"
android:id="@+id/btnListSize"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="LOAD"
android:id="@+id/btnLoad"/>

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SAVE"
android:id="@+id/btnSave"/>
</LinearLayout>


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


ModelAdapter.java
package first.learn.androidlearningcourse.Listview_Simple;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class ModelAdapter extends ArrayAdapter<Model> {


ArrayList<Model> modelArrayList = null ;
Context mContext ;

public ModelAdapter(@NonNull Context context, int resource, @NonNull Model[] objects) {
super(context, resource, objects);
}
public ModelAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Model> objects) {
super(context, resource, objects);

this.mContext = context ;
this.modelArrayList = objects;

}

boolean isOnlyName;
boolean isOnlyAddress;
boolean isAll;


public ModelAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Model> objects, boolean isOnlyName, boolean isOnlyAddress, boolean isAll) {
super(context, resource, objects);

this.mContext = context ;
this.modelArrayList = objects;

this.isOnlyName = isOnlyName;
this.isOnlyAddress = isOnlyAddress;
this.isAll = isAll;
}

@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_model_list_item, null);
}else {

view = convertView ;
}


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

LinearLayout llName,
llAddress, llAge;

llName = (LinearLayout)view.findViewById(R.id.llName);

llAddress = (LinearLayout)view.findViewById(R.id.llAddress);
llAge = (LinearLayout)view.findViewById(R.id.llAge);

if(isOnlyName){
llName.setVisibility(View.VISIBLE);
} else if(isOnlyAddress){
llAddress.setVisibility(View.VISIBLE);
}else if (isAll) {
llName.setVisibility(View.VISIBLE);
llAddress.setVisibility(View.VISIBLE);
llAge.setVisibility(View.VISIBLE);
}else {
llName.setVisibility(View.GONE);
llAddress.setVisibility(View.GONE);
llAge.setVisibility(View.GONE);
}


Model m = modelArrayList.get(position);

/*
if(m!=null){
String name = m.getName();
String age = m.getAge();
String address = m.getAddress();

tvOne.setText("Name : " +name);
tvTwo.setText("Age : "+ age);
tvThree.setText("Address : "+ address);

}

*/


if(m!=null){
String name = m.getName();
String age = m.getAge();
String address = m.getAddress();

tvOne.setText(name);
tvTwo.setText( age);
tvThree.setText(address);

}


return view ;
}
}


custom_model_list_item.xml
<?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"
android:layout_margin="15dp">




<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:orientation="horizontal"
>
<LinearLayout
android:layout_weight="8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">


<LinearLayout
android:visibility="gone"
android:id="@+id/llName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">

<TextView
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:text="Name : "
android:textColor="@color/purple_700"/>

<TextView
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="7"
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"/>

</LinearLayout>


<LinearLayout
android:visibility="gone"
android:id="@+id/llAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">

<TextView
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:text="Age : "
android:textColor="@color/purple_700"/>

<TextView
android:padding="2dp"
android:text="blah blah"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:id="@+id/tvTwo"/>

</LinearLayout>


<LinearLayout
android:visibility="gone"
android:id="@+id/llAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">

<TextView
android:padding="2dp"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dp"
android:text="Address : "
android:textColor="@color/purple_700"/>

<TextView
android:padding="2dp"
android:text="blah blah"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:id="@+id/tvThree"/>

</LinearLayout>





</LinearLayout>


<LinearLayout

android:gravity="center"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">

<ImageView
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_delete"/>

</LinearLayout>



</LinearLayout>





</LinearLayout>


Model.java : 

package first.learn.androidlearningcourse.Listview_Simple;

public class Model {

String Name, Address, Age ;


public Model() {
}

public Model(String name, String address, String age) {
Name = name;
Address = address;
Age = age;
}


public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getAddress() {
return Address;
}

public void setAddress(String address) {
Address = address;
}

public String getAge() {
return Age;
}

public void setAge(String age) {
Age = age;
}
}