I have the following JSON which I'm parsing in GSON and Retrofit. I want to display the values of the same id in one TextView. What's happening right now is that all the values are added to the array and they are being displayed in separate TextViews. I want to show all values which have the same id to be displayed in one TextView. For eg. id: 240 ab values should be in one TextView. Currently, all ab values are in separate TextView.
This is how the data is currently displaying:
This is how I want the data to be:
JSON::
{
"abc": {
"1": {
"1": {
"ab": "some content",
"id": "240",
"key": "value"
},
"2": {
"ab": "some content",
"id": "240",
"key": "value"
},
"3": {
"ab": "some content",
"id": "240",
"key": "value"
}
},
"2": {
"1": {
"ab": "more content",
"id": "241",
"key": "value"
},
"2": {
"ab": "more content 1",
"id": "241",
"key": "value"
},
"3": {
"ab": "more content 2",
"id": "241",
"key": "value"
}
}
}
}
POJOContent::
public class POJOContent {
#SerializedName("ab")
public String content;
#SerializedName("id")
public String id;
#SerializedName("key")
public String key;
#Override
public String toString() {
return content;
}
//getters and setters
}
MyContentWrapper::
public class MyContentWrapper {
public Map<Integer, MyMap> abc;
}
MyMap::
public class MyMap extends HashMap<Integer, POJOContent> {
#Override
public POJOContent put(Integer key, POJOContent value) {
if(null==value.getContent() || value.getContent().isBlank()) {
return null;
}
// Added only if content = "ab" is not blank.
return super.put(key, value);
}
}
Callback:
Callback<MyContentWrapper> myCallback = new Callback<MyContentWrapper>() {
#Override
public void onResponse(Call<MyContentWrapper> call, Response<MyContentWrapper> response) {
if (response.isSuccessful()) {
Log.d("Callback", " Message: " + response.raw());
Log.d("Callback", " Message: " + response.body().abc.values());
MyContentWrapper contentWrapper = response.body();
List<POJOContent> pojo = new ArrayList<>();
for (Map.Entry<Integer, MyMap> entry : contentWrapper.abc.entrySet()) {
Integer key = entry.getKey();
MyMap map = entry.getValue();
if (!map.isEmpty()){
Log.d("Callback", " Key: " + key);
Log.d("Callback", " Value: " + map.values());
pojo.addAll(map.values());
}
}
MyContentViewAdapter adapter = new MyContentViewAdapter(pojo);
recyclerView.setAdapter(adapter);
} else {
Log.d("Callback", "Code: " + response.code() + " Message: " + response.message());
}
}
#Override
public void onFailure(Call<MyContentWrapper> call, Throwable t) {
t.printStackTrace();
}
};
RecyclerAdapter::
public class MyContentViewAdapter extends RecyclerView.Adapter<MyContentViewAdapter.ViewHolder> {
private List<POJOContent> data;
private MyClickListener clickListener;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
private LinearLayout itemLayout;
public ViewHolder(View v) {
super(v);
text = (TextView) v.findViewById(R.id.text_content);
}
}
public MyContentViewAdapter(List<POJOContent> data) {
this.data = data;
Log.d("Recyclerview Data", data.toString());
}
#Override
public MyContentViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_content_card, parent, false);
return new MyContentViewAdapter.ViewHolder(v);
}
#Override
public void onBindViewHolder(MyContentViewAdapter.ViewHolder holder, int position) {
POJOContent pojo = data.get(position);
Log.d("Recyclerview", pojo.getContent());
holder.text.setText(pojo.getContent());
holder.itemView.setTag(pojo.getContent());
}
public void setOnItemClickListener(MyClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public int getItemCount() {
return data.size();
}
}
EDIT:
I added nested RecyclerView inside ViewHolder, so the content and value fields will be displayed dynamically. I'm Adding full code of 2 Adapter and
2 ViewHolder classes, 2 xml layouts and screenshot
I'm pretty sure it will run really smoothly with very large list too.
Everything under ID(240,241) is another recyclerView.
The idea is that list's size, for adapter to populate itself, should be as many as the number of distinct ids, so that only that many Viewholders are inflated.
List<List<POJOContent>> listOfPojoLists = new ArrayList<>();
for (Map.Entry<Integer, MyMap> entry : contentWrapper.abc.entrySet()) {
Integer key = entry.getKey();
MyMap map = entry.getValue();
if (!map.isEmpty()){
Log.d("Callback", " Key: " + key);
Log.d("Callback", " Value: " + map.values());
listOfPojoLists.add(new ArrayList<>(map.values()));
}
}
MyContentViewAdapter adapter = new MyContentViewAdapter(listOfPojoLists);
recyclerView.setAdapter(adapter);
MyContentViewAdapter.java
public class MyContentViewAdapter extends RecyclerView.Adapter<MyContentViewAdapter.ViewHolder> {
private List<List<POJOContent>> data;
private MyClickListener clickListener;
MyContentViewAdapter(List<List<POJOContent>> data) {
this.data = data;
}
#Override
public MyContentViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_content_card, parent, false);
return new MyContentViewAdapter.ViewHolder(v);
}
#Override
public void onBindViewHolder(MyContentViewAdapter.ViewHolder holder, int position) {
holder.bind(data.get(position));
}
public void setOnItemClickListener(MyClickListener clickListener) {
this.clickListener = clickListener;
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView textId;
private InnerListAdapter innerAdapter;
// inside constructor we are initializing inner recyclerView and inner Adapter.
// there will only be 3 - 5 instances of them ever created(using this
// particular viewHolder layouts), no more.
// might be slightly more if u use layouts with lower height
ViewHolder(View v) {
super(v);
textId = v.findViewById(R.id.tv_Id);
RecyclerView innerRecycler = v.findViewById(R.id.rv_inner_list);
// I added DividerItemDecoration so it would be clear that there are actually different viewHolders
// displayed by recyclerView
innerRecycler.addItemDecoration(new DividerItemDecoration(v.getContext(), DividerItemDecoration.VERTICAL));
innerAdapter = new InnerListAdapter();
innerRecycler.setAdapter(innerAdapter);
}
/* We just submit new list for our inner adapter
so it will handle rebinding values to its viewHolders */
void bind(List<POJOContent> pojoList){
textId.setText(pojoList.get(0).id);
innerAdapter.setNewItems(pojoList);
}
}
}
InnerListAdapter.java
public class InnerListAdapter extends RecyclerView.Adapter<InnerListAdapter.InnerViewHolder> {
private List<POJOContent> items = new ArrayList<>();
#NonNull
#Override
public InnerViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new InnerViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_inner_list, parent, false));
}
#Override
public void onBindViewHolder(#NonNull InnerViewHolder holder, int position) {
holder.bind(items.get(position));
}
#Override
public int getItemCount() {
return items.size();
}
void setNewItems(List<POJOContent> newItems){
items.clear();
items.addAll(newItems);
notifyDataSetChanged();
}
class InnerViewHolder extends RecyclerView.ViewHolder{
TextView tv_value;
TextView tv_content;
InnerViewHolder(#NonNull View itemView) {
super(itemView);
tv_value = itemView.findViewById(R.id.tv_value);
tv_content = itemView.findViewById(R.id.tv_content);
}
void bind(POJOContent pojoContent){
tv_value.setText(pojoContent.getKey());
tv_content.setText(pojoContent.getContent());
}
}
}
fragment_content_card.xml layout for main recyclerView viewholder
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:textSize="32sp"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="ID" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_inner_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_Id" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
item_inner_list.xml layout for inner recylerVoews' viewholder
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="value" />
<TextView
android:id="#+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_value"
tools:text="content" />
</androidx.constraintlayout.widget.ConstraintLayout>
create another pojo class for RV List Items with content and keys list
public class POJOListItem {
public String id;
public List<String> contents = new ArrayList<>();
public List<String> keys = new ArrayList<>();
}
then validate and map MyContentWrapper object to List<POJOListItem>, check the list if it contains the id, if it has the same id then add them to the contents and keys list or create a new item and add it. based on the list size dynamically add item in a container layout inside the view holder.
Check out this small demo project https://github.com/toxic-charger/SO-63045236
Tested with 50000+ items no performance issus
Based on your question, you have separate issues to tackle:
1 - Define better the data you have. You currently have POJOContent but you want to display something like a POJOContentGroup, so before getting to the view part you pre-process your data and map it from MyContentWrapper to List<POJOContentGroup>.
data class POJOContentGroup(val id: String, val contentList: List<POJOContent>) (This is in Kotlin for brevity)
2 - Having the previous defined, now you have a simple task: Display a list of POJOContentGroup. Create your recycler view based on that, similar to what you've done initially, with the difference that the item layout will be a TextView for the id and a RecyclerView for the contentList, which is the one you currently have.
As per my understanding of problem description, you are trying to construct array like this
[
"some content"+"some content"+"some content", // id = 240
"more content"+"more content 1"+"more content 2" // id = 241
]
and you want to show these 2 values in list of TextView
but the array you constructed will be,
[
"some content",
"some content",
"some content",
"more content",
"more content 1",
"more content 2"
]
and hence each entry is displayed in separate TextView
To debug further, please check value of pojo list after
pojo.addAll(map.values())
// may be if you want to group entries with comma delimiter, use
// pojo.addAll(TextUtils.join(", ", map.values()))
Hi its late but i would say create one class ListItem
class ListItem {
private var item: HashMap<Int, POJOContent>? = null
}
and overwrite toString in your POJOContent to return the values you want to display on text view like
#Override
public String toString() {
return value + "\n" + content;
}
Then use List in your adapter
Then in bind
//take care of null checks and you can use System.getProperty("line.separator"); in place of line saperator also don't forgate to set maxLine property of your text view to eome higher value
void bind(ListItem item){
Array<POJOContent> contents = item.values().toArray()
tv_value.setText(contents[0].getKey());
tv_content.setText(contents.toString().replace("[","").replace("]","").replace(",","\n"));
}
I think nested-expandable-recyclerview should be the right choice to achieve your goal, which will also increase your user experience. By default can open the nested recycler with your child data of the JSON.
Related
I am creating an android app in java, I am using Room database.
I have three tables :
Users table( user_id and user_name)
Products table( product_id and product_name)
Orders table ( order_id , user_id and product_id )
I am using ViewModel to query information and populate it to UI.
I have one query that get from Orders table the user_id and product_id.
I have this case :
user_id number 1 have ordered 3 products.
private void setUpViewModel() {
viewModel.getUserProduct(1).observe(this,new Observer<List<OrdersTable>>() {
#Override
public void onChanged(#Nullable List<OrdersTable> order) {
mAdapter.setUserList((ArrayList<OrdersTable>) order);
}
}
);
}
this query will get product 1,2 and 3 for user No.1
I want to display the user name and product name in my recylcerView but i do not know how to display two different type of table ( User table and product table) in the recylcerView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_userName_xml"
android:text="user name"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_prodcutName_xml"
android:layout_marginLeft="40dp"
android:text="product"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
ArrayList<OrderTable> order_list;
ArrayList<UserTable> user_list;
ArrayList<ProductTable> product_list;
public MyAdapter(Context context) {
this.mContext = context;
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_ts, parent, false);
MyAdapter.MyViewHolder viewHolder = new MyAdapter.MyViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int position) {
OrderTable orders = order_list.get(position);
UserTable user = user_list.get(position);
ProductTable productTable = product_list.get(position);
holder.tv_userName.setText(user.getUserName());
holder.tv_productName.setText(productTable.getProductName());
}
#Override
public int getItemCount() {
if (order_list == null) {
return 0;
}
return order_list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_userName, tv_productName;
public MyViewHolder(View itemView) {
super(itemView);
tv_userName= itemView.findViewById(R.id.tv_userName_xml);
tv_productName= itemView.findViewById(R.id.tv_prodcutName_xml);
}
}
public void setTs_list(ArrayList<OrderTable> ts) {
this.order_list = ts;
notifyDataSetChanged();
}
}
You can join the data from these tables and bind that result in a custom model class called ResultModel You can get user_name and product_name from that ResultModel
user table
product table
t_order table
Join query :
select u.user_name, p.product_name from user u, product p, t_order o
where o.user_id = u.user_id and o.product_id = p.product_id
result
Create one model class ResultModel
public class ResultModel {
public String getUser_name() {
return user_name;
}
public String getProduct_name() {
return product_name;
}
private String user_name;
private String product_name;
}
Updated observer
Note: replace OrdersTable with ResultModel wherever required in your Adapter too
viewModel.getUserProduct(1).observe(this,new Observer<List<ResultModel>>() {
#Override
public void onChanged(#Nullable List<ResultModel> order) {
mAdapter.setUserList((ArrayList<ResultModel>) order);
}
}
);
}
You can create multiple recycle view inside onBindViewHolder like this
#Override
public void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {
final String sectionName = dataList.get(i).getHeaderTitle();
ArrayList singleSectionItems = dataList.get(i).getAllItemsInSection();
itemRowHolder.itemTitle.setText(sectionName);
SectionListDataAdapter itemListDataAdapter = new SectionListDataAdapter(mContext, singleSectionItems);
itemRowHolder.recycler_view_list.setHasFixedSize(true);
itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);
}
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected TextView itemTitle;
protected RecyclerView recycler_view_list;
public ItemRowHolder(View view) {
super(view);
this.itemTitle = (TextView) view.findViewById(R.id.itemTitle);
this.recycler_view_list = (RecyclerView) view.findViewById(R.id.recycler_view_list);
}
}
See sample code here
My advice is to use different view types. Here is an article about it
First of all, you have to come up with an abstraction of a data source for your adapter. Usually we have a simple array or a list as a data source, but in your case you have not one but three types. So you can create an interface or use a collection to store all your data inside adapter, the way how you'd like to make it is up to you.
After that, you have to create different constants for each type of your models. In your example you have three models: User, Product and Order. So I'd recommend creating three constants for each of these types. These constants should be placed inside your adapter class as static fields.
private static final int USER_VIEW_TYPE = 1;
private static final int PRODUCT_VIEW_TYPE = 2;
private static final int ORDER_VIEW_TYPE = 3;
Then, you have to override method getItemViewType(int position) in your adapter. There you must tell adapter what type a particular item has. For instance, you've decided to store all your data as List<Object> (but don't do that in the production code, I'm using the Object type here just for example.). So in this case your method would look like this:
#Override public int getItemViewType(int position) {
if (data[position] instanceof User) {
return USER_VIEW_TYPE;
} else if (data[position] instanceof PRODUCT_VIEW_TYPE) {
return PRODUCT_VIEW_TYPE;
} else return ORDER_VIEW_TYPE;
}
After that you need to create different ViewHolders for your types, so you will have a UserViewHolder, a ProductViewHolder and a OrderViewHolder.
And, in order to differentiate them finally, you should use the viewType parameter in the onCreateViewHolder method. The logic is quite simple: you have to check value of this parameter and inflate different layouts depending on the result. For example:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
if (viewType == USER_VIEW_TYPE) {
return new UserViewHolder(layoutInflater.inflate(R.layout.item_user_type, parent));
} else if (viewType == PRODUCT_VIEW_TYPE) {
return new ProductViewHolder(layoutInflater.inflate(R.layout.item_product_type, parent));
} else return new OrderViewHolder(layoutInflater.inflate(R.layout.item_order_type, parent));
}
Recently I started coding my really first android project by using Android Studio 3.1.2.
Inside on one of my fragments, I have a recyclerview, in which I want to show data from a JSON API. For the items I created a custom layout which is intended to be used as a CardView.
I proceeded that far, that I receive my data, but my recyclerview remains empty. Also, if the json object is empty, or the API deosn't respond, the idea was to let the recyclerview automatically add an item, that tells the user that there's no data or the API was not available (would be cool, if I could use the same layout here, I created). This is how my code looks so far:
The raw structure of report_compact_card.xml (embedded in android.support.v7.widget.CardView):
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
app:cardCornerRadius="2dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/linearLayout"
...>
<TextView
android:id="#+id/report_header_textview"
... />
<TextView
android:id="#+id/report_body_textview"
... />
<ImageView
android:id="#+id/report_icon_imageview"
... />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
My ReportCompactAdapter:
public class ReportCompactAdapter extends RecyclerView.Adapter<ReportCompactAdapter.ReportCompactViewHolder> {
private Context context;
private ArrayList<Report> reports;
public ReportCompactAdapter(Context context, ArrayList<Report> reports) {
this.context = context;
this.reports = reports;
}
#Override
public ReportCompactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.report_compact_card, parent, false);
return new ReportCompactViewHolder(view);
}
#Override
public void onBindViewHolder(ReportCompactViewHolder holder, int position) {
//this is where I want to set a "no data" card
if (reports.isEmpty()) {
holder.reportBodyTextView.setText("Keine Meldungen");
holder.reportBodyTextView.setText(":)");
holder.reportIconImageView.setImageResource(R.drawable.ic_report_ok_24dp);
} else {
//here I want to fill my cards with my json data
Report currentReport = reports.get(position);
String currentId = currentReport.getId();
String currentTest = currentReport.getTest();
String currentTOpen = currentReport.getTOpen();
Employee currentEmployee = currentReport.getEmployee();
holder.reportHeaderTextView.setText(currentTest);
holder.reportBodyTextView.setText(currentId + " " + currentTOpen + " " + currentEmployee.getName());
holder.reportIconImageView.setImageResource(R.drawable.ic_report_err_24dp);
}
}
#Override
public int getItemCount() {
return reports.size();
}
public class ReportCompactViewHolder extends RecyclerView.ViewHolder {
public TextView reportHeaderTextView;
public TextView reportBodyTextView;
public ImageView reportIconImageView;
//this is where I try to access my layout
public ReportCompactViewHolder(View itemView) {
super(itemView);
reportHeaderTextView = itemView.findViewById(R.id.report_header_textview);
reportBodyTextView = itemView.findViewById(R.id.report_body_textview);
reportIconImageView = itemView.findViewById(R.id.report_icon_imageview);
}
}
}
Additionally in may OverviewFragment, where I use my recyclerview i'm doing like so:
public class OverviewFragment extends Fragment {
private ArrayList<Report> reports;
private RecyclerView reportRecyclerView;
private ReportCompactAdapter reportCompactAdapter;
private RequestQueue requestQueue;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_overview, container, false);
reports = new ArrayList<Report>();
//here I want to set up my recyclerview
reportRecyclerView = fragmentView.findViewById(R.id.report_recyclerview);
reportRecyclerView.setHasFixedSize(true);
reportRecyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
//I already set the adapter here to avoid the warning that no adapter is attached
reportRecyclerView.setAdapter(new ReportCompactAdapter(this.getContext(), reports));
//I use volley for Request stuff
requestQueue = Volley.newRequestQueue(this.getContext());
//this guy is intended to fetch my json data
parseJSON();
return fragmentView;
}
private void parseJSON() {
JSONObjectRequest request = new JSONObjectRequest(Request.Method.GET, "myurl.com", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("reports");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject report = jsonArray.getJSONObject(i);
reports.add(new Report(json));
}
//here I set my adapter after parsing my data
reportCompactAdapter = new ReportCompactAdapter(OverviewFragment.this.getContext(), reports);
reportRecyclerView.setAdapter(reportCompactAdapter);
} catch(JSONException e) {
e.printStackTrace();
}
}
}, new Response.OnErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}
}
Because of some reason I didn't even get my "no data" card into my recyclerview, neither my filled cards, although "myurl.com" is valid and doesn't throw any error. So my question is, where did I mis a step to successfully squeeze my cards into my recyclerview? Thanks in forward!
You need return atleast 1 item in getItemCount() like below
#Override
public int getItemCount() {
return reports.size()==0?1:report.size();
}
So I am trying to create an android app on alcohol and mixers, but have been stuck on the following problem for a while now...
I want to display every alcohol category (eg: Gin, Vodka, Whiskey, etc..) in a RecyclerView that scrolls horizontally, and every alcohol type (eg: Bourbon and Scotch for the Whiskey Category) in a RecyclerView that scrolls vertically.
I have created one adapter for each RecyclerView (CategoryAdapter for the horizontal RecyclerView called category, and MixerAdapter for the vertical RecyclerView called categoryDetails).
So far I've managed to create and display category as desired, but have some difficulties for categoryDetails.
Basically, I can't figure out how to update the contents of categoryDetails when an item of category is selected:
For example
If the user selects Whiskey in category, I want categoryDetails to display Bourbon and Scotch.
If the user then selects Gin, I want categoryDetails to only display Gin and Flavoured Gin, etc...
I hope I've been clear enough on what it is I want to accomplish!
Any help would be much appreciated, thanks!!
Here is a screenshot of how the screen appears when the activity is loaded.
If a user selects Rum (white on black RecyclerView), I want the RecyclerView currently showing Gin and Flavored Gin (black on white RecyclerView) to show the alcohols associated with the Rum category.
Screenshot
Here is the XML file holding the two recyclerViews category and categoryDetails
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TipsDrinks">
<android.support.v7.widget.RecyclerView
android:id="#+id/category"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#android:color/black"
android:orientation="horizontal"
android:scrollbars="horizontal"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SpinnerPrompt" >
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/categoryDetails"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:background="#android:color/white"
android:orientation="horizontal"
android:scrollbars="vertical"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="#+id/adView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/category"
app:layout_constraintVertical_bias="0.0">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
The Class associated with the previous layout
public class TipsDrinks extends AppCompatActivity {
private CategoryAdapter categoryAdapter; // Adapter used for the category RecyclerView
public MixerAdapter mixerAdapter; //Adapter used for the categoryDetails RecyclerView
private RecyclerView categories; // The RecyclerView holding the name of each alcohol category
public RecyclerView catDetails; // The RecyclerView holding each type of that alcohol category
private DrinkMenu drinkMenu; // The DrinkMenu is another Class holding every Alcohol Category, the type of each alcohol and the mixers good with it
private ArrayList<String> drinkCat = new ArrayList<>(); // A String ArrayList holding the name of each alcohol category (Gin, Vodka, Rum, Whiskey, Other)
private ArrayList<Drink> drinkMixers = new ArrayList<>(); // A Drink(String, ArrayList<String>) ArrayList holding the mixers of every Drink
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_drinks);
categories = findViewById(R.id.category);
catDetails = findViewById(R.id.categoryDetails);
drinkMenu = new DrinkMenu();
setCategoryView(); // Creates the category RecyclerView
setDrinkMixers(); // Creates the categoryDetails RecyclerView
}
private void setCategoryView(){
categoryAdapter = new CategoryAdapter(drinkCat);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(TipsDrinks.this, LinearLayoutManager.HORIZONTAL, false);
categories.addItemDecoration(new DividerItemDecoration(TipsDrinks.this, DividerItemDecoration.HORIZONTAL));
categories.setLayoutManager(layoutManager);
categories.setItemAnimator(new DefaultItemAnimator());
categories.setAdapter(categoryAdapter);
prepareCategories();
}
private void prepareCategories(){
drinkCat.clear();
drinkCat.addAll(drinkMenu.getDrinkCategories());
for (String drink : drinkCat) {
System.out.println(drink);
}
categoryAdapter.notifyItemInserted(drinkCat.size() - 1);
}
private void setDrinkMixers() {
mixerAdapter = new MixerAdapter(drinkMixers);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
catDetails.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
catDetails.setLayoutManager(layoutManager);
catDetails.setItemAnimator(new DefaultItemAnimator());
catDetails.setAdapter(mixerAdapter);
prepareMixers();
}
private void prepareMixers() {
drinkMixers.clear();
drinkMixers.addAll(drinkMenu.getDrinkMixers(categoryAdapter.getCurrentCategory()));
mixerAdapter.notifyItemChanged(drinkMixers.size() -1);
}
}
The CategoryAdapter Class for the category RecyclerView
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> {
private List<String> drinkList; //A List of String holding the category for each drink
private String textName = "Gin";
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView name; // The TextView holding the name of the category
MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
name = view.findViewById(R.id.catName);
}
#Override
public void onClick(View v) {
//Code to update the contents of the categoryDetails RecyclerView
}
}
CategoryAdapter(ArrayList<String> drinkList) { this.drinkList = drinkList; }
#Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.category_text, parent, false);
return new MyViewHolder(itemView);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(CategoryAdapter.MyViewHolder holder, int position) {
String drink = drinkList.get(position);
holder.name.setText(drink);
textName = holder.name.getText().toString();
}
#Override
public int getItemCount() {
return drinkList.size();
}
public String getCurrentCategory() {
return textName;
}
}
The MixerAdapter Class for the categoryDetails RecyclerView
public class MixerAdapter extends RecyclerView.Adapter<MixerAdapter.MyViewHolder>{
private List<Drink> mixerList;
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView mixerCat, mixers;
MyViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mixerCat = view.findViewById(R.id.mixerCat2);
mixers = view.findViewById(R.id.mixers2);
}
#Override
public void onClick(View v) {
if (mixers.getVisibility() == mixerCat.getVisibility()) {
mixers.setVisibility(View.GONE);
}
else {
mixers.setVisibility(View.VISIBLE);
}
}
}
MixerAdapter(ArrayList<Drink> mixerList) {
this.mixerList = mixerList;
}
#Override
public MixerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.drinks_row, parent, false);
return new MixerAdapter.MyViewHolder(itemView);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(MixerAdapter.MyViewHolder holder, int position) {
Drink drink = mixerList.get(position);
StringBuilder mixerStringList = new StringBuilder("");
holder.mixerCat.setText(drink.getSorM());
for (String mixer: drink.getMixers()) {
mixerStringList.append(mixer).append("\n");
}
holder.mixers.setText(mixerStringList.toString().trim());
}
#Override
public int getItemCount() {
return mixerList.size();
}
}
Create a callback interface in CategoryAdapter. When user clicks on an item in Category RecycleView, it's activity responsibility to populate MixerAdapter with new items as per selection.
CategoryAdapter
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> {
private CategoryInterface callback;
CategoryAdapter(ArrayList<String> drinkList, CategoryInterface listener) {
this.drinkList = drinkList;
callback = listener;
}
#Override
public void onClick(View v) {
//Code to update the contents of the categoryDetails RecyclerView
callback.onItemSelected(getAdapterPosition());
}
public interface CategoryInterface {
void onItemSelected(int position);
}
}
Activity
public class TipsDrinks extends AppCompatActivity implements CategoryInterface {
private void setCategoryView(){
categoryAdapter = new CategoryAdapter(drinkCat, this);
}
#Override
void onItemSelected(int position) {
//Reassign items in MixerAdapter
}
}
that depends on your implementations , one global method which should work for all implementations is using HolderView method and using onTouchListener on the groups's root view
v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
//the child should expand and be updated
}
return false;
}
});
I want to list only the disliked items in my recyclerview. I have a full list of items in rv in MainActivity (did not set visibility here). I can set for each items like or dislike by clicking on imagebutton. The MainActivity shows full list of items (cardviews) that shows imagebutton likes or not. If item is liked, this is stored in firebase db as separate entry under Likes with item key (firebase key .push) and not under Items. (in firebase db I have Users, Items, Likes).
Here is my subactivity code, DislikedItemsActivity, where I want to show only items that are disliked by using setVisibility(View.GONE) for items that are liked. This still holds the space between items for the View.GONE items as well (though these cardviews are empty).
mRecyclerView = (RecyclerView) findViewById(R.id.rvItemList);
mRecyclerView .setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(linearLayoutManager);
final FirebaseRecyclerAdapter<Item, MainActivity.ItemViewHolder>
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Item,
MainActivity.ItemViewHolder>(
Item.class,
R.layout.list_item,
MainActivity.ItemViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(final MainActivity.ItemViewHolder viewHolder, final Item model, final int position) {
final String itemKey = getRef(position).getKey();
mDatabaseItemsLiked.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// if item is not liked, thus no user set in db ( I want to see only items that are liked in my recyclerview)
if (!dataSnapshot.child(itemKey).hasChild(mAuth.getCurrentUser().getUid())) {
viewHolder.mView.setVisibility(View.VISIBLE);
viewHolder.itemNameSetup(model.getItemName());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent itemSheetIntent = new Intent(DislikedItemsActivity.this, ItemSheetActivity.class);
adatlapIntent.putExtra("item_key", itemKey);
startActivity(itemSheetIntent);
}
});
} else {
viewHolder.mView.setVisibility(View.GONE);
mRecyclerView.getAdapter().notifyItemRemoved(position); //this puts together the visible items, but when scrolling, it gets messed up
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.toString());
}
});
}
#Override
public void onBindViewHolder(MainActivity.TermekViewHolder viewHolder, int position) {
super.onBindViewHolder(viewHolder, position);
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
I looked for many solutions like onBindViewHolder, notifyDataChanged, set margin to 0, set layout size in xml to wrap_content. The best I could get is to have the not-liked items without space with mRecyclerView.getAdapter().notifyItemRemoved(position);, but scrolling the list backwards the whole rv gets messed up (duplicate entry, empty spaces, disordered list).
I don't know how to list only the disliked items from the complete item list from MainActivity rv in a new activity? My code above shows only disliked items, but only until I scroll to end of list, if I scroll backwards the rv gets messed up. I logged the positions of views (18items) in onBindViewHolder and first it counts all items in sequence (17,16,15,14...0), but scrolling from end of list to backwards the position jumps from 0 to 4 like 7times (changes always how many times) then it is the same for item 5,6, until item 17 (all of their positions showed in onBindViewHolder 7 or 8 times during scrolling ie. 5,5,5,5,6,6,6,6) and only for backward scrolling and during backward move rv shows only disliked items or empty views or duplicate entry of disliked item.
my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/hatter"
tools:context="com.example.user.itemlist.ItemsLikedActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rvItemList"
>
</android.support.v7.widget.RecyclerView>
(don't know how to add pics) When disliked list appears, it shows first item (cardview takes up the full screen), and when I start to scroll the list (from 1. visible item to next visible item) if there is space(item1 vis and next vis item is 4), rearrange and I can see that next visible item(item4) moves to 1. visible item, then the rest of the list is arranged well, until I start to scroll back, then it rearranges the rv with spaces and with double entry. The list goes back and forth until both end (which is the length of full items list and not just the disliked items), but visible items gets all messed up.
use this code to remove occupied space :
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = 0;
holder.itemView.setLayoutParams(params);
I found solution to filter the complete database. In my question I wanted to get only the liked/disliked items in a separate activity, though my previous code showed filtered items, but with gaps.
In below code, I changed the DatabaseReferences (mDatabase -node with complete item list and mDatabaseItemsLiked -node with item uid and user uid).
This gave only empty cards with only number as the likedItems, but to get name from the mDatabase (complete list), I used dataSnapshot.getValue(Item.class).getItemName().
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Item,
MainActivity.ItemViewHolder>(
Item.class,
R.layout.list_item,
MainActivity.ItemViewHolder.class,
mDatabaseItemsLiked
) {
#Override
protected void populateViewHolder(final MainActivity.ItemViewHolder
viewHolder, final Item model, final int position) {
final String itemKey = getRef(position).getKey();
mDatabase.child(itemKey).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
viewHolder.itemNameSetup(dataSnapshot.getValue(Item.class).getItemName());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent itemSheetIntent = new Intent(LikedItemsActivity.this, ItemSheetActivity.class);
adatlapIntent.putExtra("item_key", itemKey);
startActivity(itemSheetIntent);
}
});
This works for me without any problem. I hope it is network efficient.
You can try to store the liked ítem in boolean array and later in populateViewHolder check if ítem has like o no and set visibility.
I would do like that:
In your class declare :
private boolean [] itemLiked;
In your constructor :
this.itemLiked = new boolean [arrayOfAllItems.size]
On click event:
itemLiked[position] = true; //Where position is row position
onBindViewholder or in your case populateViewHolder:
if (!itemLiked[position]) {
viewHolder.mView.setVisibility(View.GONE); }
Hope it helps, good luck!
EDITED
I do not understand exactly what you want to do, that's why I leave you the code for two cases.
Case 1. Mark and dis-mark the rows.
Case 2. Save to database or delete.
In continuation the complete code
Activity XML add RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="#+id/my_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Make a custom layout for row:
<TextView
android:id="#+id/question_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="QUESTION"/>
<ImageButton
android:id="#+id/like"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/ic_input_add"
android:background="#android:color/transparent"
android:layout_marginRight="4dp"/>
<ImageButton
android:id="#+id/dislike"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:drawable/ic_delete"
android:background="#android:color/transparent"
android:layout_marginRight="4dp"/>
Make a Model class:
public class SomeModel {
private String question;
public SomeModel(String question) {
this.question = question;
}
public String getQuestion() {
return question;
}
}
Make Adapter Class:
public class SomeAdapter extends RecyclerView.Adapter {
private ArrayList<SomeModel> arrayList;
private boolean [] item_has_like, item_hase_vote;
public SomeAdapter(ArrayList<SomeModel> arrayList) {
this.arrayList = arrayList;
this.item_has_like = new boolean[arrayList.size()];
this.item_hase_vote = new boolean[arrayList.size()];
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder myViewHolder = null;
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.draw_row, parent, false);
myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final MyViewHolder myViewHolder = (SomeAdapter.MyViewHolder)holder;
final SomeModel item = arrayList.get(position);
int backGround;
/**In background you can save whateveryou need, example:
* backGround= R.drawable.some_background;
* backGround= View.GONE;
*.....
**/
if (item_hase_vote[position]){
if (item_has_like[position])
{
backGround= Color.GREEN;//
} else {
backGround= Color.RED;
}
} else {
backGround= Color.TRANSPARENT;
}
myViewHolder.questionTV.setText(item.getQuestion());
myViewHolder.questionTV.setBackgroundColor(backGround);
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView questionTV;
private ImageView like, dislike;
public MyViewHolder(final View itemView) {
super(itemView);
questionTV = (TextView)itemView.findViewById(R.id.question_tv);
like = (ImageView)itemView.findViewById(R.id.like);
dislike = (ImageView)itemView.findViewById(R.id.dislike);
like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Item been voted
item_hase_vote[getAdapterPosition()] = true;
//Item got Like save in boolean array by row position
item_has_like[getAdapterPosition()] = true;
//notify your adapter
notifyDataSetChanged();
/*OR Here comes the code where You save Item in Your Data Base.*/
}
});
dislike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Item been voted
item_hase_vote[getAdapterPosition()] = true;
// Item got DisLike save in boolean array by row position
item_has_like[getAdapterPosition()] = false;
//notify your adapter
notifyDataSetChanged();
/*OR Here You Remove item on Dislike
arrayList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
notifyItemRangeChanged(getAdapterPosition(),arrayList.size());
*/
}
});
}
}
}
And Your Activity:
public class SomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_some);
ArrayList<SomeModel> arrayList = new ArrayList<>();
for (int i = 0; i <77 ; i++) {
arrayList.add(new SomeModel("Question " + i));
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_rv);
SomeAdapter adapter = new SomeAdapter(arrayList);
recyclerView.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
}
}
Saving the selected item by the adapterPosition in boolean, String, int....[], the Adapter always gona know whats going on with every item and like that your list always gona be arranged.
Good Luck!
I also got the same problem. what i thought was if RelativeLayout load one after one, height=0, the specs will remove.So it works for me.
This is my ViewHolder. I Introduces my reletivelayout here.
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView txtdate;
RelativeLayout con_rel;
String name_day = "no name";
public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
con_rel=(RelativeLayout)itemView.findViewById(R.id.con_rel);
txtdate = (TextView)itemView.findViewById(R.id.day);
}
}
The I set height and width
con_ref=FirebaseDatabase.getInstance().getReference().child("/consultation");
FirebaseRecyclerAdapter<Consultation,SelectConsaltation.BlogViewHolder>recyclerAdapter=new FirebaseRecyclerAdapter< Consultation,SelectConsaltation.BlogViewHolder>(
Consultation.class,
R.layout.consultation_card,
SelectConsaltation.BlogViewHolder.class,
con_ref
) {
#Override
protected void populateViewHolder(final SelectConsaltation.BlogViewHolder viewHolder, final Consultation model, final int Consultation) {
Shedule_ref.child(model.getScheduleID()).child("Day").addValueEventListener(new ValueEventListener() {
ViewGroup.LayoutParams params = viewHolder.con_rel.getLayoutParams();
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
name_day = dataSnapshot.getValue(String.class);
if (doctor_id_from_doctor.equals( model.getDoctorID() )){
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date strDate = null;
try {
strDate = sdf.parse(model.getDate());
} catch (ParseException e) {
e.printStackTrace();
}
if(System.currentTimeMillis()<=strDate.getTime() ) {
params.height = 300;
params.width =800;
viewHolder.con_rel.setLayoutParams(params);
viewHolder.setDate(model.getDate(),name_day);
}
else {
**params.height = 0;
params.width = 0;
viewHolder.con_rel.setLayoutParams(params);**
}
}
else {
params.height = 0;
params.width = 0;
viewHolder.con_rel.setLayoutParams(params);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
recyclerView.setAdapter(recyclerAdapter);
}
My card view code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/con_rel"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#a2ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="3dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/day"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:textColor="#color/colorBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_07th_of_sunday_january_2018_at_9_00am"/>
<TextView
android:id="#+id/nextnumber"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:textColor="#color/colornextnumber"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:text="#string/next_avealable_number_is_04"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/booknow"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="240dp"
android:layout_marginBottom="10dp"
android:textColor="#color/colorbookNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/book_now"/>
</LinearLayout>
</RelativeLayout>
Here is my inreface
I have an array which contains values like {seat 1,seat 1,seat 1,seat 2, seat 2, seat 3, seat 4,seat 4,seat 4}. ie Left side of image
I need to group them according to its seat values ie. Right side of image.
For given environment seat list array will be: {"1", "-1", "-1", "2", "-1", "3", "4", "-1", "-1"}
currently i am using recyclerview and checking this with below condition:
if(seat_list.get(holder.getAdapterPosition()).equals("-1"))
{
Log.d("aaa", "if :" + seat_list.get(holder.getAdapterPosition()) );
}
else
{
Log.d("aaa", "else :" + seat_list.get(holder.getAdapterPosition()) );
holder.ll_header.setVisibility(View.VISIBLE);
holder.tv_seat_num.setText(seat_list.get(holder.getAdapterPosition()));
}
With above code list is getting destroyed on scrolling the recyclerview.
i already sorted but i want to give common header or heading to all orders with same seat
First of all, I don't think your data ready to be displayed. So it`d be better to prepare it.
And it is better to have some typed data model, that contains info about seats and orders. OOP, you know ?
To keep example simple, I prepare and map data within activity, but it's not very good idea for real world projects.
So, from your description, we have some array, where items could be duplicated and their position in array - it is orderId. So, in other way we want to have some grouped structure with key-value pair, where key -> № of Seat, and value -> collection of orderIds. But it will be much better to have a class, that represent such relations and have info about things described above.
With data like that, it will be not very big problem to just display them in recyclerView.
At the begining, I wanted to place just couple methods here, but after I realized that it may be confused without full code list.
So, here you go all stuff.
This is result of what you will get with listed below code:
= = = = = time to see some code = = = = =
Activity -> (with only RecyclerView in xml layout)
public class SeatsActivity extends AppCompatActivity {
RecyclerView recycler;
SeatsAdapter seatsAdapter;
final String[] orders =
{ "seat 1", "seat 1", "seat 1", "seat 2", "seat 2", "seat 3", "seat 4", "seat 4", "seat 4" };
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seats);
setTitle("Seats example");
recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));
final Map<String, List<Integer>> groupedOrders = groupData(orders);
final List<SeatItem> seats = mapToSeatsItems(groupedOrders);
bindToRecycler(seats);
}
private void bindToRecycler(List<SeatItem> seatItems) {
seatsAdapter = new SeatsAdapter(seatItems);
recycler.setAdapter(seatsAdapter);
}
private Map<String, List<Integer>> groupData(final String[] items) {
final Map<String, List<Integer>> grouped = new LinkedHashMap<>();
for (int i = 0; i < items.length ; i++) {
// I took it from your description. I image that orderId is the place of the seat in array
final Integer orderId = i+1;
final String headerKey = items[i];
if (!grouped.containsKey(headerKey)){
List<Integer> list = new ArrayList<>();
list.add(orderId);
grouped.put(headerKey, list);
} else {
List<Integer> list = grouped.get(headerKey);
list.add(orderId);
}
}
return grouped;
}
private List<SeatItem> mapToSeatsItems(Map<String, List<Integer>> groupOrders) {
final List<SeatItem> list = new ArrayList<>(groupOrders.size());
for (String key : groupOrders.keySet()) {
final SeatItem item = new SeatItem(key, groupOrders.get(key));
list.add(item);
}
return list;
}
}
Then SeatItem, that represent data model (POJO, Bean, whatever)
public class SeatItem {
public SeatItem(String header, List<Integer> orderIds) {
this.header = header;
this.orderIds = orderIds;
}
final String header;
final List<Integer> orderIds;
public String getHeader() {
return header;
}
public List<Integer> getOrderIds() {
return orderIds;
}
}
SeatsAdapter ->
public class SeatsAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
final List<SeatItem> dataList;
public SeatsAdapter(final List<SeatItem> items){
dataList = items;
}
#Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup
parent, int viewType) {
final View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_seat, parent,
false);
return new SeatsViewHolder(view);
}
#Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int
position) {
final SeatsViewHolder seatsViewHolder = (SeatsViewHolder) holder;
final SeatItem item = dataList.get(holder.getAdapterPosition());
final StringBuilder contentBuilder = new StringBuilder();
for (int i = 0; i < item.getOrderIds().size(); i++) {
// here is not production ready, but for just example will be ok
contentBuilder
.append("order Id: ")
.append(item.getOrderIds().get(i));
if (i + 1 != item.getOrderIds().size()){
contentBuilder.append("\n");
}
}
seatsViewHolder.tvHeader.setText(item.getHeader());
seatsViewHolder.tvContent.setText(contentBuilder);
}
#Override public int getItemCount() { return dataList.size(); }
private static class SeatsViewHolder extends RecyclerView.ViewHolder {
final TextView tvHeader;
final TextView tvContent;
SeatsViewHolder(View itemView) {
super(itemView);
tvHeader = (TextView) itemView.findViewById(R.id.tvHeader);
tvContent = (TextView) itemView.findViewById(R.id.tvContent);
}
}
}
And the last one - item xml, that will be displayed by recycler
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="header! "
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:padding="8dp"
android:background="#cccccc"
/>
<TextView
android:id="#+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="content ! ! ! "
android:layout_gravity="top"
android:gravity="top"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:padding="8dp"
/>
</LinearLayout>
You can sort using Collections.sort method