Another Option
How to search Item using Volley on android studio
MainActivity.Java
MenuItem action,reg,cart;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Type To Search");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(this, SearchResultsActivity.class);
intent.putExtra("query", query);
startActivity(intent);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
action = menu.findItem(R.id.action_search);
reg = menu.findItem(R.id.action_reg);
cart = menu.findItem(R.id.action_cart);
return true;
}
SearchActivityLayout layout .xml
<LinearLayout 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:background="@color/white"
tools:context=".SearchItem">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myRecycleView_Search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"/>
</LinearLayout>
SearchLayout.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_layout);
recyclerView = findViewById(R.id.myRecycleView_Search);
String query =getIntent().getStringExtra("query");
// Set up the RecyclerView
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
// Use Volley to perform the search operation
performSearch(query);
}
private void performSearch(String query) {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://mydomain.com/search.php?q="+query;
Toast.makeText(this,url,Toast.LENGTH_LONG).show();
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Handle the search result here
mAdapter = new SearchAdapter(SearchLayout.this,response);
recyclerView.setAdapter(mAdapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle the error here
}
});
queue.add(request);
}
Adapter.java
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchResultViewHolder> {
private JSONArray mSearchResults;
Context mCtx;
public SearchAdapter(Context mCtx, JSONArray searchResults) {
mSearchResults = searchResults;
this.mCtx = mCtx;
}
@NonNull
@Override
public SearchAdapter.SearchResultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.catalogue_horizon_item_layout, parent, false);
return new SearchResultViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SearchResultViewHolder holder, int position) {
try {
JSONObject searchResult = mSearchResults.getJSONObject(position);
String title = searchResult.getString("PTitleName");
String Ptitle = searchResult.getString("PTitle");
String url = searchResult.getString("Price");
String imageUrl = searchResult.getString("PImage");
Picasso.get().load(imageUrl).into(holder.pImage);
holder.txtTitle.setText(Ptitle);
holder.txtPrice.setText(title);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int clickedPosition = holder.getAdapterPosition();
try {
JSONObject clickedResult = mSearchResults.getJSONObject(clickedPosition);
// Pass the data to another activity
Intent intent = new Intent(view.getContext(), DetailPage.class);
intent.putExtra("PID", clickedResult.getString("PID"));
intent.putExtra("PTitleName", clickedResult.getString("PTitleName"));
intent.putExtra("PTitle", clickedResult.getString("PTitle"));
intent.putExtra("Price", clickedResult.getString("Price"));
intent.putExtra("PImage", clickedResult.getString("PImage"));
intent.putExtra("PDiscription", clickedResult.getString("PDiscription"));
intent.putExtra("Stock", clickedResult.getString("Stock"));
mCtx.startActivity(intent);
//view.getContext().startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return mSearchResults.length();
}
public class SearchResultViewHolder extends RecyclerView.ViewHolder {
private LinearLayout item_contact;
ImageView pImage;
TextView txtTitle,txtPrice,txtStoke;
RatingBar ratingBar;
public SearchResultViewHolder(@NonNull View itemView) {
super(itemView);
item_contact = (LinearLayout) itemView.findViewById(R.id.cateloge_hozon_Item);
pImage= (ImageView) itemView.findViewById(R.id.proImgH);
txtTitle= itemView.findViewById(R.id.proTitleH);
txtPrice= itemView.findViewById(R.id.proPriceH);
}
}
}
catalogue_horizon_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cateloge_hozon_Item"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="5dp"
android:elevation="3dp"
android:background="@drawable/bg_item"
android:orientation="horizontal">
<ImageView
android:id="@+id/proImgH"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/heart"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@+id/proTitleH"
android:fontFamily="@font/solaimanlipi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Watch"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="14dp"/>
<TextView
android:id="@+id/proDisH"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/solaimanlipi"
android:text="jut"
android:textColor="#7E7103"
android:textStyle="bold"
android:visibility="gone"
android:textSize="16dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/proStokeH"
android:layout_width="0dp"
android:fontFamily="@font/solaimanlipi"
android:layout_height="wrap_content"
android:text="Stoke"
android:textColor="#870581"
android:textStyle="bold"
android:layout_weight="1"
android:textSize="16dp"/>
<ImageView
android:id="@+id/proImgL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hart_on"
android:layout_gravity="right"
android:layout_weight="1"
android:visibility="gone"/>
</LinearLayout>
<TextView
android:id="@+id/proPriceH"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/solaimanlipi"
android:text="$ 985/-"
android:textColor="#DC0101"
android:textStyle="bold"
android:textSize="16dp"/>
<RatingBar
android:id="@+id/ratingBarH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progressTint="#870581"
android:numStars="5"
android:rating="3.5"
android:stepSize="0.5"
style="@style/Widget.AppCompat.RatingBar.Indicator"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>