How to update listview in Fragment with custom adapter - java

I am giving product id with barcode scanner. I can add product to listView but when i try to increase or decrease amount of the product. It doesn't update UI. I used Toast message to see weather list is updated, it updates list but doesn't update UI
I have tried to use runOnUiThread() but i couldn't find any solution. How to update UI can you please help me
custom_lisView_row
BaseActivity which keeps MainFragment on it
public class BaseActivity extends AppCompatActivity {
public static final String MAIN_FRAGMENT = "mainFragment";
public static final String PRODUCTS = "products";
FragmentManager fragmentManager;
Dialog dialog ;
public static ArrayList<MyProduct> myProductList = new ArrayList<>();
public static MyTablet myTablet = new MyTablet();
Activity mActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
//Initialize fragment manager
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fl_BaseActivity, new MainFragment()).commit();
//Create database
mDatabase = FirebaseDatabase.getInstance().getReference();
dialog = new Dialog(this);
//Runs when i enter product id
initScanner();
}
public void updateMyProductList(MyProduct myProduct){
for(int i= 0 ; i< myProductList.size() ; i++ ){
MyProduct temp = myProductList.get(i);
if (temp.getId().equals(myProduct.getId())) {
temp.setAmount(temp.getAmount() + myProduct.getAmount());
myProductList.set(i, temp);
return;
}
}
myProductList.add(myProduct);
updateMainFragment();
}
private void initScanner() {
mDatabase.child(PRODUCTS).child(finalData).get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
#Override
public void onComplete(#NonNull Task<DataSnapshot> task) {
MyProduct myProduct = task.getResult().getValue(MyProduct.class);
myProduct.setAmount(1);
dialog.setContentView(R.layout.custom_product_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(false);
TextView tv_addBasket_product_dialog = dialog.findViewById(R.id.tv_addBasket_product_dialog);
tv_addBasket_product_dialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateMyProductList(myProduct);
dialog.dismiss();
}
});
dialog.show();
}
};
}
public void updateMainFragment() {
if (isExist(MAIN_FRAGMENT)) {
Fragment fragment = findFragment(MAIN_FRAGMENT);
((MainFragment) fragment).updateMyList();
}
}
//Add fragments to BaseActivity
public void addFragments(Fragment fragment, String tag) {
fragmentManager.beginTransaction().add(R.id.fl_BaseActivity, fragment, tag).commit();
}
//Replace fragments to BaseActivity
public void replaceFragments(Fragment fragment, String tag) {
fragmentManager.beginTransaction().replace(R.id.fl_BaseActivity, fragment, tag).commit();
}
//Remove fragment from BaseActivity
public void removeFragment(String tag) {
Fragment fragmentB = fragmentManager.findFragmentByTag(tag);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (fragmentB != null) {
fragmentTransaction.remove(fragmentB);
fragmentTransaction.commit();
}
}
// finds fragment and returns it
// It may return null first check fragment is exist. use isExist() method
public Fragment findFragment(String tag) {
Fragment fragment = fragmentManager.findFragmentByTag(tag);
return fragment;
}
//Check fragment exist in BaseActivity
public boolean isExist(String tag) {
Fragment fragmentB = fragmentManager.findFragmentByTag(tag);
if (fragmentB != null) {
return true;
}
return false;
}
}
MainFragment
public class MainFragment extends Fragment {
ListView lv_MainFragment;
public MyProductListAdapter myListAdapter;
public static ArrayList<MyProduct> myProductList;
Activity mActivity;
#Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = getActivity();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myProductList = BaseActivity.myProductList;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
lv_MainFragment = view.findViewById(R.id.lv_MainFragment);
myListAdapter = new MyProductListAdapter(mActivity.getApplicationContext(), R.layout.custom_product_list_row, myProductList);
lv_MainFragment.setAdapter(myListAdapter);
return view;
}
public void updateMyList() {
myProductList = BaseActivity.myProductList;
myListAdapter.notifyDataSetChanged();
}
}
MyProductListAdapter
public class MyProductListAdapter extends ArrayAdapter<MyProduct> {
private Context mContext;
private ArrayList<MyProduct> list;
AppCompatButton acb_DecreaseAmount_productListRow, acb_IncreaseAmount_productListRow;
public MyProductListAdapter(Context context, int resource, ArrayList<MyProduct> objects) {
super(context, resource, objects);
this.mContext = context;
this.list = objects;
}
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_product_list_row, parent, false);
tv_ProductAmount_productListRow = view.findViewById(R.id.tv_ProductAmount_productListRow);
acb_DecreaseAmount_productListRow = view.findViewById(R.id.acb_DecreaseAmount_productListRow);
acb_IncreaseAmount_productListRow = view.findViewById(R.id.acb_IncreaseAmount_productListRow);
tv_ProductAmount_productListRow.setText(String.valueOf(list.get(position).getAmount()));
acb_IncreaseAmount_productListRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double productPrice = list.get(position).getPrice();
int productAmount = list.get(position).getAmount();
productAmount++;
list.get(position).setAmount(productAmount);
Toast.makeText(mContext, String.valueOf(productAmount), Toast.LENGTH_SHORT).show();
tv_ProductAmount_productListRow.setText(String.valueOf(list.get(position).getAmount()));
}
});
acb_DecreaseAmount_productListRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int productAmount = list.get(position).getAmount();
if (productAmount > 1) {
double productPrice = list.get(position).getPrice();
productAmount--;
list.get(position).setAmount(productAmount);
Toast.makeText(mContext, String.valueOf(productAmount), Toast.LENGTH_SHORT).show();
tv_ProductAmount_productListRow.setText(String.valueOf(list.get(position).getAmount()));
}
}
});
}
return view;
}
}

