Custom ListView AlertDialog With Filter and Search
I hope this tutorial might be helpful to all android developer to search or filter the item of the custom list view.
Here the list view which shows in Alert-dialog. And we can search the item of the list view.
Here I am creating the ListView and Editext programatically and search the item of the list view.
So lets start the coding to search the item of the listview in alert dialog.
Here the list view which shows in Alert-dialog. And we can search the item of the list view.
Here I am creating the ListView and Editext programatically and search the item of the list view.
So lets start the coding to search the item of the listview in alert dialog.
Main_activity.xml
1
2
3
4
5
6
7
8
9
10
11
12
| <relativelayout android:layout_height= "match_parent" android:layout_width= "match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" > <textview android:id= "@+id/textView1" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:text= "@string/hello_world" > <button android:id= "@+id/button_listviewdialog" android:layout_below= "@+id/textView1" android:layout_centerhorizontal= "true" android:layout_height= "wrap_content" android:layout_margintop= "147dp" android:layout_width= "wrap_content" android:text= "ListviewDilaog" > <edittext android:ems= "10" android:id= "@+id/editText_item" android:layout_below= "@+id/textView1" android:layout_centerhorizontal= "true" android:layout_height= "wrap_content" android:layout_margintop= "49dp" android:layout_width= "wrap_content" > <requestfocus> </requestfocus></edittext> </button></textview></relativelayout> |
alertlistrow.xml
1
2
3
4
5
6
7
| <linearlayout android:layout_height= "match_parent" android:layout_width= "match_parent" android:orientation= "vertical" android:paddingleft= "@dimen/activity_vertical_margin" android:paddingright= "@dimen/activity_vertical_margin" xmlns:android= "http://schemas.android.com/apk/res/android" > <textview android:id= "@+id/textView_titllename" android:layout_gravity= "center" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:padding= "5dp" android:text= "Large Text" android:textappearance= "?android:attr/textAppearanceMedium" > <textview android:id= "@+id/textView_alertnumber" android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:visibility= "gone" > </textview></textview></linearlayout> |
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
| package com.sunil.listviewdialog; import java.util.ArrayList; import java.util.Arrays; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; public class MainActivity extends Activity implements OnClickListener, OnItemClickListener{ private Button btn_listviewdialog= null ; private EditText txt_item= null ; private String TitleName[]={ "Sunil Gupta" , "Ram Chnadra" , " Abhishek Tripathi" , "Amit Verma" , "Sandeep Pal" , "Awadhesh Diwakar" , "Shishir Verma" , "Ravi Vimal" , "Prabhakr Singh" , "Manish Srivastva" , "Jitendra Singh" , "Surendra Pal" }; private ArrayList<string> array_sort; int textlength=0; private AlertDialog myalertDialog= null ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt_item=(EditText)findViewById(R.id.editText_item); btn_listviewdialog=(Button)findViewById(R.id.button_listviewdialog); btn_listviewdialog.setOnClickListener( this ); } @Override public void onClick(View arg0) { AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity. this ); final EditText editText = new EditText(MainActivity. this ); final ListView listview= new ListView(MainActivity. this ); editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.discoverseed_larg1, 0, 0, 0); array_sort= new ArrayList<string> (Arrays.asList(TitleName)); LinearLayout layout = new LinearLayout(MainActivity. this ); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(editText); layout.addView(listview); myDialog.setView(layout); CustomAlertAdapter arrayAdapter= new CustomAlertAdapter(MainActivity. this , array_sort); listview.setAdapter(arrayAdapter); listview.setOnItemClickListener( this ); editText.addTextChangedListener( new TextWatcher() { public void afterTextChanged(Editable s){ } public void beforeTextChanged(CharSequence s, int start, int count, int after){ } public void onTextChanged(CharSequence s, int start, int before, int count) { editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); textlength = editText.getText().length(); array_sort.clear(); for (int i = 0; i < TitleName.length; i++) { if (textlength <= TitleName[i].length()) { if (TitleName[i].toLowerCase().contains(editText.getText().toString().toLowerCase().trim())) { array_sort.add(TitleName[i]); } } } listview.setAdapter( new CustomAlertAdapter(MainActivity. this , array_sort)); } }); myDialog.setNegativeButton( "cancel" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); myalertDialog=myDialog.show(); } @Override public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) { myalertDialog.dismiss(); String strName=TitleName[position]; txt_item.setText(strName); } } </string></string> |
CustomAlertAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| package com.sunil.listviewdialog; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class CustomAlertAdapter extends BaseAdapter{ Context ctx= null ; ArrayList<string> listarray= null ; private LayoutInflater mInflater= null ; public CustomAlertAdapter(Activity activty, ArrayList<string> list) { this .ctx=activty; mInflater = activty.getLayoutInflater(); this .listarray=list; } @Override public int getCount() { return listarray.size(); } @Override public Object getItem(int arg0) { return null ; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int position, View convertView, ViewGroup arg2) { final ViewHolder holder; if (convertView == null ) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.alertlistrow, null ); holder.titlename = (TextView) convertView.findViewById(R.id.textView_titllename); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } String datavalue=listarray.get(position); holder.titlename.setText(datavalue); return convertView; } private static class ViewHolder { TextView titlename; } } </string></string> |
Borgata Hotel Casino & Spa Review & Ratings - DRMCD
ReplyDeleteOur 천안 출장샵 Borgata 광양 출장안마 Hotel Casino & Spa review for Borgata Hotel Casino & Spa includes ratings, amenities, complaints and more. Rating: 대구광역 출장안마 4 목포 출장마사지 · 안성 출장안마 Review by DRMCD