I do not get any results from the recycler on Android studio - java

Well i think i got everything as they should be , i don't really know what i did wrong with the recycler but i still go error like "No adapter attached; skipping layout"
I NOTICED THAT ON UsersAdapter.java on the line "itemView.setOnClickListener(view -> onUserClickListener.onUserClicked(getAdapterPosition()));". getadapterposition is deprecated. Would that be the problem ?
Usersadapter.java
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserHolder> {
private final ArrayList<User> users;
private final Context context;
private final OnUserClickListener onUserClickListener;
public UsersAdapter(ArrayList<User> users, Context context, OnUserClickListener onUserClickListener) {
this.users = users;
this.context = context;
this.onUserClickListener = onUserClickListener;
}
interface OnUserClickListener{
void onUserClicked(int position);
}
#NonNull
#Override
public UserHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.user_holder,parent, false);
return new UserHolder(view);
}
#Override
public void onBindViewHolder(#NonNull UserHolder holder, int position) {
holder.txtUsername.setText(users.get(position).getUsername());
Glide.with(context).load(users.get(position).getProfile_img()).error(R.drawable.account_img).placeholder(R.drawable.account_img).into(holder.profile_thumbnail);
}
#Override
public int getItemCount() {
return users.size();
}
class UserHolder extends RecyclerView.ViewHolder{
TextView txtUsername ;
ImageView profile_thumbnail;
public UserHolder(#NonNull View itemView){
super(itemView);
itemView.setOnClickListener(view -> onUserClickListener.onUserClicked(getAdapterPosition()));
txtUsername = itemView.findViewById(R.id.txtUsername);
profile_thumbnail = itemView.findViewById(R.id.profile_thumbnail);
}
}
}
user_holder.xml
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:cardBackgroundColor="#color/Color_grey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="14dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp">
<androidx.cardview.widget.CardView
android:layout_width="70dp"
app:cardCornerRadius="180dp"
android:layout_height="70dp">
<ImageView
android:id="#+id/profile_thumbnail"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/txtUsername"
android:layout_width="wrap_content"
android:layout_marginStart="20dp"
android:layout_gravity="center"
android:textColor="#color/white"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
activity_friends.xml
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
FriendsActivity.java
public class FriendsActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<User> users;
private ProgressBar progressBar_connectivity;
private UsersAdapter userAdapter;
UsersAdapter.OnUserClickListener onUserClickListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
progressBar_connectivity = findViewById(R.id.progressBar_connectivity);
users = new ArrayList<>();
recyclerView = findViewById(R.id.recycler);
onUserClickListener = position -> Toast.makeText(FriendsActivity.this,"Tapped this User "+users.get(position).getUsername(),Toast.LENGTH_SHORT).show();
getUsers();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profile_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item){
if(item.getItemId() == R.id.menu_item_profile){
startActivity(new Intent(FriendsActivity.this, Profile.class));
}
return super.onOptionsItemSelected(item);
}
private void getUsers(){
FirebaseDatabase.getInstance().getReference("user").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
users.add(dataSnapshot.getValue(User.class));
}
userAdapter = new UsersAdapter(users, FriendsActivity.this,onUserClickListener);
recyclerView.setLayoutManager(new LinearLayoutManager(FriendsActivity.this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(userAdapter);
userAdapter.notifyDataSetChanged();
progressBar_connectivity.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
I did try to add on my code this one , i saw from an other topic-question , but it didn't help , i did even move the getUsers(); on any other caller except onCreate, still same results.
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter( adapter );

Related

E/RecyclerView: No adapter attached; skipping layout in fragment

im trying to get data from firestore. but those data don't appears in my fragment activity.
and there is this error: E/RecyclerView: No adapter attached; skipping layout
i can only see the background of frangment activy, where is the error?
SecondFragment.class
public class SecondFragment extends Fragment {
public static final String TAG = "FireLog";
RecyclerView mMainList;
FirebaseFirestore mFirestore;
CollectionReference mRef;
View view;
public List<Offices> officesList;
public OfficesListAdapter officesListAdapter;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.second_layout, container, false);
//Recycler View
mMainList = (RecyclerView) view.findViewById(R.id.main_list);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(getContext()));
mMainList.setAdapter(officesListAdapter);
mFirestore = FirebaseFirestore.getInstance();
officesList = new ArrayList<>();
officesListAdapter = new OfficesListAdapter(officesList);
mFirestore.collection("TICINO").addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, #javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null){
Log.d(TAG, "Error : " + e.getMessage());
}
for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
if(doc.getType() == DocumentChange.Type.ADDED){
Offices offices = doc.getDocument().toObject(Offices.class);
officesList.add(offices);
officesListAdapter.notifyDataSetChanged();
}
}
}
});
second_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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="#+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
OfficeListAdapter.class
public class OfficesListAdapter extends RecyclerView.Adapter<OfficesListAdapter.ViewHolder> {
public List<Offices> officesList;
public OfficesListAdapter(List<Offices> officesList){
this.officesList = officesList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_offices, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.nameText.setText(officesList.get(i).getNameOffices());
viewHolder.cambioText.setText(officesList.get(i).getCambio());
}
#Override
public int getItemCount() {
return officesList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameText;
public TextView cambioText;
View mView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mView=itemView;
nameText = (TextView) mView.findViewById(R.id.text_view_name_office);
cambioText = (TextView) mView.findViewById(R.id.text_view_cambio_offices);
}
}
}
Offices.class
public class Offices {
String name, cambio;
public Offices(){}
public Offices(String name, String cambio) {
this.name = name;
this.cambio = cambio;
}
public String getNameOffices() {
return name;
}
public void setNameOffices(String name) {
this.name = name;
}
public String getCambio() {
return cambio;
}
public void setCambio(String cambio) {
this.cambio = cambio;
}
}
list_offices.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:cardBackgroundColor="#ffffe8">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="#+id/text_view_name_office"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_alignParentLeft="true"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:id="#+id/text_view_cambio_offices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Description"
android:layout_below="#id/text_view_name_office"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Try to set the adapter after instantiating it. E.g.:
// Inside OnCreate
//Recycler View
mMainList = (RecyclerView) view.findViewById(R.id.main_list);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(getContext()));
mFirestore = FirebaseFirestore.getInstance();
officesList = new ArrayList<>();
officesListAdapter = new OfficesListAdapter(officesList);
// Here your adapter is initialised
mMainList.setAdapter(officesListAdapter);
Create your adapter object before set it on RecyclerView
// add below method in your adapter class
public void dataChanged(List<Offices> officesList){
this.officesList = officesList;
notifyDataSetChanged();// make sure dont forget to call this method
}
// create adapter object before set it on RecyclerView
LinearLayoutManager manager = new LinearLayoutManager(getContext());
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(getContext()));
officesListAdapter = new OfficesListAdapter(null);// add this line here
mMainList.setAdapter(officesListAdapter);
mFirestore = FirebaseFirestore.getInstance();
officesList = new ArrayList<>();
// and finally update data on your adapter from your firebase onEvent() method
for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
if(doc.getType() == DocumentChange.Type.ADDED){
Offices offices = doc.getDocument().toObject(Offices.class);
officesList.add(offices);
}
}
officesListAdapter.dataChanged(officesList);// update from here

