How to add Search view for Firebase realtime database? [closed] - android-layout

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am developing an android app with Firebase real-time database. I created RecyclerView with Cardview and want to make a SearchView in the toolbar for all the data. Please help me how to create this.
It is my Adapter file
public class RecycleAdepter extends RecyclerView.Adapter<RecycleAdepter.MyViewHolder> {
ArrayList<Blog> arrayList=new ArrayList<>();
RecycleAdepter(ArrayList<Blog> arrayList)
{
this.arrayList=arrayList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bank_row,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.Title.setText(arrayList.get(position).getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView Title;
public MyViewHolder(View itemView) {
super(itemView);
Title=(TextView) itemView.findViewById(R.id.Idiom_title);
}
}
public void setFilter(ArrayList<Blog> newList)
{
This is my Search code for Main activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem menuItem = menu.findItem(R.id.searchbar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}

From what I understand you have already downloaded all the data in a RecyclerView and then you just implement SearchView and search the data downloaded within the RecyclerView. This has already been explained here: How to filter a RecyclerView with a SearchView

Related

use Onclick in extends PagerAdapter

Hi I’m trying to create a horizontal slide where each horizontal page will have a button that will call a Dialog but onClick is not working in extends Pageradapter can anyone tell me what I’m doing wrong?
I gave the name onClickApprove to onClick that I want you to call Dialog. This Dialog is calling an Activity with a Ratingbar to make an assessment.
Thank you.
public class SliderAdapterUsado extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
BottomSheetDialog dialog;
Button show;
public SliderAdapterUsado(Context context){
this.context = context;
}
public String[] slide_rota ={
"Titulo1",
"Titulo2"
};
public String[] slide_nome={
"Descrção do titulo 1",
"Descrção do titulo 2"
};
#Override
public int getCount(){
return slide_rota.length;
}
#Override
public boolean isViewFromObject(View view, Object object){
return view == (LinearLayout) object;
}
#Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout_usados, container, false);
TextView slideHeading = view.findViewById(R.id.slide_rota);
TextView slideDescricao = view.findViewById(R.id.slide_nome);
slideHeading.setText(slide_rota[position]);
slideDescricao.setText(slide_nome[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
public void onClickAvaliar(View view) {
show = view.findViewById(R.id.show);
dialog = new BottomSheetDialog(dialog.getContext());
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog();
dialog.show();
}
});
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
private void createDialog(){
View view = layoutInflater.inflate(R.layout.activity_rating, null, false);
Button FeedBack = view.findViewById(R.id.FeedBack);
FeedBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.setContentView(view);
}
}
Please read the documentation.
Note this in particular (emphasis mine):
To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
The expectation is that the value set on the onClick xml attribute will be a method on the hosting Activity. Yours is in the adapter. That's why it doesn't work.
So either move that method to the hosting activity, or just handle the click event explicitly:
...
TextView slideHeading = view.findViewById(R.id.slide_rota);
TextView slideDescricao = view.findViewById(R.id.slide_nome);
Button slideButton = view.findViewById(R.id.slide_button);
slideButton.setOnClickListener (...)
...

Click on widget of a recyclerview item in Android Studio

I have a car that loads data from a remote database. Each item in the list has two buttons and I need to make different volley requests for each button. I know how to make the onclick work in the complete item but not how to do it for a widget inside the item.
Item screenshot
I hope you can help me, thanks in advance.
RecyclerViewAdapater:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
//vars
private List<Buttons> buttonsList;
private Context mContext;
public RecyclerViewAdapter(List<Buttons> buttonsList, Context mContext) {
this.buttonsList = buttonsList;
this.mContext = mContext;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
Buttons buttons = buttonsList.get(position);
Glide.with(mContext)
.load(buttons.getButton_url())
.into(holder.image);
holder.name.setText(buttons.getButton_score());
holder.price.setText(buttons.getButton_price());
}
#Override
public int getItemCount() {
return buttonsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image;
TextView name, price;
Button buy_button, select_button;
public ViewHolder(View itemView) {
super(itemView);
image = (CircleImageView) itemView.findViewById(R.id.image_view);
name = (TextView) itemView.findViewById(R.id.name);
price = (TextView) itemView.findViewById(R.id.price);
buy_button = (Button) itemView.findViewById(R.id.buy_button_item);
select_button = (Button) itemView.findViewById(R.id.select_button_item);
}
}
}
MainActivity:
private void initRecyclerView(){
Log.d(TAG, "initRecyclerView: init recyclerview");
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerButtonsView);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(buttonsList, this);
recyclerView.setAdapter(adapter);
}
I solved this with the answer of LordParsley in this post

How to implement onActivityResult within RecyclerView Fragment

I am trying to have a button within a Tab which is part of an activity for uploading images, when clicking the button inside one of the 2 Tabs presented in the activity ( Tab 1 pics, Tab 2 Vid ) the user could choose an image from the device.
My problem is implemneting the onClick Listener and onActivityResult which is not being used when placed after onClick, plus the #Override is being underlined as an error.
I guess i am not implementing the onClick the right way and not sure how to activate onActivityResult the proper way, i would appreciate any guidance;
MainActivty;
public class upload extends AppCompatActivity {
public String UserID;
private static final int REQUEST_SIGNUP = 0;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
Firebase.setAndroidContext(this);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new UploadPictures(), "Pictures");
adapter.addFragment(new UploadVideos(), "Videos");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And here is the Fragment Code;
public class UploadPictures extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView picture;
public EditText tagEditText;
public Button tagCurLoc, choosePic, uploadContent;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.pictue_upload, parent, false));
picture = (ImageView) itemView.findViewById(R.id.imgToUpload);
tagEditText = (EditText) itemView.findViewById(R.id.tagEditText);
tagCurLoc = (Button) itemView.findViewById(R.id.tagCurLoc);
choosePic = (Button) itemView.findViewById(R.id.choosePic);
choosePic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
onActivityResult(requestCode, resultCode, data);
Toast.makeText(getActivity().getApplicationContext(), "Canceled ", Toast.LENGTH_SHORT).show();
} else {
if (requestCode == RESULT_LOAD_IMAGE) {
Uri selectedImgPath = data.getData();
picture.setImageURI(selectedImgPath);
RealFilePath = Uri.parse(String.valueOf(selectedImgPath));
Toast.makeText(getActivity().getApplicationContext(), " " + RealFilePath, Toast.LENGTH_SHORT).show();
}
}
}
});
uploadContent = (Button) itemView.findViewById(R.id.uploadContent);
}
}
/**
* Adapter to display recycler view.
*/
public class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return LENGTH;
}
}
Not sure if the onClick listener for the button should be implemented in the Adapter or the View holder, and from there the issue of onActivityResult cant be used ?
Weel, solved after some more reading and few try outs.
The onClick listener should be implemented at the ViewHolder class followed by
#Override
onActivityResult.
within the onClickListener onActivityResult invoked by;
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and within onActivityResult;
onActivityResult(requestCode, resultCode, data);
Considering all of this taking place in an activity extending Fragment.

How to set an ItemLongClick on a spinner list ?

I have a spinner and I want to do a long click on an item on my list but the OnItemLongClick doesn't work ...
Does anybody know the new way to set a long click on a spinner item ?
Thank you
As per the android documentation there is no way to perform itemlong click on spinner item. Here is the Link
So if you want to achieve spinner with on item long click you have to make a custom adapter and set long click on view.
public class TestSpinnerAdapter extends BaseAdapter {
private String[] mArray;
public TestSpinnerAdapter(String[] array) {
mArray = array;
}
#Override
public int getCount() {
return mArray.length;
}
#Override
public Object getItem(int position) {
return mArray[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
LayoutInflater layoutInflater =LayoutInflater.from(parent.getContext());
convertView = layoutInflater.inflate(R.layout.adapter_spinner_item,parent,false);
((TextView)convertView.findViewById(R.id.tv_name)).setText(mArray[position]);
convertView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(parent.getContext(),"On Long Click",Toast.LENGTH_SHORT).show();
return false;
}
});
return convertView;
}
}
And set this adapter to your spinner
mTestSPN = (Spinner) findViewById(R.id.spn_test);
TestSpinnerAdapter testSpinnerAdapter = new TestSpinnerAdapter(getResources().getStringArray(R.array.array_name));
mTestSPN.setAdapter(testSpinnerAdapter);
In string.xml
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>

Making a VR video player using Google Cardboard [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to make a simple VR video player with Google CardBoard Android SDK ? I am a new man in this area. I am trying to change a program in github to play video for cardboard now !
I used Rajawali Library along with Google Cardboard Sdk and Modified initscene() in this way to get this work.
public class VideoRenderer extends RajawaliCardboardRenderer {
Context mContext;
private MediaPlayer mMediaPlayer;
private StreamingTexture mVideoTexture;
public VideoRenderer(Context context) {
super(context);
mContext = context;
}
#Override
protected void initScene() {
mMediaPlayer = MediaPlayer.create(getContext(),
R.raw.video);
mMediaPlayer.setLooping(true);
mVideoTexture = new StreamingTexture("sintelTrailer", mMediaPlayer);
Material material = new Material();
material.setColorInfluence(0);
try {
material.addTexture(mVideoTexture);
} catch (ATexture.TextureException e) {
e.printStackTrace();
}
Sphere sphere = new Sphere(50, 64, 32);
sphere.setScaleX(-1);
sphere.setMaterial(material);
getCurrentScene().addChild(sphere);
getCurrentCamera().setPosition(Vector3.ZERO);
getCurrentCamera().setFieldOfView(75);
mMediaPlayer.start();
}
#Override
protected void onRender(long ellapsedRealtime, double deltaTime) {
super.onRender(ellapsedRealtime, deltaTime);
mVideoTexture.update();
}
#Override
public void onPause() {
super.onPause();
if (mMediaPlayer != null)
mMediaPlayer.pause();
}
#Override
public void onResume() {
super.onResume();
if (mMediaPlayer != null)
mMediaPlayer.start();
}
#Override
public void onRenderSurfaceDestroyed(SurfaceTexture surfaceTexture) {
super.onRenderSurfaceDestroyed(surfaceTexture);
mMediaPlayer.stop();
mMediaPlayer.release();
}
}

Resources