2011-12-23 48 views
0

我目前有一個可擴展列表,我希望能夠搜索。我一直在關注Android開發者網站上的this指南,但我堅持實際的方法和展示結果。如何使用android搜索搜索數組列表?

1)我的搜索方法被稱爲doMySearch 2)我想通過孩子陣列

代碼擴展列表 包com.sammy.umass搜索;

import android.app.ExpandableListActivity; 
import android.app.SearchManager; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.ContextMenu; 
import android.view.Gravity; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.view.Window; 
import android.widget.AbsListView; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ExpandableListView.ExpandableListContextMenuInfo; 


public class DirectionsScreenActivity extends ExpandableListActivity { 
ExpandableListAdapter mAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.directions); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); 
    Intent intent = getIntent(); 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
    { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     doMySearch(query); 
    } 
    // Set up our adapter 
    mAdapter = new MyExpandableListAdapter(); 
    setListAdapter(mAdapter); 
    registerForContextMenu(getExpandableListView()); 
} 

private void doMySearch(String query) 
{ 
    //Search Method 

} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    menu.setHeaderTitle("Sample menu"); 
    menu.add(0, 0, 0, R.string.expandable_list_sample_action); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); 

    String title = ((TextView) info.targetView).getText().toString(); 

    int type = ExpandableListView.getPackedPositionType(info.packedPosition); 
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
     int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
     Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos, 
       Toast.LENGTH_SHORT).show(); 
     return true; 
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
     int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
     Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    return false; 
} 

/** 
* A simple adapter which maintains an ArrayList of photo resource Ids. 
* Each photo is displayed as an image. This adapter supports clearing the 
* list of photos and adding a new photo. 
* 
*/ 
public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    // Sample data set. children[i] contains the children (String[]) for groups[i]. 
    private String[] groups = { " Academic", " Research & Labs", " Dining Commons", " Points of Interest"," Adminstriation", 
      " Central"," North Apartments"," Northeast"," Orchard Hill"," Southwest"," Sylvan"}; 
    private String[][] children = { 
      //Academic Buildings 
      { "Arnold House","Barlett Hall","Berkshire House","Blaisdell House","Bowditch Hall","Clark Hall","Draper Hall","Duda Hall","East Experiment Station", 
       "Fernal Hall","French Hall","Furcolo Hall","H. Alfond Management Center","Hampshire House","Herter Hall","Hillis House", 
       "Holdsworth Hall","Isenberg School Of Management","Machmer Hall","Marcus Hall","Marston Hall","Middlesex House","Montague House", 
       "Munson Hall","New Africa House","Skinner Hall","South College","Stockbridge/Bowker","Studio Arts Building","Thompson Hall","Tobin Hall", 
       "West Experiment Station"}, 
      //Research and Labs 
      { "Agricultural Engineering","Apiary Laboratory","Astronomy Building","Chenoweth Laboratory","Computer Science Building", 
      "Conte Polymer Research Building","Engineering Laboratory 1","Engineering Laboratory 2","Flint Laboratory","Goessmann Laboratory", 
      "Gunness Laboratory","Hasbrouck Laboratory","Hatch Laboratory","Integrated Science Building","Knowles Engineering Research Building", 
      "Lederle Graduate Reaserch Building","Morrill Science Center","Paige Laboratory","Thayer ANimal Disease Laboratory","W.E.B. Du Bois Library"}, 
      //Dining Commons 
      { "Berkshire", "Franklin","Hampden","Hampshire","Worcester" }, 
      //Points of Interest 
      { "Bowditch Lodge","Boyden Gym","Campus Center/Hotel","Curry Hicks","Durfee Conservatory","Farley Lodge","Fine Arts Center","Grinnell Arena", 
       "Haigis Mall","Mahar Auditorium","McGuirk Alumni Stadium","Mullins Center","Observatory","Old Chapel","Recreation Center","Student Union", 
       "Totman Gym","Vistor's Center"}, 
      //Administration 
      { "Army ROTC Building", "Auxiliary Services Food Store","Campus Center Parking","Chancellor's House","Cold Storage Building", 
        "Police Department","Forest and Parks Building","Goodell","Physical Plant","PVTA Transit Facility","Research Administration", 
        "Shade Tree Lab","Textbook Annex","University Health Services","Whitmore Administration"}, 
      //Central 
      { "Baker", "Brett","Butterfield","Chadbourne","Gorman","Greenough","Van Meter","Wheeler" }, 
      //North Apartments 
      { "North A", "North B","North C","North D" }, 
      //Northeast 
      { "Crabtree", "Dwight","Hamlin","Johnson","Knowlton","Leach","Lewis","Mary Lyon","Thatcher" }, 
      //Orchard Hill 
      { "Dickinson", "Field","Grayson","Webster" }, 
      //Southwest 
      { "Cance", "Coolidge","Crampton","Emerson","James","John Adams","John Quincy Admas","Kennedy","MacKimmie","Melville","Moore","Patteron", 
       "Pierpont","Prince","Thoreau","Washington" }, 
      //Sylvan 
      { "Brown", "Cashin","McNamara"}, 

    }; 

    public Object getChild(int groupPosition, int childPosition) { 
     return children[groupPosition][childPosition]; 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    public TextView getGenericView() { 
     // Layout parameters for the ExpandableListView 
     AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT, 64); 

     TextView textView = new TextView(DirectionsScreenActivity.this); 
     textView.setLayoutParams(lp); 
     // Center the text vertically 
     textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
     // Set the text starting position 
     textView.setPadding(36, 0, 0, 0); 
     return textView; 
    } 

    public View getChildView(final int groupPosition,final int childPosition, boolean isLastChild, 
      View convertView, ViewGroup parent) { 
     TextView textView = getGenericView(); 
     textView.setText(getChild(groupPosition, childPosition).toString()); 
     textView.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view){ 
      Uri uriToOpen = getUriForView(groupPosition, childPosition); 
      Intent i = new Intent(Intent.ACTION_VIEW, uriToOpen); 
      startActivity(i); 
      } 


    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    public int getGroupCount() { 
     return groups.length; 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
      ViewGroup parent) { 
     TextView textView = getGenericView(); 
     textView.setText(getGroup(groupPosition).toString()); 
     return textView; 
    } 

    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 



} 
} 