Check user online or offline icons

I have chat app and i have fragment and show in it my friends , and currently i think to add online and offline feature next to user name in my friend fragment, I wrote the code and it working without problem , But i need to add icon online or offline next to The Full name of users, In layout i added the icons but i can't control these in code, I want if user online.. online icon set visibilty visible and offline icon set visibilty invisible and i want if user offline.. online icon set invisible and offline icon set visible.. this is my problem just!
Note: The icons is ImageView...
I wrote comment above my problems in fisrt class and in layout xml..
I have to class the first class(FriendsFragment):
public class FriendsFragment extends Fragment {
public static int setOnlineSta;
RecyclerView results_list;
DatabaseReference mUDB,mUDBSec;
FirebaseUser mCurrentUser;
FriendsAdapter friendsAdapter;
ArrayList<String> fullNameList,userStatusList, userIdList, profileImageList;
View mMainView;
public FriendsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_friends, container, false);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = mCurrentUser.getUid();
mUDB =
FirebaseDatabase.getInstance()
.getReference().child("Friends").child(current_ui
d);
mUDB.keepSynced(true);
mUDBSec = FirebaseDatabase.getInstance().getReference();
mUDBSec.keepSynced(true);
results_list = (RecyclerView) mMainView.findViewById(R.id.users_list);
results_list.setHasFixedSize(true);
results_list.setLayoutManager(new LinearLayoutManager(getActivity()));
results_list.addItemDecoration(new DividerItemDecoration(getActivity(),
LinearLayoutManager.VERTICAL));
fullNameList = new ArrayList<>();
userStatusList = new ArrayList<>();
userIdList = new ArrayList<>();
profileImageList = new ArrayList<>();
setAdapter();
return mMainView;
}
private void setAdapter() {
mUDB.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final String uid = snapshot.getKey();
DatabaseReference userRef = mUDBSec.child("Users").child(uid);
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String uidSec = uid;
userIdList.add(uidSec);
String full_name = dataSnapshot.child("name").getValue(String.class);
String user_status = dataSnapshot.child("status").getValue(String.class);
String profile_pic = dataSnapshot.child("image").getValue(String.class);
fullNameList.add(full_name);
userStatusList.add(user_status);
profileImageList.add(profile_pic);
if (dataSnapshot.hasChild("online"))
{
// My Problem Here
Boolean userOnline_Check = (Boolean)
dataSnapshot.child("online").getValue();
if (userOnline_Check == true)
{
}
if (userOnline_Check == false)
{
}
}
friendsAdapter = new FriendsAdapter(getActivity(), fullNameList,
userStatusList, userIdList, profileImageList);
results_list.setAdapter(friendsAdapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}}
Second Class(FriendsAdapter):
public class FriendsAdapter extends
RecyclerView.Adapter<FriendsAdapter.FriendViewHolder> {
Context context;
ArrayList<String> fullNameList,userStatusList, userIdList,profileImageList;
class FriendViewHolder extends RecyclerView.ViewHolder
{
ImageView profileImage;
ImageView active_emo, not_active_emo;
TextView full_name, user_status,user_id;
public FriendViewHolder(View itemView) {
super(itemView);
full_name = (TextView) itemView.findViewById(R.id.user_single_name);
user_status = (TextView) itemView.findViewById(R.id.username_friend);
user_id = (TextView) itemView.findViewById(R.id.user_single_id);
profileImage = (ImageView) itemView.findViewById(R.id.user_single_image);
active_emo = (ImageView) itemView.findViewById(R.id.active_state);
not_active_emo = (ImageView) itemView.findViewById(R.id.not_active_state);
}
}
public FriendsAdapter(Context context, ArrayList<String> fullNameList,
ArrayList<String> userStatusList, ArrayList<String> userIdList,
ArrayList<String> profileImageList) {
this.context = context;
this.fullNameList = fullNameList;
this.userStatusList = userStatusList;
this.userIdList = userIdList;
this.profileImageList = profileImageList;
}
#Override
public FriendsAdapter.FriendViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view =
LayoutInflater.from(context).inflate(R.layout.users_single_layout, parent,
false);
return new FriendsAdapter.FriendViewHolder(view);
}
#Override
public void onBindViewHolder(final FriendsAdapter.FriendViewHolder holder,
final int position) {
holder.full_name.setText(fullNameList.get(position));
holder.user_status.setText(userStatusList.get(position));
holder.user_id.setText(userIdList.get(position));
Glide.with(context).load(profileImageList.get(position)).into(holder.profile
Image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence options[] = new CharSequence[]{"Open Profile", "Send message"};
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if(i == 0){
Intent profileIntent = new Intent(context, UserProfile.class);
profileIntent.putExtra("USER_ID",String.valueOf(userIdList.get(position)));
context.startActivity(profileIntent);
}
if(i == 1){
Intent chatIntent = new Intent(context, Search.class);
chatIntent.putExtra("USER_ID",String.valueOf(userIdList.get(position)));
context.startActivity(chatIntent);
}
}
});
builder.show();
}
});
}
#Override
public int getItemCount() {
return fullNameList.size();
}}
My Layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_single_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:padding="2dp"
android:src="#drawable/profile_default" />
<LinearLayout
android:layout_width="233dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/user_single_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full name"
android:textColor="#color/bluePrim"
android:textSize="20sp" />
<TextView
android:id="#+id/username_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name" />
<TextView
android:id="#+id/user_single_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Id"
android:visibility="gone" />
</LinearLayout>
<-- the icons below !-->
<ImageView
android:id="#+id/active_state"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/active"
android:visibility="invisible" />
<ImageView
android:id="#+id/not_active_state"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/notactive"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
Thank you..

