Reputation: 910
I am taking too much time on customized search.
Can anyone tell me , How I will implement it in my code.Plz help
I already get a data in list, I have edit box , but I do'nt know, How it will implement in Using Filterable class or how I will use
addTextChangedListener(){}
in this code.
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
My code is MainSearch.java ,Where I will get list .
In this call , where I will implement the search code .. I have little bit idea , If I will save all list items in arrayList and will compare all those value with editText value. It may be provide the solutions. Not sure about it.
public class MainSEarch extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
ListView list;
LazyAdapter adapter;
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
ArrayList<HashMap<String, String>> contactList;
// Search EditText
EditText inputSearch;
int textlength = 0;
// url to make request
String url = "URl NOT HERE But like as http://api.androidhive.info/contacts/";
// JSON Node names
static final String TAG_CONTACTS = "userinfo";
static final String TAG_ID = "id";
static final String TAG_NAME = "name";
static final String TAG_EMAIL = "email";
static final String TAG_PHONE = "cb_phonenumber";
// contacts JSONArray
JSONArray userinfo = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_search);
inputSearch = (EditText) findViewById(R.id.editText1); // Search Box
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainSEarch.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
contactList = new ArrayList<HashMap<String, String>>();
// Loading Agents list JSON in Background Thread
new LoadAgentList().execute();
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//what should I do in this method.plz provide some solutions.
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAgentList extends AsyncTask<String, String, String>
{
/** *
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainSEarch.this);
pDialog.setMessage("Agents List ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try{
// Getting Array of Contacts
userinfo = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < userinfo.length(); i++){
JSONObject c = userinfo.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String mobile = c.getString(TAG_PHONE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_EMAIL, email);
map.put(TAG_PHONE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
list=(ListView)findViewById(R.id.mylist);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(MainSEarch.this,contactList);
list.setAdapter(adapter);
}
});
}
}
My Second Class is LazyAdapter: Which will show how getView() method inflate the view and how lazy list will work code is what shold I do in this class
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
TextView name = (TextView)vi.findViewById(R.id.name); // name
TextView email = (TextView)vi.findViewById(R.id.email); // email
TextView phone = (TextView)vi.findViewById(R.id.mobile); // phone
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
name.setText(song.get(MainSEarch.TAG_NAME));
email.setText(song.get(MainSEarch.TAG_EMAIL));
phone.setText(song.get(MainSEarch.TAG_PHONE));
return vi;
}
}
What change should be there in my code , that my search will be execute . Plz I am beginner in Android. Thanks in Advance
Upvotes: 1
Views: 1091
Reputation: 11141
I have used the following code to filter data from my customized list view. have a look at it, an dcheck if you can implement it in your case,
adapter = new CustomListViewAdapter(this,R.layout.list_row, rowItems);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
and for filtering search based on the edit text, I have used,
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
public void afterTextChanged(Editable arg0) {
ActivityName.this.adapter.getFilter().filter(arg0);
}
});
Upvotes: 0
Reputation: 7742
You'll have to implement your own filter in your custom adapter, here it goes a sample(is very straightforward):
public Filter getFilter() {
Log.d("Filter", "BeginFilter");
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence arg0, FilterResults arg1) {
Log.d("Filter", "publishResults");
mGuides = (ArrayList<Guide>)arg1.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("Filter","Perform filtering");
constraint = constraint.toString().toLowerCase();
ArrayList<Guide> filteredGuides = new ArrayList<Guide>();
for (int i = 0; i < mGuides.size(); i++)
{
Guide newGuide = mGuides.get(i);
if (newGuide.getName().toLowerCase().contains(constraint))
filteredGuides.add(newGuide);
}
FilterResults filterResults = new FilterResults();
filterResults.count = filteredGuides.size();
filterResults.values = filteredGuides;
return filterResults;
}
};
}
You call it like this from the activity:
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
adapter.getFilter().filter(arg0);
}
Upvotes: 0
Reputation: 6899
Look at this example. Hope you will get an idea. Use Textwatcher. http://abdennour-insat.blogspot.in/2012/05/listview-textwatcher-autocomplete.html
Upvotes: 1