I have implemented a Custom List View which displays text along with image. The List View fetches data from XML data present over the internet. When the user scrolls down, more data is loaded into the application. Now, I am trying to include a search bar so that when the user searches for some data, the application displays the results returned by the search. The main problem is that the list view doesn't show the correct data when search is performed.
When I check the URL that is being executed by the Search bar in a browser, it shows the correct results, but in Android application it behaves differently.
Below is the code of my Activity that performs this whole work:-
public class OrganizationActivity extends Activity implements OnScrollListener {
int itemsPerPage = 10;
boolean loadingMore = false;
int mPos=0;
// All static variables
static final String URL = "some URL";
static int page_no = 1;
// XML node keys
static final String KEY_ORGANIZATION = "organization"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_CITY = "city";
static final String KEY_STATE = "state";
static final String KEY_IMAGE_URL = "image";
ListView list;
LazyAdapter adapter;
ArrayList<HashMap<String, String>> orgsList = new ArrayList<HashMap<String, String>>();
private EditText filterText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
filterText = (EditText) findViewById(R.id.search_box_et);
filterText.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
if(arg0.length()==0)
{
list.setAdapter(new LazyAdapter(OrganizationActivity.this, orgsList));
}
list.setAdapter(new LazyAdapter(OrganizationActivity.this, orgsList));
String searchtext = null;
try {
searchtext=URLEncoder.encode(arg0.toString().trim(),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String urlStr = "myURL?search="+searchtext;
new LoadData().execute(urlStr);
adapter = new LazyAdapter(OrganizationActivity.this, orgsList);
list.setAdapter(adapter);
}
});
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ORGANIZATION);
// looping through all organization nodes <organization>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_CITY, parser.getValue(e, KEY_CITY));
map.put(KEY_STATE, parser.getValue(e, KEY_STATE));
map.put(KEY_IMAGE_URL, parser.getValue(e, KEY_IMAGE_URL));
// adding HashList to ArrayList
orgsList.add(map);
}
list = (ListView) findViewById(R.id.list);
View footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.listfooter, null, false);
list.addFooterView(footerView);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(this, orgsList);
list.setAdapter(adapter);
list.setOnScrollListener(this);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = orgsList.get(position).get(KEY_NAME).toString();
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG)
.show();
}
});
}
private class LoadData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
for (String url : urls) {
// TODO Auto-generated method stub
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ORGANIZATION);
// looping through all organization nodes <organization>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_CITY, parser.getValue(e, KEY_CITY));
map.put(KEY_STATE, parser.getValue(e, KEY_STATE));
map.put(KEY_IMAGE_URL, parser.getValue(e, KEY_IMAGE_URL));
// adding HashList to ArrayList
orgsList.add(map);
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
adapter = new LazyAdapter(OrganizationActivity.this, orgsList);
list.setAdapter(adapter);
list.setSelectionFromTop(mPos, 0);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//Get the visible item position
mPos=list.getFirstVisiblePosition();
int lastInScreen = firstVisibleItem + visibleItemCount;
//Is the bottom item visible & not loading more already? Load more !
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
page_no++;
String pageURL = "myURL?page="
+ page_no;
new LoadData().execute(pageURL);
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
}
Please help me out in finding the solution to this problem...