Populate ImageView from BaseAdapter (Swipecards)

I am attempting to use the Swipecards library (https://github.com/Diolor/Swipecards) to build a tinder-esqe application. I am using a BaseAdapter to populate a layout with two text views and an image view that will be provided to the main SwipeFlingAdapterView. While both of the text fields are populated, I cannot get the image to appear on the cards. I have tried this implementation with both an ArrayAdapter and a BaseAdapter and the results are the same.
The activity layout (deal_page_layout)
<FrameLayout
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_height="match_parent"
android:layout_width="match_parent">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe_fling_view"
app:rotation_degrees="10"
tools:context=".DealPage"
android:alpha="1.0"
app:max_visible="2"
app:min_adapter_stack="5"/>
</FrameLayout>
The layout being populated by the BaseAdapter (deal_card)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/deal_card_image">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/deal_card_title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:gravity="center"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/deal_card_description"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:gravity="center"
android:textSize="20dp"/>
</RelativeLayout>
BaseAdapter class
public class DealBaseAdapter extends BaseAdapter {
private Context context;
private List<GrubbyDeal> dealList;
private LayoutInflater li;
public DealBaseAdapter(Context context, LayoutInflater li, ArrayList<GrubbyDeal> dealList){
this.context = context;
this.dealList = dealList;
this.li = li;
}
#Override
public int getCount(){
return dealList.size();
}
#Override
public Object getItem(int position){
return dealList.get(position);
}
#Override
public long getItemId(int position){
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
//resuse a view if possible
if(convertView == null){
convertView = li.inflate(R.layout.deal_card,parent,false);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.deal_card_image);
viewHolder.title = (TextView) convertView.findViewById(R.id.deal_card_title);
viewHolder.desc = (TextView) convertView.findViewById(R.id.deal_card_description);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
GrubbyDeal curDeal = dealList.get(position);
viewHolder.img.setImageURI(curDeal.getImageUri());
viewHolder.title.setText(curDeal.getTitle());
viewHolder.desc.setText(curDeal.getDescription());
return convertView;
}
//view holder class to hold cached findViewByID results
private static class ViewHolder {
public ImageView img;
public TextView title;
public TextView desc;
}
And the main activity (DealPage)
public class DealPage extends Activity {
private ArrayList<GrubbyDeal> dealList;
private DealBaseAdapter dealAdapter;
SwipeFlingAdapterView flingContainer;
#Override
public void onCreate(Bundle sis){
super.onCreate(sis);
setContentView(R.layout.deal_page_layout);
//add some awesome cat deals to the adapter
dealList = new ArrayList<>();
for(int i=0; i < 5; i++){
GrubbyDeal tmp = new GrubbyDeal(i);
dealList.add(tmp);
}
//add another type of cat deal to the list
dealList.add(new GrubbyDeal());
dealAdapter = new DealBaseAdapter(this, getLayoutInflater(), dealList);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.swipe_fling_view);
flingContainer.setAdapter(dealAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
GrubbyDeal popped = dealList.remove(0);
dealList.add(popped);
dealAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
makeToast(DealPage.this, "Left!");
}
#Override
public void onRightCardExit(Object dataObject) {
makeToast(DealPage.this, "Right!");
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
dealList.add(new GrubbyDeal());
dealAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
}
});
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
makeToast(DealPage.this, "Clicked!");
}
});
}
}
Am I missing something obvious? Is there some vastly superior library that I should be using? Thanks,
Ian
I would recommend using Picasso to load images into your imageview.
Picasso.with(context).load(imgurl).into(viewHolder.img);
The problem was formatting. I was attempting to use
Uri.parse("android.resource://com.thepackage.theapp/R.drawable.cat4.jpg");
but wasn't getting a valid Uri back. So instead I am using resource ids with picasso and the card works great!

