2012-08-09 57 views
1

我已經實現了一個自定義列表視圖,它顯示文字和圖像。列表視圖從存在於互聯網上的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

你是什麼意思的「不顯示正確的數據」呢? – 2012-08-09 06:32:53

+2

它顯示在列表視圖中加載以前的數據,並通過搜索查詢返回的數據... – 2012-08-09 06:35:06

+2

@ShekharChikara閱讀此篇搜索'定製List' http://android-helper.blogspot.in/2011/03/ android-search-in-listview-example.html – Akshay 2012-08-09 06:38:26

回答

0

看起來像我在做一點在使用新數據刷新我的ListView時出現錯誤,並且我還在其他地方調用了notifyDataSetListener。現在,它是工作絕對沒問題..