在android gridview

时间:2015-09-06 15:01:01

标签: android gridview searchview

我在我的Android应用程序中有gridview显示图像和文本所以我想在gridview中添加searchview,这样当我在searchview中输入内容时,我应该能够看到匹配的gridview项目,,,我已设法获得seachview代码对于gridview但我无法在gridview中实现这些代码,这里是searchview代码

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

public class GridviewAdapter extends BaseAdapter {

private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private Activity activity;

public GridviewAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<Integer> listFlag) {
    super();
    this.listCountry = listCountry;
    this.listFlag = listFlag;
    this.activity = activity;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listCountry.size();
}

@Override
public String getItem(int position) {
    // TODO Auto-generated method stub
    return listCountry.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static class ViewHolder
{
    public ImageView imgViewFlag;
    public TextView txtViewTitle;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder view;
    LayoutInflater inflator = activity.getLayoutInflater();

    if(convertView==null)
    {
        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.gridview_row, null);

        view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
        view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);

        convertView.setTag(view);
    }
    else
    {
        view = (ViewHolder) convertView.getTag();
    }

    view.txtViewTitle.setText(listCountry.get(position));
    view.imgViewFlag.setImageResource(listFlag.get(position));

    return convertView;
}

1 个答案:

答案 0 :(得分: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;
}
});

让你的适配器为Country数组列表取一个参数,另一个用于flags。

最后使用listCountrytemp和listFlagtemp创建一个适配器并将其发送到网格视图。我不熟悉searchviews,所以我不知道你应该在最后一步做什么。

这可能不是最好的解决方案,但对我来说它运作得很好。

当您在Facebook搜索等搜索视图中键入时,网格视图不会更新。

希望这会有所帮助

编辑:

使用新的arraylists创建gridview:

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

 gridView.setAdapter(mAdapter);