Simply expanding a recyclerview to show a TextView

I've explored many ways to achieve this. But I'm much confused now. I tried many Githubs libraries but got confused. Also tried this but I don't think it allows a textview to be shown in expanded view: http://bignerdranch.github.io/expandable-recycler-view/
WHAT I WANT: A simple Recyclerview fetching some text from server for different items. On each item click, a layout or view should be expanded and TextView should become visible with respective fetched text from the server.
I have a simple RecyclerView with code following:
DataAdapter.java
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<List> android_versions;
private Context context;
int i =0;
public DataAdapter(Context context,ArrayList<List> android_versions) {
this.context = context;
this.android_versions = android_versions;
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
}
#Override
public int getItemCount() {
return android_versions.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tv_android;
public ViewHolder(View view) {
super(view);
tv_android = (TextView)view.findViewById(R.id.tv_android);
}
}
List.java (Some functions aren't being used as of now)
public class List {
private String android_version_name;
private String descr;
private int android_image_url;
private String android_image_url1;
public String getAndroid_version_name() {
return android_version_name;
}
public String getDescr(){ return descr;}
public void setDescr(String descr) {
this.descr = descr;
}
public void setAndroid_version_name(String android_version_name) {
this.android_version_name = android_version_name;
}
public int getAndroid_image_url() {
return android_image_url;
}
public String getAndroid_image_url1() {
return android_image_url1;
}
public void setAndroid_image_url(int android_image_url) {
this.android_image_url = android_image_url;
}
public void setAndroid_image_url1(String android_image_url1) {
this.android_image_url1 = android_image_url1;
}
}
row_layout.xml
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp"
android:background="#drawable/custom_bg"
android:clickable="true"
android:translationZ="3dp"
android:elevation="1dp"
android:focusable="true">
<RelativeLayout
android:layout_marginLeft="0dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_bg">
<TextView
android:layout_marginTop="10dp"
android:textSize="20sp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_android"
android:textStyle="normal"
android:layout_centerVertical="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Work.java (It's a fragment)
String[] sublist={
"English",
"Hindi"
};
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
rcl = (RecyclerView) getActivity().findViewById(R.id.homeworklist);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
rcl.setLayoutManager(layoutManager);
dataAdapter = new DataAdapter(getActivity(), new ArrayList<>(prepareData()));
rcl.setAdapter(dataAdapter);
}
public ArrayList prepareData(){
ArrayList android_version = new ArrayList<>();
for(int i=0;i<sublist.length;i++){
List androidVersion = new List();
androidVersion.setAndroid_version_name(sublist[i]);
android_version.add(androidVersion);
break;
}
return android_version;
}

