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;
}
}
No comments:
Post a Comment