如何在安卓中实现SearchView过滤器?

3

由于我还不熟悉Android,我不知道如何实现SearchView过滤器。由于我使用了picasso从服务器检索图像并通过CardView显示它。我的期望输出是当用户输入汽车名称时,只有特定的GridView需要被过滤。请有人帮助我使这个SearchView可行。

MainActivity屏幕截图

MainActivity.java

public class MainActivity extends AppCompatActivity {

static String urlAddress="server_url";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    final GridView gv= (GridView) findViewById(R.id.gv);
    new Downloader(MainActivity.this,urlAddress,gv).execute();

}

}

spacecraft.java(Data_Object)

public class Spacecraft {

int id;
String name,propellant,description,imageUrl;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getPropellant() {
    return propellant;
}

public void setPropellant(String propellant) {
    this.propellant = propellant;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}
}

Connector.java

public class Connector {

public static HttpURLConnection connect(String urlAddress)
{
    try
    {
        URL url=new URL(urlAddress);
        HttpURLConnection con= (HttpURLConnection) url.openConnection();

        //PROPERTIES
        con.setRequestMethod("GET");
        con.setConnectTimeout(20000);
        con.setReadTimeout(20000);
        con.setDoInput(true);

        return con;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
}

DataParser.java

public class DataParser extends AsyncTask<Void,Void,Boolean> {

Context c;
String jsonData;
GridView gv;

ProgressDialog pd;
ArrayList<Spacecraft> spacecrafts=new ArrayList<>();

public DataParser(Context c, String jsonData, GridView gv) {
    this.c = c;
    this.jsonData = jsonData;
    this.gv = gv;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    pd=new ProgressDialog(c);
    pd.setTitle("Parse");
    pd.setMessage("Parsing..Please wait");
    pd.show();
}

@Override
protected Boolean doInBackground(Void... params) {
    return this.parseData();
}

@Override
protected void onPostExecute(Boolean parsed) {
    super.onPostExecute(parsed);

    pd.dismiss();

    if(parsed)
    {
        //BIND
        CustomAdapter adapter=new CustomAdapter(c,spacecrafts);
        gv.setAdapter(adapter);
    }else {
        Toast.makeText(c,"Unable To Parse",Toast.LENGTH_SHORT).show();
    }
}

private Boolean parseData()
{
    try
    {
        JSONArray ja=new JSONArray(jsonData);
        JSONObject jo;

        spacecrafts.clear();
        Spacecraft spacecraft;

        for (int i=0;i<ja.length();i++)
        {
            jo=ja.getJSONObject(i);

            int id=jo.getInt("id");
            String name=jo.getString("name");
            String prop=jo.getString("propellant");
            String desc=jo.getString("description");
            String imageUrl=jo.getString("imageurl");

            spacecraft=new Spacecraft();

            spacecraft.setId(id);
            spacecraft.setName(name);
            spacecraft.setPropellant(prop);
            spacecraft.setDescription(desc);
            spacecraft.setImageUrl(imageUrl);

            spacecrafts.add(spacecraft);

        }

        return true;

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return false;
}
}

Downloader.java

public class Downloader extends AsyncTask<Void,Void,String> {

Context c;
String urlAddress;
GridView gv;

ProgressDialog pd;

public Downloader(Context c, String urlAddress, GridView gv) {
    this.c = c;
    this.urlAddress = urlAddress;
    this.gv = gv;
}



@Override
protected void onPreExecute() {
    super.onPreExecute();

    pd=new ProgressDialog(c);
    pd.setTitle("Retrieve");
    pd.setMessage("Retrieving..Please wait");
    pd.show();

}

@Override
protected String doInBackground(Void... params) {
    return this.downloadData();
}

@Override
protected void onPostExecute(String jsonData) {
    super.onPostExecute(jsonData);

    pd.dismiss();

    if(jsonData==null)
    {
        Toast.makeText(c,"Unsuccessful,No Data Retrieved ",Toast.LENGTH_SHORT).show();
    }else {
        //PARSER
        DataParser parser=new DataParser(c,jsonData,gv);
        parser.execute();

    }

}

private String downloadData()
{
    HttpURLConnection con=Connector.connect(urlAddress);
    if(con==null)
    {
        return null;
    }

    try
    {
        InputStream is=new BufferedInputStream(con.getInputStream());
        BufferedReader br=new BufferedReader(new InputStreamReader(is));

        String line;
        StringBuffer jsonData=new StringBuffer();

        while ((line=br.readLine()) !=null)
        {
            jsonData.append(line+"\n");
        }

        br.close();
        is.close();

        return jsonData.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

public class execute {
}
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

Context c;
ArrayList<Spacecraft> spacecrafts;

public CustomAdapter(Context c, ArrayList<Spacecraft> spacecrafts) {
    this.c = c;
    this.spacecrafts = spacecrafts;
}

@Override
public int getCount() {
    return spacecrafts.size();
}

@Override
public Object getItem(int position) {
    return spacecrafts.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null)
    {
        convertView= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
    }

    TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
    ImageView img= (ImageView) convertView.findViewById(R.id.spacecraftImage);

    final Spacecraft s= (Spacecraft) this.getItem(position);

    nameTxt.setText(s.getName());
    PicassoClient.downloadImage(c, s.getImageUrl(), img);



    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDetailACtivity(s.getName(),s.getPropellant(),s.getDescription(),s.getImageUrl());
        }
    });

    return convertView;
}

private void openDetailACtivity(String name,String propellant,String 
 description,String imageUrl)
  {
    Intent i=new Intent(c, DetailActivity.class);

    //PACK DATA
    i.putExtra("NAME_KEY",name);
    i.putExtra("PROPELLANT_KEY",propellant);
    i.putExtra("DESCRIPTION_KEY",description);
    i.putExtra("IMAGEURL_KEY",imageUrl);

    c.startActivity(i);
 }
 } 

PicassoClient.java

public class PicassoClient {

public static void downloadImage(Context c,String imageUrl,ImageView img)
{
    if(imageUrl!=null && imageUrl.length()>0)
    {

Picasso.with(c).load(imageUrl).placeholder(R.drawable.placeholder).into(img);
    }else {
        Picasso.with(c).load(R.drawable.placeholder).into(img);
    }
}
}

内容主要部分

   <?xml version="1.0" encoding="utf-8"?>
    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light" />

     <GridView
       android:id="@+id/gv"
     android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="49dp" />
  </RelativeLayout>

model.xml

  <?xml version="1.0" encoding="utf-8"?>
  <android.support.v7.widget.CardView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" android:layout_width="match_parent"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:layout_margin="10dp"
  card_view:cardCornerRadius="5dp"
  card_view:cardElevation="5dp"
  android:layout_height="150dp">

 <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:id="@+id/spacecraftImage"
        android:padding="10dp"
        android:src="@drawable/placeholder" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name"
        android:id="@+id/nameTxt"
        android:padding="10dp"
        android:textColor="@color/colorAccent"
        android:layout_alignParentLeft="true"
         />

</LinearLayout>
</android.support.v7.widget.CardView>

这是用于展示产品描述的DetailActivity。 DetailActivity屏幕 activity_detail
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_detail" />

DetailActivity.java

public class DetailActivity extends AppCompatActivity {

TextView nameTxt,propTxt,descTxt;

//Initialize webservice URL
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    nameTxt= (TextView) findViewById(R.id.nameTxtDetail);
    descTxt= (TextView) findViewById(R.id.descDetailTxt);
    propTxt= (TextView) findViewById(R.id.propellantTxtDetail);
    img= (ImageView) findViewById(R.id.spacecraftImageDetail);



