2015-09-06 71 views
0

我有GridView控件在我的Android應用程序,顯示圖像和文字,所以我想在GridView中添加搜索查看,這樣,當我鍵入搜索查看東西,我應該能夠看到匹配的GridView項,,,我有設法得到seachview代碼gridview的,但我沒能在我的GridView控件來實現這些代碼,這裏是搜索查看代碼android系統中的GridView實現搜索查看

MainActivity

public class MainActivity extends Activity { 

GridView gv; 
SearchView sv; 

ArrayAdapter<String> adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Initial Views 
    gv=(GridView) findViewById(R.id.gridView1); 
    sv=(SearchView) findViewById(R.id.searchView1); 

    // Adapter 
    gv.setAdapter adapter 

    // Set Adapter 
    adapter=new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1.teams); 

    sv.setOnQueryTextListener(new OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextSubmit(String arg0) { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String query) { 
      // TODO Auto-generated method stub 

      adapter.getFilter().filter(query); 

      return false; 
     } 
    }); 

} 

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.searching.MainActivity" > 

<GridView 
    android:id="@+id/gridView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_marginTop="49dp" 
    android:numColumns="3" > 
</GridView> 

<SearchView 
    android:id="@+id/searchView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/gridView1" 
    android:layout_alignParentTop="true" 
    android:queryHint="Search Here" 
    android:layout_alignRight="@+id/gridView1" > 
</SearchView> 

,所以我想在我的GridView 這裏實現這個代碼是我的GridView控件代碼

MainActivity

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 

private GridviewAdapter mAdapter; 
private ArrayList<String> listCountry; 
private ArrayList<Integer> listFlag; 

private GridView gridView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    prepareList(); 

    // prepared arraylist and passed it to the Adapter class 
    mAdapter = new GridviewAdapter(this,listCountry, listFlag); 

    // Set custom adapter to gridview 
    gridView = (GridView) findViewById(R.id.gridView1); 
    gridView.setAdapter(mAdapter); 

    // Implement On Item click listener 
    gridView.setOnItemClickListener(new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
       long arg3) { 
      Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

} 

public void prepareList() 
{ 
     listCountry = new ArrayList<String>(); 

     listCountry.add("india"); 
     listCountry.add("Brazil"); 
     listCountry.add("Canada"); 
     listCountry.add("China"); 
     listCountry.add("France"); 
     listCountry.add("Germany"); 
     listCountry.add("Iran"); 
     listCountry.add("Italy"); 
     listCountry.add("Japan"); 
     listCountry.add("Korea"); 
     listCountry.add("Mexico"); 
     listCountry.add("Netherlands"); 
     listCountry.add("Portugal"); 
     listCountry.add("Russia"); 
     listCountry.add("Saudi Arabia"); 
     listCountry.add("Spain"); 
     listCountry.add("Turkey"); 
     listCountry.add("United Kingdom"); 
     listCountry.add("United States"); 

     listFlag = new ArrayList<Integer>(); 
     listFlag.add(R.drawable.india); 
     listFlag.add(R.drawable.brazil); 
     listFlag.add(R.drawable.canada); 
     listFlag.add(R.drawable.china); 
     listFlag.add(R.drawable.france); 
     listFlag.add(R.drawable.germany); 
     listFlag.add(R.drawable.iran); 
     listFlag.add(R.drawable.italy); 
     listFlag.add(R.drawable.japan); 
     listFlag.add(R.drawable.korea); 
     listFlag.add(R.drawable.mexico); 
     listFlag.add(R.drawable.netherlands); 
     listFlag.add(R.drawable.portugal); 
     listFlag.add(R.drawable.russia); 
     listFlag.add(R.drawable.saudi_arabia); 
     listFlag.add(R.drawable.spain); 
     listFlag.add(R.drawable.turkey); 
     listFlag.add(R.drawable.united_kingdom); 
     listFlag.add(R.drawable.united_states); 
} 

GridviewAdapter

回答

0

在這裏你去:

sv.setOnQueryTextListener(new OnQueryTextListener() { 

    @Override 
    public boolean onQueryTextSubmit(String newText) { 
     // filter your adapter here 
     private ArrayList<String> listCountrytemp; 
     private ArrayList<Integer> listFlagtemp; 
     for(int i =0 ; i<listContry.size() ; i++){ 
      if(listContry(i).contains(newText)){ 
         listCountrytemp.add(listContry(i)); 
         listFlagtemp.add(listFlag(i)); 
      } 
     } 
     return false; 
    } 

    @Override 

public boolean onQueryTextChange(String query) { 
    // TODO Auto-generated method stub 

    adapter.getFilter().filter(query); 

    return false; 
} 
}); 

使您的適配器爲國家數組列表和另一個標誌採取兩個參數之一。

最後創建與listCountrytemp和listFlagtemp適配器,並將其發送到網格視圖。我不熟悉searchviews,所以我不知道你應該在哪裏做最後一步。

這可能不是最好的解決辦法,但對我來說,工作得很好。

不是因爲你在像Facebook搜索的搜索鍵入視圖網格視圖將更新。

希望這有助於

編輯:

創建gridview的新的ArrayList:

mAdapter = new GridviewAdapter(this,listCountrytemp , listFlagtemp); 

gridView.setAdapter(mAdapter); 
+0

如何創建與listCountrytemp和listFlagtemp適配器?併發送到網格查看 –

+0

檢查編輯, –