Hej Metehan,
your use case sounds perfect for a RecyclerView with a ListAdapter. You just submit a new list of products to the adapter and it will handle the updating and notifying for you.

Related

Passing events from DialogFragment back to RecyclerView Adapter

My fragment has a Recycler View. Therefore I have a RecyclerView Adapter too. From this Adapter, I am opening an AlertDialog. When I click OK, I need to pass the onclick event from my DialogFragment back to my RecyclerView Adapter.
Currently, I am doing it like here, but this passes the event back to the activity and not to the RecyclerView Adapter.
public class FreshwaterRecyclerViewAdapter extends RecyclerView.Adapter<FreshwaterRecyclerViewAdapter.ViewHolder> implements BiotopeDialogFragment.NoticeDialogListener {
private List<Biotope> data;
private LayoutInflater layoutInflater;
FreshwaterRecyclerViewAdapter(Context context, List<Biotope> data) {
this.layoutInflater = LayoutInflater.from(context);
this.data = data;
}
//The dialog fragment receives a reference to this Activity through the
//Fragment.onAttach() callback, which it uses to call the following methods
//defined by the NoticeDialogFragment.NoticeDialogListener interface
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
notifyItemInserted(getItemCount()-1);
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == R.layout.biotope_cardview){
itemView = layoutInflater.inflate(R.layout.biotope_cardview, parent, false);
} else {
itemView = layoutInflater.inflate(R.layout.biotope_add_button, parent, false);
}
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
if (position == data.size()) {
holder.imageButtonAddBiotope.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = ((AppCompatActivity) layoutInflater.getContext()).getSupportFragmentManager();
DialogFragment dialog = new BiotopeDialogFragment();
dialog.show(fragmentManager, "NoticeDialogFragment");
}
});
} else {
holder.textViewBiotopeTitle.setText(getItem(position).name);
Picasso.get().load(Uri.parse(getItem(position).imageUri)).into(holder.imageViewBiotope);
LastValuesRecyclerViewAdapter recyclerAdapter = new LastValuesRecyclerViewAdapter(layoutInflater.getContext(), getData());
holder.recyclerViewLastValues.setLayoutManager(new LinearLayoutManager(layoutInflater.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerViewLastValues.setAdapter(recyclerAdapter);
}
}
//total number of rows
#Override
public int getItemCount() {
return data.size() + 1; //+1 for the add button
}
#Override
public int getItemViewType(int position) {
return (position == data.size()) ? R.layout.biotope_add_button : R.layout.biotope_cardview;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textViewBiotopeTitle;
private ImageView imageViewBiotope;
private RecyclerView recyclerViewLastValues;
private ImageButton imageButtonAddBiotope;
public ViewHolder(View view) {
super(view);
textViewBiotopeTitle = (TextView) view.findViewById(R.id.textViewBiotopeTitle);
imageViewBiotope = (ImageView) view.findViewById(R.id.imageViewBiotopeCardview);
recyclerViewLastValues = (RecyclerView) view.findViewById(R.id.recyclerViewLastValues);
imageButtonAddBiotope = (ImageButton) view.findViewById(R.id.imageButtonAddBiotope);
}
}
Biotope getItem(int id) {
return data.get(id);
}
private List<String> getData() {
List<String> data = new ArrayList<>();
data.add("PO4");
data.add("NO3");
return data;
}
}
This is my dialog.
public class BiotopeDialogFragment extends DialogFragment {
private NoticeDialogListener listener;
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
//Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
//Verify that the host activity implements the callback interface
try {
//Instantiate the NoticeDialogListener so we can send events to the host
listener = (NoticeDialogListener) context;
} catch (ClassCastException e) {
//The activity doesn't implement the interface, throw exception
throw new ClassCastException("FreshwaterRecyclerViewAdapter must implement NoticeDialogListener | Context: " + context.toString());
}
}
public static final String TAG = "biotope_dialog_fragment";
private ActivityResultLauncher<Intent> activityResultLauncher;
private Uri imageUri = null;
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext());
LayoutInflater inflater = requireActivity().getLayoutInflater();
//Inflate and set the layout for the dialog
//Pass null as the parent view because its going in the dialog layout
View view = inflater.inflate(R.layout.fragment_dialog_biotope, null, false);
builder.setView(view);
View colorPickerPreviewView = view.findViewById(R.id.colorPickerPreviewView);
ColorPickerView colorPickerView = view.findViewById(R.id.colorPickerView);
ImageView imageViewBiotope = view.findViewById(R.id.imageViewBiotopePreview);
TextInputEditText textFieldBiotopeName = view.findViewById(R.id.textFieldBiotopeName);
builder.setTitle("New biotope")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
BiotopeDatabase database = BiotopeDatabase.getDbInstance(requireContext().getApplicationContext());
Biotope biotope = new Biotope();
if (textFieldBiotopeName.getText() != null) {
biotope.name = textFieldBiotopeName.getText().toString();
} else {
biotope.name = "";
}
if (imageUri != null) {
biotope.imageUri = imageUri.toString();
} else {
biotope.imageUri = "";
}
biotope.color = colorPickerView.getColor();
database.biotopeDao().insertAll(biotope);
//Send the positive button event back to the host activity
listener.onDialogPositiveClick(BiotopeDialogFragment.this);
}
})
.setNegativeButton("noke", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Send the negative button event back to the host activity
listener.onDialogNegativeClick(BiotopeDialogFragment.this);
Objects.requireNonNull(BiotopeDialogFragment.this.getDialog()).cancel();
}
});
return builder.create();
}
public static BiotopeDialogFragment display(FragmentManager fragmentManager) {
BiotopeDialogFragment fragment = new BiotopeDialogFragment();
fragment.show(fragmentManager, TAG);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
This is my fragment building the RecyclerView. Alternatively, I can pass the event back to the fragment if it is not possible to pass it to the adapter.
public class BiotopesFragment extends Fragment {
private FreshwaterRecyclerViewAdapter recyclerAdapter;
public static BiotopesFragment newInstance(String param1, String param2) {
BiotopesFragment fragment = new BiotopesFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_biotopes, container, false);
RecyclerView recyclerViewFreshwater = (RecyclerView) root.findViewById(R.id.recyclerViewFreshwater);
recyclerAdapter = new FreshwaterRecyclerViewAdapter(getContext(), getData());
recyclerViewFreshwater.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
recyclerViewFreshwater.setAdapter(recyclerAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
recyclerViewFreshwater.addItemDecoration(dividerItemDecoration);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.START | ItemTouchHelper.END, 0) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
Collections.swap(getData(), fromPosition, toPosition);
recyclerView.getAdapter().notifyItemMoved(fromPosition, toPosition);
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
});
itemTouchHelper.attachToRecyclerView(recyclerViewFreshwater);
return root;
}
private List<Biotope> getData() {
BiotopeDatabase database = BiotopeDatabase.getDbInstance(requireContext().getApplicationContext());
BiotopeDao biotopeDao = database.biotopeDao();
return biotopeDao.getAll();
}
}
Ideal Way to do this to create all UI component in Fragment not in the adapter . Create an interface to handle events in the fragment and provide callback to the fragment from adapter. now your Fragment should create all the UI component .
Now coming to the
How to provide callback from dialog fragment to Fragment.
You can use setTargetFragment but its deprecated . Now you can use setFragmentResultListener instead of setTargetFragment(), its the safest way i think. Once you get the result back in fragment you can call any method of your adapter.

