2013-09-28 152 views
0

我正在從XML讀取數據,然後根據數據創建ListView並單擊ListView中的任何項目,以打開具有詳細信息的另一個活動。我試圖在ListView中實現搜索功能,下面是我的代碼,我不知道如何實現搜索,我嘗試了很多方法,但都沒有工作。任何幫助,高度讚賞。提前致謝。篩選器列表視圖數據

public class BrowseActors extends Activity { 
    private static final String TAG = "QuizListActivity"; 
    private ImageView bannerImageView; // displays a Image 
    // Search EditText 
    private EditText inputSearch; 
    // XML node keys 
    static final String KEY_TAG = "ActorData"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_NAME = "name"; 
    static final String KEY_ICON = "icon"; 

    String position; 
    String sourceFile; 
    // List items 
    ListView list; 
    //BinderActorData adapter = null; 
    List<HashMap<String, String>> actorDataCollection; 
    ArrayAdapter<HashMap<String, String>> adapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); // Remove title 
     setContentView(R.layout.browse_actor); 
     bannerImageView = (ImageView) findViewById(R.id.topImageView); 
     try { 
      // Get Category to filter 
      Intent in = getIntent(); 
      this.position = in.getStringExtra("position"); 
      sourceFile="BaseData.xml" 
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 
        .newInstance(); 
      DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
      Document doc = docBuilder.parse(getAssets().open(sourceFile)); 

      actorDataCollection = new ArrayList<HashMap<String, String>>(); 

      // normalize text representation 
      doc.getDocumentElement().normalize(); 

      NodeList quizList = doc.getElementsByTagName("ActorData"); 

      HashMap<String, String> map = null; 

      for (int i = 0; i < quizList.getLength(); i++) { 

       map = new HashMap<String, String>(); 

       Node firstQuestionNode = quizList.item(i); 

       if (firstQuestionNode.getNodeType() == Node.ELEMENT_NODE) { 

        Element firstAircraftElement = (Element) firstQuestionNode; 
         // 1.------- 
         NodeList idList = firstAircraftElement 
           .getElementsByTagName(KEY_ID); 
         Element firstIdElement = (Element) idList.item(0); 
         NodeList textIdList = firstIdElement.getChildNodes(); 
         // --id 
         map.put(KEY_ID, textIdList.item(0).getNodeValue() 
           .trim()); 

         // 2.------- 
         NodeList nameList = firstAircraftElement 
           .getElementsByTagName(KEY_NAME); 
         Element firstNameElement = (Element) nameList.item(0); 
         NodeList textNameList = firstNameElement 
           .getChildNodes(); 
         // --name 
         map.put(KEY_NAME, textNameList.item(0).getNodeValue() 
           .trim()); 

         // 3.------- 
         NodeList iconList = firstAircraftElement 
           .getElementsByTagName(KEY_ICON); 
         Element firstIconElement = (Element) iconList.item(0); 
         NodeList textIconList = firstIconElement 
           .getChildNodes(); 
         // -- Image Icon 
         map.put(KEY_ICON, textIconList.item(0).getNodeValue() 
           .trim()); 
         // Add to the Arraylist 
         actorDataCollection.add(map); 

       } 
      } 

      BinderActorData bindingData = new BinderActorData(this, 
        actorDataCollection); 
      // Adding items to listview 
      list = (ListView) findViewById(R.id.list); 
      // Adding items to listview 
      inputSearch = (EditText) findViewById(R.id.inputSearch); 

      list.setAdapter(bindingData); 

      /** 
      * Enabling Search Filter 
      * */ 
      inputSearch.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void onTextChanged(CharSequence cs, int arg1, int arg2, 
         int arg3) { 
        // When user changed the Text 
        //BrowseActors.this.bindingData.getFilter().filter(cs); 
       } 

       @Override 
       public void beforeTextChanged(CharSequence arg0, int arg1, 
         int arg2, int arg3) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void afterTextChanged(Editable arg0) { 
        // TODO Auto-generated method stub 
       } 
      }); 

      // Click event for single list row 
      list.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

        Intent i = new Intent(); 
        i.setClass(BrowseActors.this, ViewActor.class); 

        // parameters 
        i.putExtra("position", String.valueOf(position + 1)); 

        /* 
        * selected item parameters 
        */ 

        i.putExtra("name", 
          actorDataCollection.get(position).get(KEY_NAME)); 
        i.putExtra("icon", 
          actorDataCollection.get(position).get(KEY_ICON)); 

        // start the sample activity 
        startActivity(i); 
       } 
      }); 
     } 

     catch (IOException ex) { 
      Log.e("Error", ex.getMessage()); 
     } catch (Exception ex) { 
      Log.e("Error", "Loading exception"); 
     } 
    } 
} 

回答

0

搜索將在原始數據源上實現,它可能是XML/JSON /數據庫或其他任何東西。

您正在使用「position」參數進行搜索,我認爲您可以使用View視圖參數。 只需在ListView行中保留一個隱藏的字段,並將其與某種主鍵映射即可。

當發生OnClick事件時,您可以從視圖中獲取行佈局,並執行findViewByID()調用來獲取您的隱藏控件。從該控件的值中,您可以調用後端Web服務或運行x路徑查詢或任何您想要的。

+0

謝謝,但你可以給我一個例子。 – Sid