In my Android app I have a recyclerview which contains a list of items. Each item has two textview and one button. How can I code that button such that when a user click on that button that item adds to Listview of Cartactivity(Which is another activity).
I am using JSON and MYSQL to populate the recyclerview list.
A really simple solution will be to create a singleton class which will contain all the items that you have added. For example, you can create a class called CartItems which will have a list of CartItem. In your CartActivity you can access the object of CartItems and do whatever you want with it.
Just some high-level code
public class CartItems {
static CartItems cartItems;
List<CartItem> items;
public static CartItems getInstance() {
//Return your singleton object
}
public void addItem(CartItem item) {
//Add item to your list
}
//Add other methods for your use
}
public class CartItem {
//Data members
//Member functions
}
In your CartActivity you can call CartItems.getInstance() to get all the added items and proceed with whatever you want.
Related
I want to develop a screen that represents a store and in it you can find a list of products (presented by a recycleview) where you can find the price of each item and select your desired quantity.
when the user update the quantity of the product the price changes too.
I have a textview below the recycleview which display the total sum of prices of all selected products.
When the user want to change his required quantity of a specific product, it is simple to update the price represented in the item but how to update in the same time the one (the total one) in the main screen ?
Use Interface to listen to the change in the recycler view item and update the value on the user select the quantity.
Create a interface class
public interface UpdateListener {
void onQuantityUpdated();
}
In your adapter class
private Context context;
private UpdateListener deleteListener;
public YourRVAdapter(Context context, List<ConsumedMaterialModel> list,
UpdateListener updateListener) {
this.context = context;
this.list = list;
this.deleteListener = deleteListener;
}
on user select quantity inside adapter
updateListener.onQuantityUpdated();
Now implement this interface in your Activity
public class MainActivity extends AppCompatActivity implements UpdateListener
#Override
public void onQuantityUpdated() {
//update your textview by calculating amount here
}
I want to create an ArrayList, add values to it from different other activities and then show it as a listview in some another activity. I know how to create a list view but I am don't know how to create an ArrayList globally and add values to it via other activities. It would be really helpful if you can provide me with a sample code for the full problem. I don't want to go for any advanced method, as it is for a beginner college project and I want to explain my implementation method properly.
Basically, it's a shopping app where I am storing the name of bought products in an ArrayList on the click of a button. And then showing the final list in listview on the cart activity.
First, create a class to hold the ArrayList globally:
public class ListHolder {
public static List<String> list = new ArrayList<>();
}
Use the array list in any activity you want:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
... // Init views
// Obtain the list
List<String> list = ListHolder.list;
// Maybe add something to it
list.add("sample");
// Set the list to your adapter to render it by your ListView
yourAdapter.setData(list);
yourAdapter.notifyDataSetChanged();
}
}
how to store only selected item list data in recycler view from other activity list.
i used this code -- successfully select data but don't know how to add only selected data items in new activity recycler view -
i used this code snippet ite working fine-
//
https://en.proft.me/2018/03/3/multi-selection-recyclerview/
StringBuilder stringBuilder = new StringBuilder();
for (ExcercisesSelectedModel.DataBean data : getList()) {
if (selectedIds.contains(data.getId()))
stringBuilder.append("\n").append(data.getName());
Change
public interface OnClickAction {
public void onClickAction();
}
to
public interface OnClickAction {
public void onClickAction(Item item);
}
In adapter class, on item click
receiver.onClickAction(item);
In Activity class
private List<Item> selectedItem = new ArrayList()
public void onClickAction(Item item) {
selectedItem.add(item);
}
Now use this selected item list in new activity.
Edit: Don't forget to use or rename the interface to Callback, something along the lines of ActivityCallback or OnClickCallback.
This is for the sake of naming conventions.
A simple flag isSelected in parent list will save your day.
So whenever user select/unselect the item just change the value of isSelected flag to true or false.
Now you have a final list from which you can easily identify selected items, simple store in separated list named selectedItemList.
At last use the selectedItemList and fill your Recycleview. Hope it make sense.
I have an Android MVP project. I want to keep out any Android references out of the presenter. This way, I can keep UI separated in the Activity/View.
There is a ListView in the Activity which uses a custom ArrayList in the adapter (MyAdapter). This uses MyModel objects to populate the ListView with data.
Now, I'm trying to initialize the adapter and the ListView.
By doing that on the activity I would end up with something like
`MyAdapter adapter = new MyAdapter<MyModel>(this, R.layout.list_item, items);`
The problem with this is that the Activity now have access to the Model and has a reference to an ArrayList of items which I wanted to keep only in the presenter and manage it from there.
I can't move setup the adapter on the Presenter because I would have to share the context from the Activity to the presenter, setup the adapter and pass it back to the Activity. The problem with this is that the presenter now depends on an Android context object (There shouldn't be any Android code in the Presenter part of an MVP Android project).
So the question is what do I do in this case? Where and how do I handle setting up the ArrayAdapter?
You can keep a list of items in the Presenter and then, send it to the Activity when you need to set up the adapter.
I think you can do:
Contract:
public interface ViewContract {
void setupContentList(ArrayList<MyModel> list);
}
public interface PresenterContract {
void onViewCreated();
}
Activity:
public class MainActivity extends Activity implements ViewContract {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
mListView = findViewById(R.id.listview);
mPresenter.onViewCreated();
}
public void setupContentList(ArrayList<MyModel> list) {
MyAdapter adapter = new MyAdapter<MyModel>(this, R.layout.list_item, items);
mListView.setAdapter(adapter);
}
}
Presenter:
public class Presenter implements PresenterContract {
public Presenter() {
ArrayList<MyModel> mItems = new ArrayList();
// Add items to the list
}
public void onViewCreated() {
mView.setupContentList(mItems);
}
}
I recommend you to use RecyclerView and not ListView for better performance.
The activity can hold the RecyclerView, you should ask the presenter to fetch the data you want from your model repository, etc.
After the presenter retrieved the data you should notify the Activity about the new data, and set it to the RecyclerAdapter.
Now your activity holds inflates all regarding the view, and the presenter is a hub for data and logic.
In my activity I have one recyclerview and each item view contains buttons. I want to be able to change some UI elements and other things such as an Array of custom objects for the adapter itself in my activity from the recyclerview adapter. Until now I declared the needed views as static but I found out that it's a terrible practice.
Example: I have the following recyclerview that represents a cart filled with a custom viewholder, from an Array of custom "cart_product" objects. (One of this custom oject's proprieties is "quantity" - represented by the spinner). I want to be able to change the object's "quantity" property by changing the spinner's value from the adapter... How could this be done? And when all the products are removed from cart (by swiping & detected from adapter) I want to display a textvie
ScreenShot
You can use callbacks:
In adapter create an interface:
public interface EventHandler {
void handle(int position) // if u need know position. If no, just create method without params
}
Create an private instance of interface in adapter:
public class YourAdapter extends RecyclerView.Adapter<YourHolder> {
private EventHanlder handler;
}
Implement EventHanlder in activity:
public class Mainacitivity extends Activity implements YourAdapter.EventHandler {
//.....
#Override
void handle (int position) {
// TODO do whatever u want
}
}
Add EventHandler to constructor parameters:
public YourAdapter (List<YourObject> data, EventHandler handler) {
//....
this.handler = handler;
}
When you need to change UI call
handler.hanlde(position);
And, finally pass this when initializing adapter
adapter = new YourAdapter (data, this)
If u need something else (not position), just change signature of handle() method