Cannot cast onItem clicklistener inside Fragment Class form Adapter Class

I have an Fragment with recycleview where I populate it with json items from internet.
It load fine and Next step I want to is open new Activity when any row is clicked. It works in activity, thus I modified the same code for fragment but for fragment it throws exception in line
mExampleAdapter.setOnItemClickListener(getActivity());
with errror setOnItemClickListener of refrence adatper cannot be applied to Fragment activty and thus when I change line to
(ExampleAdapter.OnItemClickListener)
and when i build and run . Then app crashes with error that Mainactivity which holds framgnet cannot be cast in to .ExampleAdapter$OnItemClickListener
Here is my whole Fragment class
public class Mynotes extends Fragment implements ExampleAdapter.OnItemClickListener{
public static final String YTD_LINK = "link";
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList<ExampleItem> mExampleList;
private RequestQueue mRequestQueue;
String url="https://api.myjson.com/bins/16mecx";
public Mynotes() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_jsonfeed, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new MyDividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL, 36));
mExampleList = new ArrayList<>();
mExampleAdapter = new ExampleAdapter(getActivity(), mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener((ExampleAdapter.OnItemClickListener) getActivity());
parseJSON();
return view;
}
private void parseJSON() {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
myProgressBar.setVisibility(View.GONE);
try {
JSONArray jsonArray = response.getJSONArray("hits");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String videoTitle = hit.getString("title");
String link = hit.getString("link");
mExampleList.add(new ExampleItem(videoTitle, link));
mExampleAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
#Override
public void onItemClick(int position) {
Intent intent = new Intent(getActivity(), NewActiviyt.class);
ExampleItem clickedItem = mExampleList.get(position);
intent.putExtra(YTD_LINK, clickedItem.getmLink());
startActivity(intent);
}
#Override
public void onRefresh() {
}
}
and my Adapter Class is
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private Context mContext;
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public ExampleAdapter(Context context, ArrayList<ExampleItem> exampleList) {
mContext = context;
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.example_item, parent, false);
return new ExampleViewHolder(v);
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
String title = currentItem.getTitle();
// int likeCount = currentItem.getLikeCount();
// String imageUrl = currentItem.getImageUrl();
holder.mTextViewCreator.setText(title);
// holder.mTextViewLikes.setText("Likes: " + likeCount);
// Glide.with(mContext).load(imageUrl).apply(RequestOptions.circleCropTransform()).into(holder.mImageView);
}
#Override
public int getItemCount() {
return mExampleList.size();
}
public class ExampleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mTextViewCreator;
// public TextView mTextViewLikes;
// public ImageView mImageView;
public ExampleViewHolder(View itemView) {
super(itemView);
// mTextViewLikes = itemView.findViewById(R.id.text_view_likes);
// mImageView = itemView.findViewById(R.id.image_view);
mTextViewCreator = itemView.findViewById(R.id.text_title);
}
#Override
public void onClick(View view) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
}
}
Thanks in advance.
replace getActivity with getContext when you work in fragment,
You can read more here
What is different between getContext and getActivity from Fragment in support library?
Your activity should implement ExampleAdapter.OnItemClickListener