    //RECEIVE
    Intent i=this.getIntent();
    final String name=i.getExtras().getString("NAME_KEY");
    String propellant=i.getExtras().getString("PROPELLANT_KEY");
    String desc=i.getExtras().getString("DESCRIPTION_KEY");
    String imageurl=i.getExtras().getString("IMAGEURL_KEY");

    //BIND
     nameTxt.setText(name);
     propTxt.setText(propellant);
     descTxt.setText(desc);
     PicassoClient.downloadImage(this,imageurl,img);
  }
  }

项目结构

数据库结构

5个回答

0

1:首先在菜单中创建一个带有searchView项的菜单,然后将该菜单填充到您的MainActivity类中

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:orderInCategory="100"
    android:title="@string/action_search"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView" />


这个答案不够,还需要Java代码。 - Farhana Naaz Ansari
同样,我也在寻找Java代码的功能。 - Sandy
我按照要求添加了所有内容,但什么都不起作用,也没有错误提示,请帮帮我。 - Sandy
检查您的适配器类中列表的大小!无论列表项是否存在。 - Faisal iqbal
你是否将步骤2中所提到的注释中的Parselist添加到MainActivity类中的Staticlist中了呢? - Faisal iqbal
显示剩余2条评论

0
4:inflate it and then create instance of y0ur adapter class and then call these methods 
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.search_menu, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                    .getActionView();
            searchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));
            searchView.setMaxWidth(Integer.MAX_VALUE);
            // listening to search query text change
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    // filter recycler view when query submitted
                    customAdapter.filter(query, "role");
                    return false; }
                @Override
                public boolean onQueryTextChange(String query) {
                    // filter recycler view when text is changed
                    customAdapter.filter(query, "role");
                    return false; }});
            return true; }

customAdapter.filter(或)customAdapter.flterResult?? - Sandy
将此函数添加到您的适配器类中,如第3步所述 @Sandy - Faisal iqbal
customadapter.filter() - Faisal iqbal
你在适配器类中检查了列表大小吗? - Faisal iqbal
如何检查它? - Sandy

0
        2:  Create static list in Your MainActivity Class

            public class MainActivity extends AppCompatActivity {

            static String urlAddress="server_url";
              public static ArrayList<Spacecraft> list = new ArrayList<>();


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

                final GridView gv= (GridView) findViewById(R.id.gv);
                new Downloader(MainActivity.this,urlAddress,gv).execute();
    // add the parse datalist in this static list by list.addall() then pass this list to custom adapter
        CustomAdpater customAdapter = new CustomAdpater(this,list);
// then set the adapetr to your gridview

     }
    }     

0
3:In Your Custom adapter class create function
 public void filter(String charText,String role) {

  if (role .equals("role")) {

            charText = charText.toLowerCase(Locale.getDefault());

            MainActivity.list.clear();
            if (charText.length() == 0) {
// char text is equlas to zero then add non filter list 
                MainActivity.list.addAll(spacecrafts);

            } else {
                for (SpaceCraft spaceCraft : spacecrafts) {
                    if (charText.length() != 0 && 
     spaceCraft.name.toLowerCase(Locale.getDefault()).contains(charText)) {
                       MainActivity.list.add(spaceCraft);
                    } 
                }
            }
            notifyDataSetChanged();
        }
}
    }

spaceCraft.name代表什么?它显示错误,当我改用getName()时,错误消失了。 - Sandy
错误了...实际上是 spacecraft.getname()。 - Faisal iqbal

0
add this log in your getcount() method
then in logcat check it
 public class CustomAdapter extends BaseAdapter {

Context c;
ArrayList<Spacecraft> spacecrafts;

public CustomAdapter(Context c, ArrayList<Spacecraft> spacecrafts) {
    this.c = c;
    this.spacecrafts = spacecrafts;
}

@Override
public int getCount() {
   Log.d("size", "checksize: " +  spacecrafts.size());

    return spacecrafts.size();
}

将以下的代码添加到你的列表大小检查中 @Sandy - Faisal iqbal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接