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