Fragment "overwrites" another fragment when first selected

I have Bottom Navigation with which I switch between 3 fragments: "ConnectFragment", "DashboardFragment" and "ChatFragment".
Switching from Connect to Chat and vice versa works OK, but when I select Dashboard it causes a bug that makes Dashboard appear when selecting Chat in navigation, what is causing this?
All 3 fragments have identical functionality and layout, so i assume the problem lays in MainActivity.
MainActivity:
public class MainActivity extends AppCompatActivity implements DashboardFragment.FragmentDashListener, ChatFragment.FragmentChatListener, ConnectFragment.FragmentConnListener {
FragmentManager fm = getSupportFragmentManager();
Fragment active;
Fragment FragmentConnect = new ConnectFragment();
Fragment FragmentDashboard = new DashboardFragment();
Fragment FragmentChat = new ChatFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
fm.beginTransaction().add(R.id.fragment_container, FragmentChat).hide(FragmentChat).commit(); //Ustvari vse 3 fragmente, skrije 2 da se nena vedno znova kreirajo
fm.beginTransaction().add(R.id.fragment_container, FragmentDashboard).hide(FragmentDashboard).commit();
fm.beginTransaction().add(R.id.fragment_container, FragmentConnect).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
active = FragmentConnect;
switch (menuItem.getItemId()) {
case R.id.nav_connect:
fm.beginTransaction().hide(active).show(FragmentConnect).commit();
active = FragmentConnect;
return true;
case R.id.nav_dashboard:
fm.beginTransaction().hide(active).show(FragmentDashboard).commit();
active = FragmentDashboard;
return true;
case R.id.nav_send:
fm.beginTransaction().hide(active).show(FragmentChat).commit();
active = FragmentChat;
return true;
}
return false;
}
};
#Override
public void onInputChatSent(CharSequence input) {
ConnectFragment.updateEditText(input);
}
#Override
public void onInputConnSent(CharSequence input) {
DashboardFragment.updateEditText(input);
}
#Override
public void onInputDashSent(CharSequence input) {
ChatFragment.updateEditText(input);
}
}
Fragments:
All 3 fragments have identical code, below are Dashboard and Chat.
public class DashboardFragment extends Fragment {
private FragmentDashListener listener;
private static EditText editText;
private Button ButtonOk;
public interface FragmentDashListener{
void onInputDashSent (CharSequence input);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dashboard, container, false);
editText = v.findViewById(R.id.edit_text);
ButtonOk = v.findViewById(R.id.Button_Ok);
ButtonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputDashSent(input);
}
});
return v;
}
public static void updateEditText(CharSequence newText){
editText.setText(newText);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if(context instanceof FragmentDashListener){
listener = (FragmentDashListener) context;
} else {
throw new RuntimeException(context.toString()+"must implement FragmentDashListener");
}
}
}
public class ChatFragment extends Fragment {
private FragmentChatListener listener;
private static EditText editText;
private Button ButtonOk;
public interface FragmentChatListener{
void onInputChatSent (CharSequence input);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_chat, container, false);
editText = v.findViewById(R.id.edit_text);
ButtonOk = v.findViewById(R.id.Button_Ok);
ButtonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputChatSent(input);
}
});
return v;
}
public static void updateEditText(CharSequence newText){
editText.setText(newText);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if(context instanceof FragmentChatListener){
listener = (FragmentChatListener) context;
} else {
throw new RuntimeException(context.toString()+"must implement FragmentChatListener");
}
}
}
Remove the first line
active = FragmentConnect;
from onNavigationItemSelected method. This will fix the issue
or modify it as below
if(active == null) {
active = FragmentConnect;
}