Unable to populate any Cards in RecyclerView, possible cause?

I am trying to populate CardView's inside a RecyclerView. Though I am able to log all the adapter values(to make sure they are non-empty) I can't populate any in the UI. Here is the Activity Code:
FoodActivity.class
public class FoodActivity extends AppCompatActivity
{
private RecyclerView foodView;
private List<Result> adapter_data;
private CustomPlacesAdapter adapter;
private LinearLayoutManager llm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food);
foodView = (RecyclerView)findViewById(R.id.foodRView);
adapter = new CustomPlacesAdapter(adapter_data);
adapter_data = new ArrayList<>();
llm = new LinearLayoutManager(this);
foodView.setLayoutManager(llm);
foodView.setAdapter(adapter);
doGetRequest("restaurants in los angeles airport");
}
private void doGetRequest(final String message)
{
ApiInterfacePlaces apiService =
ApiClientPlaces.getClient().create(ApiInterfacePlaces.class);
Call<PlacesPojo> call = apiService.getValues(message, Util.getKeyForPlaces());
call.enqueue(new Callback<PlacesPojo>()
{
#Override
public void onResponse(Call<PlacesPojo>call, Response<PlacesPojo> response)
{
try
{
Log.e("TAG",""+response.body().toString());
List<Result> response_res = response.body().getResults();
adapter_data = response_res;
adapter.notifyDataSetChanged();
}
catch (Exception e)
{
Toast.makeText(FoodActivity.this, "Check data connection", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<PlacesPojo> call, Throwable t) {
// Log error here since request failed
Log.e("FAILURE", t.toString());
}
});
}
}
Here is the code to the RecyclerView's adapter:
CustomPlacesAdapter.class
public class CustomPlacesAdapter extends RecyclerView.Adapter<CustomPlacesAdapter.HotelsViewHolder>
{
private DataHolder d2 = new DataHolder();
public class HotelsViewHolder extends RecyclerView.ViewHolder
{
private TextView hotelName;
private Typeface face;
private ImageView hotel_logo;
private Context mcontext;
HotelsViewHolder(View itemView)
{
super(itemView);
mcontext = itemView.getContext();
hotelName = (TextView)itemView.findViewById(R.id.hotelName);
face = Typeface.createFromAsset(itemView.getContext().getAssets(), "Fonts/Roboto-Regular.ttf");
hotelName.setTypeface(face);
hotel_logo = (ImageView)itemView.findViewById(R.id.logoOfHotel);
}
}
private static class DataHolder
{
List<Result> feeds;
}
public CustomPlacesAdapter(List<Result> feeds)
{
this.d2.feeds = feeds;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public HotelsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.food_item, viewGroup, false);
HotelsViewHolder pvh = new HotelsViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(HotelsViewHolder feedViewHolder, int i)
{
feedViewHolder.hotelName.setText(d2.feeds.get(i).getName());
Picasso.with(feedViewHolder.mcontext).load(d2.feeds.get(i).getIcon()).into(feedViewHolder.hotel_logo);
}
#Override
public int getItemCount()
{
if(d2.feeds!=null)
{
return d2.feeds.size();
}
else
{
return 0;
}
}
}
This is the CardView that I use:
food_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
app:cardCornerRadius="5dp"
android:layout_height="100dp"
card_view:cardUseCompatPadding="false"
android:id="#+id/cv">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoOfHotel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/hotelName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Cross checked many things, still unable to fix the issue, what is possibly causing this? Any help would be much appreciated.

Categories

Resources