我已经实现了一个自定义列表视图,它可以显示文本和图像。列表视图从通过Internet呈现的XML数据中提取数据。当用户向下滚动时,更多数据被加载到应用程序中。现在,我正在尝试包含一个搜索栏,以便当用户搜索某些数据时,应用程序会显示搜索返回的结果。主要问题是列表视图在执行搜索时没有显示正确的数据。
当我在浏览器中检查搜索栏正在执行的URL时,它会显示正确的结果,但在Android应用程序中,它的行为方式不同。
以下是执行整个工作的我的活动代码: -
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
}
}
请帮助我找到解决此问题的方法......
答案 0 :(得分:0)
看起来我在用新数据刷新ListView时犯了一点点错误,而且我在其他地方调用了notifyDataSetListener。现在它工作得很好..