i'm trying to create a a resturant apk which it has a menu inside this menu you chose what kinda food you want lets say if you want a pizza and when you press pizza it will open another menu with different kind of pizza and when press the pizza that i want it should take you to another activity and show you the details of the pizza but its not showing me and i don't know what is the problem
this is my food list activity
package com.example.median1.demo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class FoodLIst extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference foodList;
String categoryId;
FirebaseRecyclerAdapter<Food,FoodViewHolder>adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
//firebase
database=FirebaseDatabase.getInstance();
foodList= database.getReference("Foods");
recyclerView=(RecyclerView)findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//get food list intent
if(getIntent()!=null){
categoryId=getIntent().getStringExtra("CategoryId");
if(!categoryId.isEmpty()&& categoryId!=null){
loadListFood(categoryId);
}
}
}
private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,
R.layout.food_item,
FoodViewHolder.class,
foodList.orderByChild("MenuId").equalTo(categoryId)) {
#Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
viewHolder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(viewHolder.food_image);
final Food local=model;
viewHolder.setItemClicklistener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
// start new activity for the food destails
Intent foodDetails=new Intent(FoodLIst.this,FoodDetail.class);
foodDetails.putExtra("FoodId",adapter.getRef(position).getKey()); //send foood id to new activity
startActivity(foodDetails);
}
});
}
};
//set adapter
Log.d("TAG",""+adapter.getItemCount());
recyclerView.setAdapter(adapter);
}
}
and this is my food details activity
package com.example.median1.demo;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.io.File;
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//firebase
database=FirebaseDatabase.getInstance();
foods=database.getReference("Foods");
//int View
numberButton=(ElegantNumberButton)findViewById(R.id.number_button);
btnCart=(FloatingActionButton)findViewById(R.id.btnCart);
food_description=(TextView)findViewById(R.id.food_description2);
food_name=(TextView)findViewById(R.id.food_name);
food_price=(TextView)findViewById(R.id.food_price);
food_image=(ImageView)findViewById(R.id.img_food);
collapsingToolbarLayout=(CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
//get food id from the intent
if(getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if(foodId.isEmpty()){
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food=dataSnapshot.getValue(Food.class);
//set image
Picasso.with(getBaseContext()).load(food.getImage()).into(food_image);
collapsingToolbarLayout.setTitle(food.getName());
food_price.setText(food.getPrice());
food_name.setText(food.getName());
food_description.setText(food.getDescription());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
and this is the food class for the food details where i retrieve all the childs from the firebase
package com.example.median1.demo;
/**
* Created by median1 on 11/5/2017.
*/
public class Food {
private String Name,Image,Description,Price,Discount,MenuId;
public Food() {
}
public Food(String name, String image, String description, String price, String discount, String menuId) {
Name = name;
Image = image;
Description = description;
Price = price;
Discount = discount;
MenuId = menuId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getDiscount() {
return Discount;
}
public void setDiscount(String discount) {
Discount = discount;
}
public String getMenuId() {
return MenuId;
}
public void setMenuId(String menuId) {
MenuId = menuId;
}
}
and this is the xml of the food details
i dont know if its important
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.median1.demo.FoodDetail">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="#android:color/transparent">
<ImageView
android:id="#+id/img_food"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:title="food name"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/btnCart"
android:src="#drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"
/>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:clipToPadding="false"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="Food name "
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layout_price"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/ic_attach_money_black_24dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/food_price"
android:text="1,000 "
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="wrap_content" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
android:id="#+id/number_button"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="18dp"
app:textSize="8sp"
app:backGroundColor="#color/colorAccent"
app:initialNumber="1"
app:finalNumber="20"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_description2"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="description"
android:textColor="#android:color/black"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Firebase database
can't you see the error on logcat or run? i think the error is here:
//get food id from the intent
if(getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if(foodId.isEmpty()){
getDetailFood(foodId);
}
}
or here
//get food list intent
if(getIntent()!=null){
categoryId=getIntent().getStringExtra("CategoryId");
if(!categoryId.isEmpty()&& categoryId!=null){
loadListFood(categoryId);
}
}
}
maybe check run when you click and the problem happens to see where is the problem.. you may get a null string
EDITED: i made the same app got the same problem with you and on my run its saying the problem is here if(!categoryId.isEmpty()&& categoryId!=null
Related
I am using firebase ui to load data from firebase database to recycler view but the data is not showing up inside the recycler view.
This is my fragment
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
import com.infinity.houseestimatorandmodeler.R;
import com.infinity.houseestimatorandmodeler.databinding.FragmentBuildersBinding;
public class BuilderFragment extends Fragment {
private BuilderAdapter adapter;
public BuilderFragment() {
// Required empty public constructor
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentBuildersBinding databinding= DataBindingUtil.inflate(inflater, R.layout.fragment_builders, container, false);
FirebaseRecyclerOptions<buildersModel> options =
new FirebaseRecyclerOptions.Builder<buildersModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("builders"), buildersModel.class)
.build();
adapter = new BuilderAdapter(options);
databinding.buildersRecyclerView.setAdapter(adapter);
return databinding.getRoot();
}
}`
this is my adapter class that is adapting the data for showing in the recycler view
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.infinity.houseestimatorandmodeler.R;
public class BuilderAdapter extends FirebaseRecyclerAdapter<buildersModel, BuilderAdapter.BuilderViewHolder> {
public BuilderAdapter(#NonNull FirebaseRecyclerOptions<buildersModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull BuilderViewHolder holder, int i, #NonNull buildersModel builder) {
holder.name.setText(builder.getName());
holder.builderClass.setText(builder.getBuilderClass());
holder.address.setText(builder.getAddress());
}
#NonNull
#Override
public BuilderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.builders_card, parent, false);
return new BuilderViewHolder(view);
}
class BuilderViewHolder extends RecyclerView.ViewHolder{
TextView name, builderClass, address;
public BuilderViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.nameofbuilder);
builderClass = itemView.findViewById(R.id.classOfBuilder);
address = itemView.findViewById(R.id.address);
}
}
}
this is my model class for data coming from firebase `
class buildersModel
{
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBuilderClass() {
return builderClass;
}
public void setBuilderClass(String builderClass) {
this.builderClass = builderClass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public buildersModel(String address, String builderClass, String name) {
this.address = address;
this.builderClass = builderClass;
this.name = name;
}
String address;
String builderClass;
String name;}
this is my firebase database
card view for individual element in recycler view
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#drawable/rounded_cards"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="20dp"
android:src="#drawable/ic_builder" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rounded_cards">
<TextView
android:id="#+id/nameofbuilder"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/baloo"
android:textSize="20dp"
android:padding="2dp"
android:text="Abdul Shakoor"/>
<TextView
android:id="#+id/classOfBuilder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="15sp"
android:textColor="#342C2C"
android:padding="2dp"
android:text="Location Islambad"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#342C2C"
android:padding="2dp"
android:text="Address : I10 Islamabad near Rahimia mosque"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
recycler view layout
<layout 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"
>
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/ic_builders"
android:background="#75CDDC"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/buildersRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
You didn't set the RecyclerView LayoutManger, you can either set it in you layout
<androidx.recyclerview.widget.RecyclerView
....
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Or in the fragment
databinding.buildersRecyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
I am getting the error E/RecyclerView: No adapter attached; skipping layout
here is my code.. The code runs fine except the intent method. Profile activity has no recycler view.
when I click to start the intent it shows the error in logcat
2020-06-14 09:53:06.887 3521-3521/com.technolgiya.nitcalling11 E/RecyclerView: No adapter attached; skipping layout
FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final FindFriendsViewHolder holder, final int position, #NonNull final Contacts model)
{
holder.userNameTxt.setText(model.getName());
Picasso.get().load(model.getImage()).into(holder.profileImageView);
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "view on:" + visit_user_id+getRef(position), Toast.LENGTH_LONG).show();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.getAdapterPosition();
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "clicked on:" + visit_user_id, Toast.LENGTH_LONG).show();
Intent intent = new Intent (FindPeopleActivity.this, ProfileActivity.class);
intent.putExtra("visit_user_id", visit_user_id);
intent.putExtra("profile_image", model.getImage());
intent.putExtra("profile_name", model.getName());
startActivity(intent);
}
});
}
here is my recycler view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".FindPeopleActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout_find_people"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_contacts"
android:layout_width="match_parent"
android:layout_height="60dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/search_user_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="find freiend..."
android:textColorHint="#android:color/white"
android:textAlignment="center"
android:layout_marginRight="16dp"
android:drawableLeft="#drawable/search"
android:textColor="#android:color/white"
android:layout_centerVertical="true"
>
</EditText>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/find_friends_list"
android:layout_below="#+id/app_bar_layout_find_people"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
here is my ProfileActivity.java
package com.technolgiya.nitcalling11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class ProfileActivity extends AppCompatActivity {
private String receiverUserID="";
private String receiverUserImage="";
private String receiverUserName="";
private ImageView background_profile_view;
private TextView name_profile;
private Button add_friend;
private Button decline_friend_request;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
background_profile_view.findViewById(R.id.background_profile_view);
name_profile = findViewById(R.id.name_profile);
add_friend = findViewById(R.id.add_friend);
decline_friend_request = findViewById(R.id.decline_friend_request);
receiverUserID = getIntent().getExtras().get("visit_user_id").toString();
receiverUserImage = getIntent().getExtras().get("profile_image").toString();
receiverUserName = getIntent().getExtras().get("profile_name").toString();
Toast.makeText(this, receiverUserID, Toast.LENGTH_SHORT).show();
Picasso.get().load(receiverUserImage).into(background_profile_view);
name_profile.setText(receiverUserName);
}
}
here is my activity_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ProfileActivity">
<ImageView
android:id="#+id/background_profile_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/profile_image"
android:scaleType="centerCrop"
>
</ImageView>
<TextView
android:id="#+id/name_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User name"
android:textColor="#color/colorPrimary"
android:textSize="30dp"
android:maxLines="2"
android:textStyle="bold"
android:gravity="center"
android:layout_centerInParent="true"
>
</TextView>
<Button
android:id="#+id/add_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:layout_below="#+id/name_profile"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginBottom="12dp"
android:background="#color/colorPrimary"
android:text="Add Friend"
android:padding="5dp"
>
</Button>
<Button
android:id="#+id/decline_friend_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:layout_below="#+id/add_friend"
android:layout_marginTop="1dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginBottom="12dp"
android:background="#color/colorPrimary"
android:text="Cancel Friend Request"
android:padding="5dp"
android:visibility="gone"
>
</Button>
</RelativeLayout>
here is the whole code of FindPeople_Activity
package com.technolgiya.nitcalling11;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class FindPeopleActivity extends AppCompatActivity {
private RecyclerView findFriendList;
private EditText searchET;
private String str = "";
private DatabaseReference usersRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_people);
usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
FirebaseRecyclerOptions<Contacts> options = null;
searchET = findViewById(R.id.search_user_text);
findFriendList = findViewById(R.id.find_friends_list);
findFriendList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
searchET.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charseq, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence charseq, int start, int before, int count) {
if (searchET.getText().toString().equals("")) {
Toast.makeText(FindPeopleActivity.this, "please write name to search", Toast.LENGTH_SHORT).show();
} else {
str = charseq.toString();
onStart();
}
}
#Override
public void afterTextChanged(Editable charseq) {
}
});
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Contacts> options = null;
if(str.equals(""))
{
options =
new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(usersRef, Contacts.class)
.build();
}
else
{
options =
new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(usersRef.orderByChild("name")
.startAt(str)
.endAt(str + "\uf8ff")
, Contacts.class)
.build();
}
FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final FindFriendsViewHolder holder, final int position, #NonNull final Contacts model)
{
holder.userNameTxt.setText(model.getName());
Picasso.get().load(model.getImage()).into(holder.profileImageView);
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "view on:" + visit_user_id+getRef(position), Toast.LENGTH_LONG).show();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.getAdapterPosition();
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "clicked on:" + visit_user_id, Toast.LENGTH_LONG).show();
Intent intent = new Intent (FindPeopleActivity.this, ProfileActivity.class);
intent.putExtra("visit_user_id", visit_user_id);
intent.putExtra("profile_image", model.getImage());
intent.putExtra("profile_name", model.getName());
startActivity(intent);
}
});
}
#NonNull
#Override
public FindFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup p, int viewType)
{
View view = LayoutInflater.from(p.getContext()).inflate(R.layout.contact_design,p,false);
FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
return viewHolder;
}
};
findFriendList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
{
TextView userNameTxt;
Button videoCallBtn;
ImageView profileImageView;
RelativeLayout cardView;
public FindFriendsViewHolder(#NonNull View itemView) {
super(itemView);
userNameTxt=itemView.findViewById(R.id.name_contact);
videoCallBtn=itemView.findViewById(R.id.call_btn);
profileImageView=itemView.findViewById(R.id.image_contact);
cardView=itemView.findViewById(R.id.card_view1);
videoCallBtn.setVisibility(View.GONE);
}
}
}
here is my contacts.java
package com.technolgiya.nitcalling11;
public class Contacts
{
String name,image,status,uid;
public Contacts() {
}
public Contacts(String name, String image, String status, String uid) {
this.name = name;
this.image = image;
this.status = status;
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
here is the activity_contacts.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_contacts"
android:layout_width="match_parent"
android:layout_height="50dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#android:color/white"
android:layout_centerVertical="true"
>
</TextView>
<ImageView
android:id="#+id/find_people_btn"
android:layout_width="38dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="12dp"
android:src="#drawable/find_people"
android:tint="#android:color/white"
>
</ImageView>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/contact_list"
android:layout_below="#+id/app_bar_layout_contacts"
android:orientation="vertical"
android:layout_above="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu" />
</RelativeLayout>
UPDATE:
I got:
Unable to start activity ComponentInfo{com.technolgiya.nitcalling11/com.technolgiya.nitcalling11.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
Please Try to start listening for Firebase data before setting the adapter, so
replace
findFriendList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
with
firebaseRecyclerAdapter.startListening();
findFriendList.setAdapter(firebaseRecyclerAdapter);
in your onStart() method.
UPDATE
Unable to start activity ComponentInfo{com.technolgiya.nitcalling11/com.technolgiya.nitcalling11.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
In ProfileActivity Replace
background_profile_view.findViewById(R.id.background_profile_view);
With
background_profile_view = findViewById(R.id.background_profile_view);
My intention is to get data from the run-time growing listview (as shown in below figures) which has Edittext and Spinners and send this data to remote server using json.
Am I on the correct path or there is another kind of method to be used for this.
I want to access Edittext and Spinner from the listview which is expanded at runtime.
My Problem is, whenever a new view is added, it overlaps on the previous view.
Instead, it should get added below the 1st view. (Solved this. See the edited code).
Another thing is how to access edittext & spinner from each view? I want to accept this data from user & save it in the device. How to do that? (in private void SaveUserData() {} method).
Below is my full code.
Edited on 20 Mar 2018
MainActivity.java
package tandel.amit.expandablelistview;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button buttonSave;
FloatingActionButton fab;
Adapter adapter;
int count = 0;
String[] Heading = {"User 1","User 2","User 3","User 4"};
String Name = "Name";
String ID = "ID";
String Gender = "Gender";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
buttonSave = findViewById(R.id.btnSave);
fab = findViewById(R.id.fabAdd);
buttonSave.setOnClickListener(onClickListener);
fab.setOnClickListener(onClickListener);
adapter = new Adapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(adapter);
AddView(count++);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSave:
SaveUserData(); // Save the data to Local Database
break;
case R.id.fabAdd:
AddView(count++); // Add new View in listview
break;
default:
break;
}
}
};
private void AddView(int i) {
DataProvider dataProvider = new DataProvider(Heading[i],Name,Gender,ID);
adapter.add(dataProvider);
adapter.notifyDataSetChanged();
}
private void SaveUserData() {
}
}
Adapter.java
package tandel.amit.expandablelistview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/*
* Created by Amit Tandel on 2/2/2018.
*/
public class Adapter extends ArrayAdapter{
List list = new ArrayList();
public Adapter(#NonNull Context context, int resource) {
super(context, resource);
}
#Override
public void add(#Nullable Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Nullable
#Override
public Object getItem(int position) {
return this.list.get(position);
}
static class DataHandler{
TextView Heading,Name,Gender,ID;
EditText UserName,UserID;
Spinner UserGender;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
DataHandler dataHandler = new DataHandler();
if (convertView == null){ // If row is empty, we need to create row
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.row_layout,parent,false);
dataHandler.Heading = convertView.findViewById(R.id.tvHeading);
dataHandler.Name = convertView.findViewById(R.id.tvName);
dataHandler.Gender = convertView.findViewById(R.id.tvGender);
dataHandler.ID = convertView.findViewById(R.id.tvID);
dataHandler.UserName = convertView.findViewById(R.id.etName);
dataHandler.UserID = convertView.findViewById(R.id.etID);
dataHandler.UserGender = convertView.findViewById(R.id.spGender);
convertView.setTag(dataHandler);
}else {
dataHandler = (DataHandler) convertView.getTag(); // If row already present then get that row
}
DataProvider dataProvider;
dataProvider = (DataProvider) this.getItem(position);
if (dataProvider != null) {
dataHandler.Heading.setText(dataProvider.getHeading());
dataHandler.Name.setText(dataProvider.getName());
dataHandler.Gender.setText(dataProvider.getGender());
dataHandler.ID.setText(dataProvider.getID());
}
return convertView;
}
}
DataProvider.java
package tandel.amit.expandablelistview;
/*
* Created by Amit Tandel on 2/2/2018.
*/
public class DataProvider {
private String Heading;
private String Name;
private String Gender;
private String ID;
public DataProvider(String Heading, String Name, String Gender, String ID) {
this.setHeading(Heading);
this.setName(Name);
this.setGender(Gender);
this.setID(ID);
}
public String getHeading() {
return Heading;
}
public void setHeading(String heading) {
Heading = heading;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="tandel.amit.expandablelistview.MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
n
<Button
android:id="#+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Save" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:id="#+id/fabAdd"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="#drawable/plus"
android:layout_marginBottom="80dp" />
</RelativeLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="User 1"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textSize="16sp" />
<EditText
android:id="#+id/etName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender"
android:textSize="16sp" />
<Spinner
android:id="#+id/spGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:textSize="16sp" />
<EditText
android:id="#+id/etID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="8dp"
android:background="#000000" />
</LinearLayout>
i want to use a checkbox on items in recyclerview i load data from the server 4 by 4 (after seeing the last item he add another 4 new item)but the problem when add new items the checked item change to another item idon't know why i want to save every item checked on SharedPreferences to use them on commande activity
some pictures :
enter image description here
after going dow i found another item checked and the firs one uchecked :
enter image description here
card.xml
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="119dp"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardCornerRadius="1dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt_idArticle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="ID Article : "
android:textColor="#android:color/black"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txt_reference1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/txt_des"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"
android:text="Reference : "
android:textColor="#android:color/background_dark"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txt_des1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txt_prix"
android:layout_alignBottom="#+id/txt_prix"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Prix : "
android:textColor="#android:color/background_dark"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txt_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/txt_prix1"
android:layout_toRightOf="#+id/txt_prix1"
android:text="TextView" />
<TextView
android:id="#+id/txt_prix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_prix1"
android:layout_toEndOf="#+id/txt_des1"
android:layout_toRightOf="#+id/txt_des1"
android:text="TextView" />
<TextView
android:id="#+id/txt_idArticle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/txt_idArticle1"
android:layout_toRightOf="#+id/txt_idArticle1"
android:text="TextView" />
<TextView
android:id="#+id/txt_reference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txt_reference1"
android:layout_alignBottom="#+id/txt_reference1"
android:layout_toEndOf="#+id/txt_reference1"
android:layout_toRightOf="#+id/txt_reference1"
android:text="TextView" />
<TextView
android:id="#+id/txt_prix1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txt_reference1"
android:layout_marginBottom="5dp"
android:text="Designation : "
android:textColor="#android:color/black"
android:textStyle="normal|bold" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="panier"
android:textSize="10sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
recherche_art.java
package com.example.bacha.pfe.activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.bacha.pfe.R;
import com.example.bacha.pfe.adapter.ArticleAdapter;
import com.example.bacha.pfe.classes.Article;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recherche_art extends AppCompatActivity {
private RecyclerView recyclerView ;
private GridLayoutManager gridLayoutManager;
private ArticleAdapter adapter ;
private List<Article> data_list ;
private String recherche_article;
private ImageButton btnrechArt ;
private EditText arech;
int d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recherche_art);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
btnrechArt = (ImageButton) findViewById(R.id.btnrechArt);
recherche_art.this.onRestart();
btnrechArt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d=0;
Log.d("PFE", String.valueOf(d));
arech= (EditText) findViewById(R.id.eTextRechArt);
data_list = new ArrayList<>();
recherche_article=arech.getText().toString();
load_article_from_server(d);
gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new ArticleAdapter(recherche_art.this,data_list);
recyclerView.setAdapter(adapter);
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
d=data_list.size();
load_article_from_server(d);
Log.d("PFE", String.valueOf(d));
}
}
});
}
private void load_article_from_server(final int id) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
#Override
protected Void doInBackground(Integer... Params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for(int i=0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null ;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
activity_recherche_art.xml
<TextView
android:text="Recherche Article :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|bold"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/recherche"
android:id="#+id/btnrechArt"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/eTextRechArt"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/eTextRechArt"
android:layout_below="#+id/textView"
android:layout_toLeftOf="#+id/btnrechArt"
android:layout_toStartOf="#+id/btnrechArt" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
article_view:cardUseCompatPadding="true"
article_view:cardElevation="5dp"
article_view:cardCornerRadius="5dp"
android:scrollbars="vertical"
android:layout_marginTop="18dp"
android:layout_below="#+id/btnrechArt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Article.java
package com.example.bacha.pfe.classes;
import java.io.Serializable;
/**
* Created by BACHA on 07/03/2017.
*/
public class Article implements Serializable {
private int id;
private String id_Article,Reference,Designation,PVTTC;
public Article(/*int id,*/ String id_Article, String reference, String designation, String PVTTC) {
// this.id = id;
this.id_Article = id_Article;
Reference = reference;
Designation = designation;
this.PVTTC = PVTTC;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getId_Article() {
return id_Article;
}
public void setId_Article(String id_Article) {
this.id_Article = id_Article;
}
public String getReference() {
return Reference;
}
public void setReference(String reference) {
Reference = reference;
}
public String getDesignation() {
return Designation;
}
public void setDesignation(String designation) {
Designation = designation;
}
public String getPVTTC() {
return PVTTC;
}
public void setPVTTC(String PVTTC) {
this.PVTTC = PVTTC;
}
}
ArticleAdapter.java
package com.example.bacha.pfe.adapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.bacha.pfe.R;
import com.example.bacha.pfe.activity.ArticleDetail;
import com.example.bacha.pfe.classes.Article;
import java.util.List;
import static com.example.bacha.pfe.R.layout.card;
/**
* Created by BACHA on 08/03/2017.
*/
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private static Context context ;
private List<Article> my_data ;
public ArticleAdapter(Context context, List<Article> my_data) {
this.context = context;
this.my_data = my_data;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(card,parent,false);
return new ArticleAdapter.ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.id_Article.setText(my_data.get(position).getId_Article());
holder.Reference.setText(my_data.get(position).getReference());
holder.Designation.setText(my_data.get(position).getDesignation());
holder.PVTTC.setText(my_data.get(position).getPVTTC());
holder.root.setTag(my_data.get(position));
}
#Override
public int getItemCount() {
return my_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView id_Article,Reference,Designation,PVTTC;
View root;
public ViewHolder(View itemView) {
super(itemView);
root = itemView;
root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Article art = (Article)v.getTag();
Intent intent = new Intent(context, ArticleDetail.class);
intent.putExtra("REFERENCE", art.getReference());
context.startActivity(intent);
}
});
id_Article=(TextView) itemView.findViewById(R.id.txt_idArticle);
Reference=(TextView) itemView.findViewById(R.id.txt_reference);
Designation=(TextView) itemView.findViewById(R.id.txt_des);
PVTTC=(TextView) itemView.findViewById(R.id.txt_prix);
}
#Override
public void onClick(View v) {
}
}
}
I am new to app development. I have two apps linked in firebase, one for sending image to Firebase Storage and image url to Firebase Realtime Database and one for retrieving all images with RecyclerView. The first app is running fine. But the second doesn't do anything. It only shows the layout arrivals.
Code of arrivals.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="gr.packagename.name.MainActivity"
android:background="#drawable/background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/logo"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrivals_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="144dp" />
</RelativeLayout>
Code of model.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="100dp"
android:layout_height="200dp"
android:id="#+id/clothesimage"
android:src="#drawable/men"/>
<TextView
android:layout_width="74dp"
android:layout_height="wrap_content"
android:id="#+id/categoryimage"
android:text="category"
android:layout_alignBottom="#+id/clothesimage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Code of arrivals.java
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class arrivals extends AppCompatActivity {
DatabaseReference mdatabase;
RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrivals);
mdatabase = FirebaseDatabase.getInstance().getReference().child("Images");
rv = (RecyclerView) findViewById(R.id.arrivals_list);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart(){
super.onStart();
FirebaseRecyclerAdapter<Images, ImageViewHolder> firebaseadapter = new FirebaseRecyclerAdapter<Images, ImageViewHolder>(
Images.class,
R.layout.list_content,
ImageViewHolder.class,
mdatabase
) {
#Override
protected void populateViewHolder(ImageViewHolder viewHolder, Images model, int position) {
viewHolder.setCategory(model.getCategory());
viewHolder.setImageurl(getApplicationContext(), model.getImageurl());
}
};
rv.setAdapter(firebaseadapter);
}
public static class ImageViewHolder extends RecyclerView.ViewHolder{
View mView;
public ImageViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setCategory(String category){
TextView categoryimg = (TextView) mView.findViewById(R.id.categoryimage);
categoryimg.setText(category);
}
public void setImageurl(Context ctx, String imageurl){
ImageView img = (ImageView) mView.findViewById(R.id.clothesimage);
Picasso.with(ctx).load(imageurl).into(img);
}
}
}
Code of Images.java
public class Images {
private String imageurl;
private String category;
public Images() {
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}