Hi Guys,
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.
You can download the source code CustomListviewFilter.
Cheers Guys!
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.
Main_activity.xml
alertlistrow.xml
MainActivity.java
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 ArrayListarray_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(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);
}
}
CustomAlertAdapter.java
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;
ArrayListlistarray=null;
private LayoutInflater mInflater=null;
public CustomAlertAdapter(Activity activty, ArrayListlist)
{
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;
}
}
You can download the source code CustomListviewFilter.
Cheers Guys!
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :