FirebaseUI-Android
FirebaseUI-Android copied to clipboard
How to pass onQueryTextSubmit Activity to ListContentFragment
MainActivity
public class MainActivity extends AppCompatActivity {
ListView list;
public List<Product> products=new ArrayList<>();
private DrawerLayout mDrawerLayout;
public Context context;
ViewPager viewPager;
TabLayout tabs;
ProgressBar mProgressBar;
Adapter adapter=new Adapter(getSupportFragmentManager());
public String qc="Energy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
// mProgressBar = (ProgressBar) findViewById(R.id.pbHeaderProgress);
// Create Navigation drawer and inflate layout
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
// Adding menu icon to Toolbar
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setHomeAsUpIndicator(R.drawable.ic_dehaze_black_24dp);
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
// Set behavior of Navigation drawer
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Set item in checked state
menuItem.setChecked(true);
// TODO: handle navigation
// Closing drawer on item click
mDrawerLayout.closeDrawers();
return true;
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Hello Snackbar!",
Snackbar.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// Retrieve the SearchView and plug it into SearchManager
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
EditText searchPlate = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchPlate.setHint("Search En");
View searchPlateView = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchPlateView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
// use this method for search process
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// int positions= tabs.getSelectedTabPosition();
Bundle args = new Bundle();
args.putString("edtteext", query);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment newFragment = new ListContentFragment(qc); //your search fragment
Bundle argsd = new Bundle();
argsd.putString("edtteext", query);
newFragment.setArguments(args);
transaction.replace(R.id.viewpager, newFragment);
//transaction.addToBackStack(null);
transaction.commit();
//adapter.notifyDataSetChanged();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == android.R.id.home) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
return super.onOptionsItemSelected(item);
}
private void setupViewPager(ViewPager viewPager) {
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
ListContentFragment list= new ListContentFragment(qc);
list.setArguments(bundle);
adapter.addFragment(list, "Energy");
//adapter.addFragment(new TileContentFragment(), "Video");
// adapter.addFragment(new CardContentFragment(), "News");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentStatePagerAdapter {
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);
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
}
}
**ListContentFragment**
public class ListContentFragment extends Fragment {
public List<Product> products=new ArrayList<>();
public ContentAdapter contentAdapter;
RecyclerView recyclerView;
public int page=0;
public boolean limitOver=false;
public String q;
public ListContentFragment(String q) {
this.q=q;
}
@Override
public void onResume() {
super.onResume();
}
protected void displayReceivedData(String message)
{
Log.d("Show String==",message);
}
public void doSomething(String myValue){
Log.d("Show String==",myValue);
}
//SearchView searchView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
if(getArguments()!=null) {
String strtext = getArguments().getString("edtteext");
if(strtext!=null) {
Log.d("Reload---==", strtext);
}
}
RecyclerView rootView = (RecyclerView) inflater.inflate(R.layout.recycler_view, container, false);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
ContentAdapter contentAdapter = new ContentAdapter(rootView.getContext(),rootView,products);
rootView.setAdapter(contentAdapter);
rootView.setHasFixedSize(true);
rootView.setLayoutManager(layoutManager);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Product product=new Product(1,"Hi how are","Hi how are","http://dev.soulilution.com/sit_chat/webroot/img/cake.icon.png");
products.add(product);
Product product2=new Product(1,"Hi how are","Hi how are","http://dev.soulilution.com/sit_chat/webroot/img/cake.icon.png");
products.add(product2);
}
public void setPorducatData(String data) throws JSONException {
JSONObject bookCatalogResult;
bookCatalogResult = (JSONObject) new JSONTokener(data).nextValue();
JSONArray books = bookCatalogResult.getJSONArray("list");
if(books != null) {
int num_books = books.length();
for(int i = 0; i < num_books; i++) {
JSONObject innerObject = books.getJSONObject(i);
Product p = new Product(innerObject.getInt("id"),innerObject.getString("title"),innerObject.getString("description"),innerObject.getString("image"));
products.add(p);
}
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView avator;
public TextView name;
public TextView description;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_list, parent, false));
avator = (ImageView) itemView.findViewById(R.id.list_avatar);
name = (TextView) itemView.findViewById(R.id.list_title);
description = (TextView) itemView.findViewById(R.id.list_desc);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
//Product pdd= products.get(getAdapterPosition());
Log.d("possition==",String.valueOf(getAdapterPosition()));
intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition());
context.startActivity(intent);
}
});
}
}
/**
* Adapter to display recycler view.
*/
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private static final int LENGTH = 1;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private OnLoadMoreListener onLoadMoreListener;
private String[] mPlaces;
private String[] mPlaceDesc;
private Drawable[] mPlaceAvators;
private String[] images;
private List<Product> pl=new ArrayList<>();
Context context;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean isLoading;
private boolean changeView;
Activity activity;
public ContentAdapter(Context context, RecyclerView recyclerView, List<Product> product) {
Resources resources = context.getResources();
this.context=context;
Log.d("Length==",String.valueOf(product.size()));
this.pl=product;
TypedArray a = resources.obtainTypedArray(R.array.place_avator);
mPlaceAvators = new Drawable[a.length()];
Iterator<Product> ps=product.iterator();
for (int i = 0; i < mPlaceAvators.length; i++) {
mPlaceAvators[i] = a.getDrawable(i);
}
//final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!isLoading ) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});
a.recycle();
}
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.onLoadMoreListener = mOnLoadMoreListener;
}
@Override
public int getItemViewType(int position) {
return pl.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view, parent, false);
changeView=true;
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(context).inflate(R.layout.item_loading, parent, false);
changeView=false;
}
//return null;
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(changeView==true) {
Product product = pl.get(position);
if(product!=null) {
Picasso.with(context).load(product.getImage()).into(holder.avator);
holder.name.setText(product.getName());
holder.description.setText(product.getDescripton());
}
}
}
@Override
public int getItemCount() {
//return LENGTH;
return pl == null ? 0 : pl.size();
}
public void setLoaded() {
isLoading = false;
}
private class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public LoadingViewHolder(View view) {
super(view);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
}
}
public class UserViewHolder extends RecyclerView.ViewHolder {
public TextView phone;
public TextView email;
public UserViewHolder(View view) {
super(view);
}
}
}
}
@sunnykumargpt I have to close this issue because I am not able to debug it for a few reasons
- You have provided your entire Main Activity, without enough information to find out which piece of code you're actually having an issue with.
- You did not provide any other information such as the version of the library you're using, what errors you are seeing, what behavior you want to achieve, etc.
Please provide all of the information requested in the issue template while also showing as little code as needed to reproduce the issue.