Custom List View with Add Data | Delete | Search .
Create project using empty Activity.
Step 1: XML setup
-Put code in your main activity .
activti_main.xml
-Create layout xml .
raw.xml
-Create layout for custom dialog.this dialog we use for add data.
custom_dialog.xml
Step 1: JAVA setup
-put below code in your MainActivity.Java
MainActivity.java
-Create java class name is Model for store multiple data.
Model.Java
-Create java class for inflate custom view
CustomListAdapter .Java
Create project using empty Activity.
Step 1: XML setup
-Put code in your main activity .
activti_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.aaru.grideviewupdatedeletesearch.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/serchFieldRelative"
android:layout_marginLeft="15dp"
android:background="@color/colorAccent"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:visibility="visible"
android:id="@+id/linerFuncationality"
android:gravity="center_horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:text="Add"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSearch"
android:text="Search"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:visibility="gone"
android:background="@drawable/round_corner"
android:id="@+id/linerSecrhEditext"
android:gravity="center_horizontal"
>
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/edtSearch"
android:paddingLeft="15dp"
android:hint="Search"
android:background="@drawable/round_corner"
android:layout_centerHorizontal="true"></EditText>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:id="@+id/btnCancelEditext"
android:padding="10dp"
android:background="#ec1717"
android:src="@drawable/ic_action_cancel"
android:layout_marginTop="15dp"
/>
</LinearLayout>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/serchFieldRelative"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:numColumns="3"
android:id="@+id/listView"
></GridView>
</RelativeLayout>
-Create layout xml .
raw.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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:text="Name"
android:textSize="20dp"
android:padding="10dp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvField"
android:text="Field"
android:padding="10dp"
/>
</LinearLayout>
-Create layout for custom dialog.this dialog we use for add data.
custom_dialog.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:background="@color/colorPrimary"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Data"
android:textSize="20dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/edtName"
android:hint="Name"
android:textColor="#fff"
android:paddingLeft="10dp"
android:layout_gravity="center_horizontal"
/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/edtField"
android:paddingLeft="10dp"
android:hint="Field"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center_horizontal"
android:id="@+id/btnSubmitData"
android:background="@color/colorAccent"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL"
android:layout_marginLeft="15dp"
android:layout_gravity="center_horizontal"
android:id="@+id/btnDismiss"
android:layout_weight="1"
android:background="@color/colorAccent"
/>
</LinearLayout>
</LinearLayout>
Step 1: JAVA setup
-put below code in your MainActivity.Java
MainActivity.java
package com.example.aaru.grideviewupdatedeletesearch;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnAdd, btnSearch;
EditText edtSearch;
LinearLayout linerFuncationality, linerSecrhEditext;
ImageView btnCancelEditext;
ArrayList<Model> allAdd;
ArrayList<Model> searchData;
ListView listView;
Model m;
CustomListAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getID();
setListener();
allAdd = new ArrayList<>();
allAdd.add(new Model("Raj", "Computer Engineering"));
allAdd.add(new Model("Mohit", "Computer Engineering"));
allAdd.add(new Model("Karishma", "IT Engineering"));
allAdd.add(new Model("Bhoomi", "Mechanical Engineering"));
allAdd.add(new Model("Anil", "Electrical Engineering"));
allAdd.add(new Model("Mahi", "Civil Engineering"));
allAdd.add(new Model("Krutagna", "Computer Engineering"));
allAdd.add(new Model("Urvika", "Computer Engineering"));
allAdd.add(new Model("Jabir", "Computer Engineering"));
allAdd.add(new Model("Dipti", "Computer Engineering"));
allAdd.add(new Model("Shrey", "Civil Engineering"));
allAdd.add(new Model("Jyoti", "Electrical Engineering"));
allAdd.add(new Model("Hansraj", "Production Engineering"));
allAdd.add(new Model("Shrikant", "Computer Engineering"));
searchData = new ArrayList<>(allAdd);
customAdapter= new CustomListAdapter (searchData, MainActivity.this);
listView.setAdapter(grideAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
searchData.remove(i);
customAdapter.notifyDataSetChanged();
customAdapter.notifyDataSetInvalidated();
Toast.makeText(MainActivity.this, "Delete", Toast.LENGTH_SHORT).show();
return true;
}
});
}
private void setListener() {
btnAdd.setOnClickListener(this);
btnSearch.setOnClickListener(this);
btnCancelEditext.setOnClickListener(this);
}
private void getID() {
btnAdd = findViewById(R.id.btnAdd);
btnSearch = findViewById(R.id.btnSearch);
edtSearch = findViewById(R.id.edtSearch);
btnCancelEditext = findViewById(R.id.btnCancelEditext);
linerFuncationality = findViewById(R.id.linerFuncationality);
linerSecrhEditext = findViewById(R.id.linerSecrhEditext);
listView= findViewById(R.id.listView);
}
@Override
public void onClick(View view) {
if (view.equals(btnSearch)) {
linerFuncationality.setVisibility(View.GONE);
linerSecrhEditext.setVisibility(View.VISIBLE);
search();
} else if (view.equals(btnCancelEditext)) {
linerFuncationality.setVisibility(View.VISIBLE);
linerSecrhEditext.setVisibility(View.GONE);
} else if (view.equals(btnAdd)) {
addCustomData();
}
}
private void addCustomData() {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_dialog);
final EditText edtName = dialog.findViewById(R.id.edtName);
final EditText edtField = dialog.findViewById(R.id.edtField);
Button btnSubmit = dialog.findViewById(R.id.btnSubmitData);
Button btnDismiss = dialog.findViewById(R.id.btnDismiss);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m = new Model(edtName.getText().toString(), edtField.getText().toString());
searchData.add(m);
customAdapter.notifyDataSetChanged();
customAdapter.notifyDataSetInvalidated();
edtName.setText("");
edtField.setText("");
dialog.dismiss();
}
});
btnDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
private void search() {
final String searchString = edtSearch.getText().toString();
final int serchLength = searchString.length();
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int position, int i1, int i2) {
searchData.clear();
for (Model m : allAdd) {
if (serchLength <= m.getName().length()) {
if (m.getName().toLowerCase().contains(charSequence.toString().toLowerCase())) {
searchData.add(m);
}
}
}
customAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
-Create java class name is Model for store multiple data.
Model.Java
package com.example.aaru.grideviewupdatedeletesearch;
/**
* Created by Aaru on 12/23/2017.
*/
public class Model {
public Model(String name, String field) {
this.name = name;
this.field = field;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
String name;
String field;
}
-Create java class for inflate custom view
CustomListAdapter .Java
package com.example.aaru.grideviewupdatedeletesearch;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Aaru on 12/23/2017.
*/
public class CustomListAdapter extends BaseAdapter {
private ViewHolde holde;
public CustomListAdapter (ArrayList<Model> alldata, Context context) {
this.alldata = alldata;
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
LayoutInflater inflater;
ArrayList<Model> alldata;
Context context;
@Override
public int getCount() {
return alldata.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolde holde = new ViewHolde();
if (view == null) {
view = inflater.inflate(R.layout.gride_raw, null);
holde.textName = view.findViewById(R.id.tvName);
holde.tvField = view.findViewById(R.id.tvField);
view.setTag(holde);
} else {
holde = (ViewHolde) view.getTag();
}
holde.textName.setText(alldata.get(i).getName());
holde.tvField.setText(alldata.get(i).getField());
return view;
}
private static class ViewHolde {
TextView textName, tvField;
}
}
>>>>>>>>>>>>>>>>>>>....Run And Enjoy......<<<<<<<<<<<<<<<<<<<