problems passing data from a recycleView to another

I'm a beginner on Android development and I was trying to make a simple app that has a recycleView inside a fragment using cardViews and when you click the like button inside of any of the items inside the list, the app then should send this item to another fragment called FavoriteFragment (which also contains recycleView) and display it here.
I tried to use an interface to do this, but whenever I click one of the like buttons, my activity does not receive the information (I tried to Log.d a message inside the method and is not being displayed)
here is the code for each of this:
My interface, what I am trying to use to pass the data from countries fragment-> adapter -> viewholder -> activity -> favorite fragment
public interface InterfaceListItemClickListener {
void listItemClickAction(ArrayList properties);
}
my CountriesFragment, which is the fragment that holds the first RecycleView:
public class CountriesFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
ArrayList<RecycleViewItem> listCountries = new ArrayList<>();
String names[] = {"Thailand", "Venezuela", "Sweden"};
int images[] = {R.drawable.thailand, R.drawable.venezuela, R.drawable.sweden};
RecyclerView myRecyclerView;
InterfaceListItemClickListener sender;
public CountriesFragment() {
// Required empty public constructor
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sender = (InterfaceListItemClickListener) getActivity();
}
public static CountriesFragment newInstance(String param1, String param2) {
CountriesFragment fragment = new CountriesFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
initializeList();
}
public void initializeList(){
listCountries.clear();
for(int i = 0; i < names.length; i++){
RecycleViewItem item = new RecycleViewItem();
item.setCardName(names[i]);
item.setImageResourceID(images[i]);
item.setIsFav(0);
item.setIsTurned(0);
listCountries.add(item);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_countries, container, false);
myRecyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
myRecyclerView.setHasFixedSize(true);
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
myLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
ListsForFragments.populateCountriesList();
if (ListsForFragments.countriesList.size() > 0 & myRecyclerView != null) {
myRecyclerView.setAdapter(new MyAdapter(ListsForFragments.countriesList, sender));
}
myRecyclerView.setLayoutManager(myLayoutManager);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
My Adapter class:
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
private ArrayList<RecycleViewItem> list = new ArrayList<RecycleViewItem>();
InterfaceListItemClickListener sender = null;
public MyAdapter(ArrayList<RecycleViewItem> list, InterfaceListItemClickListener sender) {
this.list = list;
this.sender = sender;
}
public MyAdapter(ArrayList<RecycleViewItem> list) {
this.list = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_items, parent, false);
MyViewHolder holder = new MyViewHolder(view, sender);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.titleTextView.setText(list.get(position).getCardName());
holder.coverImageView.setImageResource(list.get(position).getImageResourceID());
holder.coverImageView.setTag(list.get(position).getImageResourceID());
}
#Override
public int getItemCount() {
return list.size();
}
}
My viewHolder class:
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView titleTextView;
public ImageView coverImageView;
public ImageView likeImageView;
public ImageView shareImageView;
public ImageView favoriteImageView;
public TextView favoriteTextView;
private ArrayList goesToFavourites = new ArrayList();
public MyViewHolder(View itemView, final InterfaceListItemClickListener sender) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.titleTextView);
coverImageView = (ImageView) itemView.findViewById(R.id.coverImageView);
likeImageView = (ImageView) itemView.findViewById(R.id.likeImageView);
shareImageView = (ImageView) itemView.findViewById(R.id.shareImageView);
favoriteImageView = (ImageView) itemView.findViewById(R.id.imageView_favorite);
favoriteTextView = (TextView) itemView.findViewById(R.id.textView_name_favorite);
if (likeImageView != null) {
likeImageView.setTag(R.drawable.ic_like);
likeImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int id = (int) likeImageView.getTag();
if (id == R.drawable.ic_like) {
likeImageView.setTag(R.drawable.ic_liked);
likeImageView.setImageResource(R.drawable.ic_liked);
if(sender != null) {
getGoesToFavourites().add(coverImageView);
getGoesToFavourites().add(likeImageView);
sender.listItemClickAction(getGoesToFavourites());
}
} else {
likeImageView.setTag(R.drawable.ic_like);
likeImageView.setImageResource(R.drawable.ic_like);
}
}
});
}
}
public ArrayList getGoesToFavourites() {
return goesToFavourites;
}
public void setGoesToFavourites(ArrayList goesToFavourites) {
this.goesToFavourites = goesToFavourites;
}
}
My favoriteFragment class, the one receiving the information:
public class FavoriteFragment extends Fragment {
private OnFragmentInteractionListener mListener;
RecyclerView myRecyclerView;
ArrayList receiver = new ArrayList();
public FavoriteFragment() {
}
public void receiveData(ArrayList receiver){
this.receiver = receiver;
}
public static FavoriteFragment newInstance() {
FavoriteFragment fragment = new FavoriteFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
myRecyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
myRecyclerView.setHasFixedSize(true);
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
myLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (receiver != null & myRecyclerView != null) {
myRecyclerView.setAdapter(new MyAdapter(receiver));
}
myRecyclerView.setLayoutManager(myLayoutManager);
return view; }
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
finally, my MainActivity, the bridge:
public class MainActivity extends AppCompatActivity implements
HostFragment.OnFragmentInteractionListener,
CountriesFragment.OnFragmentInteractionListener,
CitiesFragment.OnFragmentInteractionListener,
PlacesFragment.OnFragmentInteractionListener,
FavoriteFragment.OnFragmentInteractionListener,
ViewPagerFragment.OnFragmentInteractionListener,
InterfaceListItemClickListener{
FragmentManager fm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();
//if this is the first time we are running the app
if(savedInstanceState == null){
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.content, new ViewPagerFragment());
transaction.commit();
}
}
#Override
public void onFragmentInteraction(Uri uri) {
}
#Override
public void listItemClickAction(ArrayList list) {
FavoriteFragment favorite = (FavoriteFragment)
fm.findFragmentById(R.id.favorite);
favorite.receiveData(list);
Log.d("INTERFACE", "Data received!" + list);
}
}
If any extra information needed let me know and I'll provide.
EDIT: so trying to debug, I found out that the InterfaceListItemClickListener that MyViewHolder is a null, but not sure why...
EDIT 2: so I changed some things, inside My CountriesFragment:
I deleted the OnActivityCreated method and initialized the interface rather inside the onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeList();
sender = (InterfaceListItemClickListener) getActivity();
}
Now with this change, I was able to know that the findFragmentById inside my MainActivity is returning me a null. now, the way I am displaying this fragment is by using a ViewPager fragment which holds both fragments. this looks like this:
public class ViewPagerFragment extends Fragment {
private OnFragmentInteractionListener mListener;
public static ViewPager viewPager;
public ViewPagerFragment() {
// Required empty public constructor
}
public static ViewPagerFragment newInstance() {
ViewPagerFragment fragment = new ViewPagerFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(null);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_pager, container, false);
CustomAdapter adapter = new CustomAdapter(getChildFragmentManager()); //getChildFragmentManager
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
return view;
}
public class CustomAdapter extends FragmentPagerAdapter {
public CustomAdapter(FragmentManager fm){
super(fm);
}
//position tells the program what fragment we are currently on/displaying
public Fragment getItem(int position){
switch (position){ //notice we don't use breaks on each case, due to the return statement on each.
case 0:return CountriesFragment.newInstance();
case 1: return FavoriteFragment.newInstance();
default: return FavoriteFragment.newInstance();
}
}
public int getCount(){
return 2;
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
How would I proceed in this case to finish passing the information?
OnCreateView() is called before OnActivityCreate() thats why your sender is null.Moreover,you must avoid setting or creating any objects in the OnCreateView(). Your fragment should be like this
public class CountriesFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
ArrayList<RecycleViewItem> listCountries = new ArrayList<>();
String names[] = {"Thailand", "Venezuela", "Sweden"};
int images[] = {R.drawable.thailand, R.drawable.venezuela, R.drawable.sweden};
RecyclerView myRecyclerView;
InterfaceListItemClickListener sender;
public CountriesFragment() {
// Required empty public constructor
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sender = (InterfaceListItemClickListener) getActivity();
myRecyclerView.setHasFixedSize(true);
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
myLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
ListsForFragments.populateCountriesList();
if (ListsForFragments.countriesList.size() > 0 & myRecyclerView != null) {
myRecyclerView.setAdapter(new MyAdapter(ListsForFragments.countriesList, sender));
}
myRecyclerView.setLayoutManager(myLayoutManager);
}
public static CountriesFragment newInstance(String param1, String param2) {
CountriesFragment fragment = new CountriesFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
initializeList();
}
public void initializeList(){
listCountries.clear();
for(int i = 0; i < names.length; i++){
RecycleViewItem item = new RecycleViewItem();
item.setCardName(names[i]);
item.setImageResourceID(images[i]);
item.setIsFav(0);
item.setIsTurned(0);
listCountries.add(item);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_countries, container, false);
myRecyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}

View Pager Android

I'm trying to implement a View Pager from : http://developer.android.com/training/animation/screen-slide.html
But I seem to have a problem when it comes to creating the page number. When debugging the downloaded zip from the previous mentioned website, first method called is create(int pageNumber) and afterwards onCreate() where you're getting the page number. In my case, it's the other way around, therefore I get a null pointer exception.
Here is my current implementation of the View Pager:
public class SingleCheckInDisplay extends android.support.v4.app.Fragment {
private Checkin data;
private FragmentManager fragmentManager;
private List<CheckinUser> enlooped;
private TextView checkInLocation;
private TextView checkInDescription;
private TextView checkInTime;
private Button cancelBtn;
private ImageButton singleCheckInEnloopBtn;
private ImageButton singleCheckInCancelBtn;
private HorizontalListView enloopedFriends;
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static SingleCheckInDisplay create(int pageNumber) {
SingleCheckInDisplay fragment = new SingleCheckInDisplay();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public SingleCheckInDisplay(Checkin data) {
this.data = data;
}
public SingleCheckInDisplay() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = 2;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_single_check_in_display, container, false);
enlooped = data.getCheckinUsers();
fragmentManager = getActivity().getSupportFragmentManager();
ImageView main_pic = (ImageView) v.findViewById(R.id.main_pic);
checkInLocation = (TextView) v.findViewById(R.id.single_check_in_location);
checkInDescription = (TextView) v.findViewById(R.id.single_check_in_desc);
checkInTime = (TextView) v.findViewById(R.id.single_check_in_time_text);
checkInTime.setText(data.getCheckinDate().toString());
enloopedFriends = (HorizontalListView) v.findViewById(R.id.sinlge_check_in_enlooped_list);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.sm_profile)
.showImageForEmptyUri(R.drawable.sm_profile)
.showImageOnFail(R.drawable.sm_profile)
.cacheOnDisk(true)
.cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.considerExifParams(true)
.displayer(new SimpleBitmapDisplayer())
.build();
ImageLoader.getInstance().displayImage(data.getImages(), main_pic, options);
checkInDescription.setText(data.getDescription());
checkInLocation.setText(data.getPlaceAddressAndName());
checkInLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Set a bundle with place ID, the rest will be obtained by calling Graph API
Bundle args = new Bundle();
args.putString("placeID", data.getPlaceId());
SingleCheckInPlace scp = new SingleCheckInPlace();
scp.setArguments(args);
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.container, scp).addToBackStack(null).commit();
}
});
SingleCheckInAdapter adapter = new SingleCheckInAdapter(getActivity(), enlooped);
enloopedFriends.setAdapter(adapter);
cancelBtn = (Button) v.findViewById(R.id.single_ck_display_cancel_button);
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MapsFilterFragment firstFragment = new MapsFilterFragment();
fragmentManager.beginTransaction().add(R.id.container, firstFragment).commit();
}
});
singleCheckInEnloopBtn = (ImageButton) v.findViewById(R.id.single_check_in_enloop_btn);
singleCheckInEnloopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Enlooped !", Toast.LENGTH_SHORT).show();
fragmentManager.popBackStack();
}
});
singleCheckInCancelBtn = (ImageButton) v.findViewById(R.id.single_check_in_no_btn);
singleCheckInCancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Maybe next time", Toast.LENGTH_SHORT).show();
goBack();
}
});
return v;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
private void goBack() {
fragmentManager.popBackStack();
}
public int getPageNumber() {
return mPageNumber;
}
}
And here is the miplementation of the Pager itself:
public class ScreenSlideFragment extends android.support.v4.app.Fragment {
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 11;
public ScreenSlideFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_screen_slide, container, false);
mPager = (ViewPager) view.findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
return view;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return SingleCheckInDisplay.create(position);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
DO like this
ViewPager mviewPager=(ViewPage)findViewById(R.id.urcontainer);
mviewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void OnPageSelected(int position) {
int page_number=position; //this will give u the current page number
}
});
`

Categories

Resources