回答

2

首先,因爲你已經張貼更多的代碼比要回答這個問題您的文章非常難讀。

無論如何,兒童數組在BaseExpandableListAdapter中聲明爲私有成員變量。您尚未提供任何工具來訪問不同類別的陣列(即DirectionsScreenActivity),因此您需要提供某種方法讓DirectionsScreenActivity訪問陣列,然後才能對其執行任何操作。

一種方法是製作一個公共(或受保護的)方法,該方法只是返回數組,但可能提供太多訪問權限。

我會做的是將搜索方法移動到MyExpandableListAdapter即。

public [some meaningful return type] doSearch(String query) 
{ 
     // Search code 
} 

而是調用DirectionsScreenActivitydoMySearch()的 - 通過您的MyExpandableListAdapter實例調用doSearch()(因爲已經有你的陣列),以及做任何你需要做的,結果前:

if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
{ 
    String query = intent.getStringExtra(SearchManager.QUERY); 
    [some meaningful type] result = mAdapter.doSearch(query); 

    // Do something with the result 
} 

至於如何進行搜索,那只是標準的Java,並不是針對Android的。在Java中有一百萬個和一個基本搜索算法的例子,所以我不會在這裏詳細介紹它們。

作爲一個方面說明,您可以通過使用散列表的散列表或散列表String --> List<String>來獲得更好的性能。鍵將是組,值將是兒童列表。

這樣,如果有人正在查找所有管理員建築物,您可以在固定時間內獲取管理員建築物列表(只需使用「管理」鍵從哈希表中檢索列表[值])即可。給定名稱的建築羣是有點棘手的,在最壞的情況下需要迭代散列表中的所有項目。

+0

由於這看起來是功課,所以我會再添加一個提示:不要在Java中使用==來進行字符串比較。使用'string1.equals(anotherString)'。 – debracey