In Home Fragment
userListAdapter = new UserListAdapter(getActivity(), response.getTray(), ?);
RVUserList.setAdapter(userListAdapter);
in Adapter
public UserListAdapter(Context context, ArrayList<TrayModel> list, UserListInterface listInterface) {
this.context = context;
this.trayModelArrayList = list;
this.userListInterface = listInterface;
}
and also implement UserListInterface in fragment
userListAdapter = new UserListAdapter(getActivity(), response.getTray(), new UserListInterface(){
// interface methods
});
RVUserList.setAdapter(userListAdapter);
userListAdapter = new UserListAdapter(getActivity(), response.getTray(), HomeFragment.this::userListClick);
RVUserList.setAdapter(userListAdapter);
Related
I have this on my GroupDescriptionActivity class:
GroupDescriptionViewModel mFavViewModel;
ArrayList<String> mFav;
mFavViewModel = new ViewModelProvider(this).get(GroupDescriptionViewModel.class);
final Observer<ArrayList<String>> favsObserver = new Observer<ArrayList<String>>() {
#Override
public void onChanged(#Nullable final ArrayList<String> updatedList) {
if (mFav == null) {
mFav = updatedList;
ArrayAdapter arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_expandable_list_item_1, mFav);
partecipants.setAdapter(arrayAdapter);
}
}
};
And in my GroupDescriptionViewModel:
public class GroupDescriptionViewModel extends AndroidViewModel {
private static MutableLiveData<ArrayList<String>> mFavs;
public GroupDescriptionViewModel(#NonNull Application application) {
super(application);
}
public static MutableLiveData<ArrayList<String>> getFavs(ArrayList<HashMap> partecipants) {
if (mFavs == null) {
mFavs = new MutableLiveData<>();
}
loadFavs(partecipants);
return mFavs;
}
public static void loadFavs(ArrayList<HashMap> partecipants) {
ArrayList<String> newFavs = new ArrayList<>();
for (HashMap member : partecipants) {
newFavs.add(member.get("mail").toString());
}
mFavs.setValue(newFavs);
}
}
I then have a firestore database change listener in the Source.java file. How can I update my ListView which is in the activity GroupDescription from Source.java?
I tried in Source.java
GroupDescriptionViewModel.getFavs((ArrayList<HashMap>) ((HashMap) arr.get(2)).get("partecipants"));
but didn't work. ((ArrayList<HashMap>) ((HashMap) arr.get(2)).get("partecipants") is my new array for the updates.
So I would like the new data to be entered from source via livedata, and my listview to update in real time
if Source.Java has a listener, why not implement it in the activity GroupDescription? or make an instance of Source.java in your viewModel and set the listener interface there and postValue of your MutableLiveData() inside the listener interface and observe it in your activity? from there you can notify your recyclerView adapter.
Error Log I'm stuck here I've tried many solutions but same error
java.lang.ClassCastException: com.beauty.fashion.style.KidsNavigationDrawerActivity cannot be cast to com.beauty.fashion.style.interfaces.UpdateOrderTotal
at com.beauty.fashion.style.adapters.CheckoutAdapter.<init>(CheckoutAdapter.java:38)
at com.beauty.fashion.style.fragment.OrderConfirmationFragment.onCreateView(OrderConfirmationFragment.java:109)
This is my android code
public CheckoutAdapter(Context context, List<ProductModel> list) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
callback = (UpdateOrderTotal) context; //This is line 38 of CheckoutAdapter
helper = new FashionDbHelper(context);
utils = new CartUtils(context);
}
This is where I'm getting products to cart list and app is crashing
if (utils.getCartList().getValue() != null) {
cartList.addAll(utils.getCartList().getValue());
if (cartList != null && cartList.size() > 0) {
linearEmptyCart.setVisibility(View.GONE);
cvGrandTotal.setVisibility(View.VISIBLE);
adapter = new CheckoutAdapter(getActivity(), cartList); //This is line 109
rvConfirmOrder.setAdapter(adapter);
rvConfirmOrder.setLayoutManager(new LinearLayoutManager(getActivity()));
calculateTotal(cartList);
} else {
linearEmptyCart.setVisibility(View.VISIBLE);
cvGrandTotal.setVisibility(View.GONE);
}
} else {
cvGrandTotal.setVisibility(View.GONE);
linearEmptyCart.setVisibility(View.VISIBLE);
}
You need KidsNavigationDrawerActivity to implement UpdateOrderTotal interface.
Something like this:
public class KidsNavigationDrawerActivity extends AppCompatActivity implements UpdateOrderTotal {
// your Activity code here
}
I've done it by myself after implementing interface to KidsNavigationDrawer activity
I have an custom adapter that is giving me problem. When I add or edit something it updates the listview but when I delete it doesn't.
Code when I remove something:
private ArrayList<Teams> m_orders = null;
private TeamsAdapter m_adapter;
private ListView lstv;
...
private void deleteTeam(int indexRemove){
hasKeys.remove(hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()));
Menu.teams.remove(m_orders.get(indexRemove).getTeamName());
m_orders.remove(indexRemove);
m_adapter.notifyDataSetChanged();
}
I tried use a Runnable, but without success.
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
m_adapter.notifyDataSetChanged();
}
};
My onCreate method:
lstv = findViewById(R.id.teamsList);
m_orders = new ArrayList<Teams>();
this.m_adapter = new TeamsAdapter(this, R.layout.row, m_orders);
lstv.setAdapter(this.m_adapter);
You have to remove an item from the ArrayList which is inside the Adapter class. So you need to write your delete function inside the adapter class. In this way will able to delete items and update listview by calling notifyDataSetChanged() method.
You are creating an m_adapter with m_orders dataset, but you are removing item from m_teams dataset, make no sense, they are different instances.
final List<Teams> mTeams = new ArrayList<>();
TeamsAdapter mAdapter;
onCreate() {
mAdapter = new TeamsAdapter(this, R.layout.row, mTeams);
ListView listView = (ListView) findViewById(R.id.teamsList);
listView.setAdapter(mAdapter);
}
addTeams(Collection<Teams> items) {
mTeams.addAll(items);
mAdapter.notifyDataSetChanged();
}
deleteTeam(int index) {
mTeams.remove(index);
mAdapter.notifyDataSetChanged();
}
I have two classes , the first one is for the GUI , where I declared my listview and the adapter , and the setters , to call them from my second class .
public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {
private transient ListView lv;
List<String> mydeviceslist;
ArrayAdapter<String> adapter ;
public void setAdapter(ArrayAdapter<String> adapter) {
this.adapter = adapter;
adapter.notifyDataSetChanged();
}
public void setMydeviceslist(List<String> mydeviceslist) {
this.mydeviceslist = mydeviceslist;
}
#Override
public void onCreateView() {
lv=(ListView) fragment.findViewById("xdevices") ;
mydeviceslist = new ArrayList<String>();
adapter = new ArrayAdapter<String>(fragment.getContext(),android.R.layout.simple_list_item_1,mydeviceslist);
lv.setAdapter(adapter);
In my second class I'll wait an event to receive the list that I want to load it in my listview , then I'll call the list setter to set the new received list and the adapter setter to update it , but it didn't work , nothing was displayed despite I receieved the list of devices in my log .
public class triprincipal extends BCModel {
public List<String> mydevices ;
BCEvent bcEvent;
final ArrayAdapter<String> adapter =guiModifier.getAdapter();
while (isRunning()) {
bcEvent = waitForBCEvent();
if (bcEvent.getID() == checkevent) {
mydevices = bcCommandSenderPlugin.getDevicesNames(); // here I get a list of my devices
Log.i("devices", mydevices.toString());
guiModifier.getFragment().getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
guiModifier.setMydeviceslist(mydevices);
guiModifier.setAdapter(adapter);
}
} );
In setMydeviceslist() do it like:
this.mydeviceslist.addAll(mydeviceslist);
adapter.notifyDataSetChanged();
Hope it will help you out.
adapter.notifyDataSetChanged() will not work in this case, as the value of the reference you have passed to the adapter doesn't actually change.
You will need to create a new Adapter and set it to the ListView to make it work. Change your setAdapter() to this :
public void setAdapter() {
this.adapter = new ArrayAdapter<String>(fragment.getContext(), android.R.layout.simple_list_item_1, mydeviceslist);
lv.setAdapter(adapter);
}
try to update the list in same fragment/activity and after update call notifyDataSetChanged() both in same activity/fragment ....donot set adapter on list repetedly......hope it helps
I used a constructor like this:
public class CustomList extends ArrayAdapter<String> {
private String[] ids;
private String[] names;
private String[] urls;
private String[] fileTypes;
private String[] date_times;
private Activity context;
public CustomList(Activity context, String[] ids, String[] names, String[] str_url,String[] fileType ,String[] dateTime){
super(context, R.layout.list_view_layout, ids);
this.context = context;
this.ids = ids;
this.names = names;
this.urls = str_url;
this.fileTypes = fileType;
this.date_times = dateTime;
}
And able to use this class from main activity like this
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.names,ParseJSON.url,ParseJSON.file_type,ParseJSON.uploaded_at);
But whenever I am trying to pass the first parameter of constructor "this" error message showing. So what will be the first parameter whenever using that calass from a fragment
This is actually what I have done in a fragment
if (arrayListPollingSite.size() > 0) {
int pollingSiteCount = arrayListPollingSite.size();
pollingSiteNameArray = new String[pollingSiteCount];
HashMap<String, String> pollDataMap = new HashMap<String, String>();
for (int i = 0; i < pollingSiteCount; i++) {
pollDataMap = arrayListPollingSite.get(i);
pollingSiteNameArray[i] = pollDataMap.get(i);
// this line showing error
CustomList c1=new CustomList(getActivity(),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID));
}
// listView.setAdapter(cl);
}
'this' means always the current context.
the fragment's context and the activity's context are different.
so you should give the activity's context to the constructor: try 'getActivity()'
You can call this from the Activity class.
But in a fragment you must call the activity that contain this fragment with getActivity(), like this:
CustomList cl = new CustomList(getActivity(), ParseJSON.ids,
ParseJSON.names,ParseJSON.url,ParseJSON.file_type,
ParseJSON.uploaded_at);
Harunduet said:
I have tried according to the following way;
pollingSiteNameArray = new String[pollingSiteCount];
CustomList c1=new CustomList(getActivity(),pollingSiteNameArray[1],pollingSiteNameArray[2],pollingSiteNameArray[3],pollingSiteNameArray[4],pollingSiteNameArray[5]);
it show error
Harunduet said:
if (arrayListPollingSite.size() > 0) {
int pollingSiteCount = arrayListPollingSite.size();
pollingSiteNameArray = new String[pollingSiteCount];
HashMap<String, String> pollDataMap = new HashMap<String, String>();
for (int i = 0; i < pollingSiteCount; i++) {
pollDataMap = arrayListPollingSite.get(i);
pollingSiteNameArray[i] = pollDataMap.get(i);
// this line showing error
CustomList c1=new CustomList(getActivity(),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID),pollDataMap.get(Constants.KEY_COUNTY_ID));
}
// listView.setAdapter(cl);
}
Where are you exe this code? Try to move to onActivityCreated method, in your fragment.
try this
CustomList cl = new CustomList(getActivity(), ParseJSON.ids,ParseJSON.names,ParseJSON.url,ParseJSON.file_type,ParseJSON.uploaded_at);