Part 32 : Model ListView

 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.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

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

ArrayList<Model> modelArrayList = null;

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

mContext = this;

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


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

modelArrayList = new ArrayList<>();

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 = new ModelAdapter(mContext, R.layout.custom_model_list_item, modelArrayList);
listView2.setAdapter(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 ));

adapter.notifyDataSetChanged();

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

new Handler().post(new Runnable() {
@Override
public void run() {
adapter.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();
}
});



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);
adapter.notifyDataSetChanged();
btnListSize.setText("List Size : "+modelArrayList.size());


}
})
.setNeutralButton("Cancel", null)
.create().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"/>



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


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


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




}


@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);


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








No comments:

Post a Comment