Dynamic add and remove item on listview in android or android studio

Progammatically add new item and delete existing item on listview, basically apply in when you add item on cart, when making to do list, or any thing important date, person, books, location add or delete of your own list.

First you have to create layout file(activity_main.xml):

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   android:padding="10dp"  
   tools:context="com.dynamiclistview.MainActivity" >  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:orientation="horizontal" >  
     <EditText  
       android:id="@+id/editTextView"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:layout_weight="1"  
       android:hint="@string/EnterHint" />  
     <ImageView  
       android:id="@+id/imgViewAdd"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:onClick="addValue"  
       android:background="@drawable/add"  
        />  
   </LinearLayout>  
   <ListView  
     android:id="@+id/listview"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:layout_weight="1"  
     android:dividerHeight="2dp" >  
   </ListView>  
 </LinearLayout>  

Custom listItem layout file(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="horizontal" >  
   <TextView  
     android:id="@+id/tvName"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_weight="1"  
     android:gravity="left"  
     android:padding="8dp"  
     android:textColor="#000000"  
     android:textSize="18sp" />  
   <ImageView  
     android:id="@+id/imgRemove"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:focusable="false"  
     android:background="@drawable/remove" />  
 </LinearLayout>  


Here add and remove ,image you can put in drawable folder, as your required color and size.

Now, In MainActivity, we have write code about to click event of add imageview, whenever you want to add item,MainActivity.Java

 public class MainActivity extends Activity {  
  ListView listView;  
  EditText editTextView;  
  ArrayList<Model> ItemModelList;  
  CustomAdapter customAdapter;  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_main);  
  listView = (ListView) findViewById(R.id.listview);  
  editTextView = (EditText) findViewById(R.id.editTextView);  
  ItemModelList = new ArrayList<Model>();  
  customAdapter = new CustomAdapter(getApplicationContext(), ItemModelList);  
         listView.setEmptyView(findViewById(R.id.empty));  
  listView.setAdapter(customAdapter);  
  }  
  @SuppressLint("NewApi")  
  public void addValue(View v) {  
  String name = editTextView.getText().toString();  
  if (name.isEmpty()) {  
   Toast.makeText(getApplicationContext(), "Plz enter Values",  
    Toast.LENGTH_SHORT).show();  
  } else {  
   Model md = new Model(name);  
   ItemModelList.add(md);  
   customAdapter.notifyDataSetChanged();  
   editTextView.setText("");  
  }  
  }  
 }  


Now, More details of CustomAdater, where wen define the how to remove and listitem populated style,CustomAdapter .Java

 public class CustomAdapter extends BaseAdapter {  
  Context context;  
  ArrayList<Model> itemModelList;  
  public CustomAdapter(Context context, ArrayList<Model> modelList) {  
  this.context = context;  
  this.itemModelList = modelList;  
  }  
  @Override  
  public int getCount() {  
  return itemModelList.size();  
  }  
  @Override  
  public Object getItem(int position) {  
  return itemModelList.get(position);  
  }  
  @Override  
  public long getItemId(int position) {  
  return position;  
  }  
  @Override  
  public View getView(final int position, View convertView, ViewGroup parent) {  
  convertView = null;  
  if (convertView == null) {  
   LayoutInflater mInflater = (LayoutInflater) context  
    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);  
   convertView = mInflater.inflate(R.layout.items, null);  
   TextView tvName = (TextView) convertView.findViewById(R.id.tvName);  
   ImageView imgRemove = (ImageView) convertView.findViewById(R.id.imgRemove);  
   Model m = itemModelList.get(position);  
   tvName.setText(m.getName());  
   // click listiner for remove button  
   imgRemove.setOnClickListener(new OnClickListener() {  
   @Override  
   public void onClick(View v) {  
    itemModelList.remove(position);  
    notifyDataSetChanged();  
   }  
   });  
  }  
  return convertView;  
  }  
 }  

Then finally, don;t forget to create model class, Model.java, where you can set getter and setter, but simply we don;t need any more getter and setter, we just create constructor method and assign .
 public class Model {  
  String name;  
  public Model(String name) {  
  this.name = name;  
  }  
 }  


and Last we can see out out like this,






Happy Coding!!!



Dynamic add and remove item on listview in android or android studio Dynamic add and remove item on listview in android or android studio Reviewed by D Kshetri on 5:24 PM Rating: 5

1 comment:

  1. sir, how to implement add to cart using recyclerview cardview by using php mysql in android

    ReplyDelete

Powered by Blogger.