my question is is there a way to save the SpamCount of a ReciclerView in SharedPreferences?
I try to make a List that can change the view from a "List mode" to "Grid mode" and save that information in SharedPreferences so that the "List View" will remain after killing the app.
The example of what I am trying to do would be the following ...
example of what i try to do on button click
And the code that I try to change is the following ...
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static RecyclerView recyclerView;
private ReciclerAdapter reciclerAdapter;
ImageView viewGridS, viewListS;
SharedPreferences vSettings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recicler_main);
vSettings = this.getSharedPreferences("Vision", 0);
notesRecyclerView = findViewById(R.id.recycler_view);
notesRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
viewListS = findViewById(R.id.viewList);
viewGridS = findViewById(R.id.viewGrid);
#Override
public void onClick(View v) {
SharedPreferences vSettings = this.getSharedPreferences("Vision", 0);
SharedPreferences.Editor viEdit = vSettings.edit();
switch (v.getId())
{
case R.id.viewList:
viewGridS.setVisibility(View.GONE);
viewListS.setVisibility(View.VISIBLE);
viEdit.putString("Vision",listView());
viEdit.apply();
break;
case R.id.viewGrid:
viewListS.setVisibility(View.GONE);
viewGridS.setVisibility(View.VISIBLE);
listWiew();
viEdit.putString("Vision",gridView());
viEdit.apply();
break;
}
});
}
public int gridView(){
notesRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
return null;
}
public int listView(){
notesRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
return null;
}
}
When trying to start the Activity the RecyclerView does not show anything and the button does not work.
if someone could help me I would appreciate it in advance ... thanks
You have done couple of things wrong here.
Incorrect use of SharedPreferences
Adapter on RecycleView not setup
Manage LinearLayout and StaggeredGridLayoutManager
I will try to accomplished kind similar end result which you are looking for, a simple button which is switching layout on click. Code as follows -
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String LIST_VIEW = "List View";
private String GRID_VIEW = "Grid View";
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private StaggeredGridLayoutManager staggeredGridLayoutManager;
private SharedPreferences sharedPreferences;
private RecycleViewAdapter recycleViewAdapter;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Simple List setup for data to show in recycle view
List<String> stringList = new ArrayList<>();
stringList.add("Monday\nI'll ask her out.");
stringList.add("Tuesday");
stringList.add("Wednesday\nI'll take her to garden to talk.");
stringList.add("Thursday\nI'll give her a gift");
stringList.add("Friday");
stringList.add("Saturday");
stringList.add("Sunday\nI'll propose her. ");
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
recyclerView = findViewById(R.id.recycler_view);
recycleViewAdapter = new RecycleViewAdapter(this, stringList);
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
/*
* SharedPreferences value will be null for very first install of app
* ELSE SharedPreferences will have use last saved value
* */
String CURRENT_VIEW = sharedPreferences.getString("LAYOUT_TYPE", null);
if (CURRENT_VIEW == null) {
CURRENT_VIEW = LIST_VIEW;
sharedPreferences.edit().putString("LAYOUT_TYPE", LIST_VIEW).apply();
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(recycleViewAdapter);
} else
switchView(CURRENT_VIEW);
Button buttonPanel = findViewById(R.id.buttonPanel);
buttonPanel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sharedPreferences.getString("LAYOUT_TYPE", null).equals(LIST_VIEW))
switchView(GRID_VIEW);
else
switchView(LIST_VIEW);
}
});
}
/*
* Method will switch Layout on RecycleView and update new Adapter
* #param: Required Layout Type (LIST_VIEW or GRID_VIEW)
* */
private void switchView(String layoutTypeValue) {
if (layoutTypeValue.equals(LIST_VIEW))
recyclerView.setLayoutManager(linearLayoutManager);
else if (layoutTypeValue.equals(GRID_VIEW))
recyclerView.setLayoutManager(staggeredGridLayoutManager);
sharedPreferences.edit().putString("LAYOUT_TYPE", layoutTypeValue).apply();
recyclerView.setAdapter(recycleViewAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:padding="8dp"
android:layout_margin="16dp"
android:layout_gravity="end|bottom"
android:id="#+id/buttonPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:background="#android:color/holo_orange_dark"
android:text="Change Layout"/>
</FrameLayout>
RecycleViewAdapter.java
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.CustomViewHolder> {
private Context context;
private List<String> stringList;
public RecycleViewAdapter(Context context, List<String> stringList) {
this.context = context;
this.stringList = stringList;
}
#NonNull
#Override
public CustomViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new CustomViewHolder(LayoutInflater.from(context).inflate(R.layout.recycle_item_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull CustomViewHolder holder, int position) {
holder.textView.setText(stringList.get(position));
}
#Override
public int getItemCount() {
return stringList.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public CustomViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
}
}
}
recycle_item_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:layout_margin="8dp">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#android:color/holo_blue_dark"
android:textColor="#android:color/white"
android:textSize="24sp" />
</LinearLayout>
Above code will give you following result -
Happy Coding !
Related
I have created RecyclerView and showing data from JSON.
Issue I'm facing is, while Toast data is showing correctly, but in RecyclerView same data is not appear.
Here is code:
public class MainActivity extends AppCompatActivity {
private static final int NUM_LIST_ITEMS = 100;
private static final String TOKEN =
"71cf2d3dec294394e267fbb0bf28916f4198f8d6";
private CuloAdapter culoAdapter;
List<Hotel> lh = new ArrayList<>();
RecyclerView recyclerView;
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv_tiketapi);
LinearLayoutManager layout = new LinearLayoutManager(this);
layout.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layout);
CuloAdapter ar = new CuloAdapter(lh);
recyclerView.setAdapter(ar);
loadHotelLocation();
}
private void loadHotelLocation() {
final search apiService = ApiService.getService(search.class);
retrofit2.Call<SingleResult<Hotel>> call = apiService.findHotel(TOKEN,
"json");
call.enqueue(new Callback<SingleResult<Hotel>>() {
#Override
public void onResponse(retrofit2.Call<SingleResult<Hotel>> call,
Response<SingleResult<Hotel>> response) {
if (response.body().getDiagnostic().isSuccess()) {
//SingleResult.ResultList list =
response.body().getResults();
List<Hotel> mHotels = (List<Hotel>)
response.body().getResults().getResult();
lh.addAll(mHotels);
Toast.makeText(getApplicationContext(), "OK" +lh,
Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(retrofit2.Call<SingleResult<Hotel>> call,
Throwable t) {
}
});
}
RecyclerView Adapter :
public class CuloAdapter extends
RecyclerView.Adapter<CuloAdapter.ViewHolder> {
public CuloAdapter(List<Hotel> lh) {
this.items = lh;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public TextView txtSubTitle;
public ImageView imgIcon;
public ViewHolder(final View container) {
super(container);
txtTitle = (TextView) container.findViewById(R.id.airportName);
txtSubTitle = (TextView) container.findViewById(R.id.airportCode);
}
}
private List<Hotel> items;
public CuloAdapter(final Activity activity, List<Hotel> items) {
this.items = items;
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public void onBindViewHolder(CuloAdapter.ViewHolder holder, int position) {
Hotel item = items.get(position);
holder.txtTitle.setText(item.getLabel());
holder.txtSubTitle.setText(item.getId());
}
#Override
public CuloAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_flight, parent, false);
return new ViewHolder(v);
}
}
MainActivity 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="vertical"
tools:context="com.example.admin.exampletiketapi.MainActivity">
<EditText
android:id="#+id/et_find"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_tiketapi"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Item List XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/airportsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/airportName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Soekarno Hatta" />
<TextView
android:id="#+id/airportCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="20" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="#DDD"
android:visibility="visible" />
</LinearLayout>
In your code you are trying to set adapter before loading data.
CuloAdapter ar = new CuloAdapter(lh);
recyclerView.setAdapter(ar);
loadHotelLocation();
What i found is You are getting data in loadHotelLocation(), but trying to set adpter before that,
Call notifyDatasetChanged after lh.addAll(mHotels). And check for null's else you are heading for crash in case search results are zero/null
I tried to implement an onClick method to my card recyclerView. When I click the card nothing happens but when I click the gap between the cards the intended action is performed. This was confirmed when as I tested this by increasing the spacing between the cards. What did I do wrong and how do I change my code to fix this problem?
This is the Adapter
public class JobListRecyclerAdapter extends RecyclerView.Adapter<JobListRecyclerAdapter.jobViewHolder> {
private ArrayList<JobClass> jobClassList;
private static OnItemClickListener clickListener;
public JobListRecyclerAdapter(ArrayList<JobClass> rList) {
this.jobClassList = rList;
}
#Override
public int getItemCount() {
if(jobClassList == null){return 0;}
return jobClassList.size();
}
#Override
public jobViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.job_list_card, viewGroup, false);
return new jobViewHolder(itemView);
}
#Override
public void onBindViewHolder(jobViewHolder jobViewHolder, int position) {
JobClass ci = jobClassList.get(position);
jobViewHolder.jName.setText("Name: "+ci.getmJobNum());
jobViewHolder.jDate.setText("Quantity: "+ci.getmJobDate());
}
public void setClickListener(OnItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
public static class jobViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView jName;
protected TextView jDate;;
public jobViewHolder(View j) {
super(j);
jName = (TextView) j.findViewById(R.id.job_name);
jDate = (TextView) j.findViewById(R.id.job_due_date);
itemView.findViewById(R.id.job_card_layout).setOnClickListener(this); // bind the listener
}
#Override
public void onClick(View view) {
if (clickListener != null) {clickListener.onClick(view, getLayoutPosition());}
}
}
}
This is the Activity
public class JobList extends Fragment implements OnItemClickListener {
public JobList() {
}
public static JobList newInstance() {
JobList fragment = new JobList();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_job_list, container, false);
final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.job_list_recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(llm);
final ArrayList<JobClass> joblistclass = new ArrayList<>();
/*
// Create a new Adapter
final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1);
*/
// Get a reference to the todoItems child items it the database
//String userPath = ((GlobalData) getActivity().getApplication()).getUserPath() +"JOBS";
//final DatabaseReference myRef = database.getReference(userPath);
// final DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("USERS/04950F4AE53F80/JOBS");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference jobsRef = rootRef
.child("USERS")
.child("04950F4AE53F80")
.child("JOBS");
// Assign a listener to detect changes to the child items
// of the database reference.
// myRef.addValueEventListener(new ValueEventListener() {
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String jobno = ds.child("JOBOVERVIEW").child("jobNum").getValue(String.class);
JobClass jblst = ds.child("JOBOVERVIEW").getValue(JobClass.class);
joblistclass.add(jblst);
((GlobalData) getContext().getApplicationContext()).saveJobNum(jobno);
}
final JobListRecyclerAdapter adapterb = new JobListRecyclerAdapter(joblistclass);
recyclerView.setAdapter(adapterb);
adapterb.setClickListener(JobList.this);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
jobsRef.addListenerForSingleValueEvent(eventListener);
///to send to next page
Button nextPage = (Button) rootView.findViewById(R.id.addNewJob);
// Capture button clicks
nextPage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Start NewActivity.class
Intent myIntent = new Intent(getContext(), JobForm.class);
startActivity(myIntent);
}
});
return rootView;
}
#Override
public void onClick(View view, int position) {
ArrayList<String> nameArray = ((GlobalData) getActivity().getApplication()).getJobNums();
String name = nameArray.get(position);
((GlobalData) getActivity().getApplication()).setJobId(name);
Toast.makeText(getContext(), name,
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getContext(), Main2Activity.class);
startActivity(intent);
}
}
Card View 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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardUseCompatPadding="true"
android:id="#+id/job_card_layout"
android:layout_marginBottom="4dp"
android:clickable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:id="#+id/job_linear_layout"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="9dp"
android:clickable="true"
>
<TextView
android:id="#+id/job_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:clickable="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/job_due_date"
android:layout_toRightOf="#+id/job_name"
android:clickable="true"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Please use below xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="#+id/job_card_layout"
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_marginBottom="4dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:id="#+id/job_linear_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="9dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<TextView
android:id="#+id/job_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="20sp" />
<TextView
android:id="#+id/job_due_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/job_name" />
</LinearLayout>
Note: android:clickable="true" has to be removed from your xml file.
Also, use
j.setOnClickListener(this);
instead of
itemView.setOnClickListener(this);
You don't have itemView field in your adapter or viewHolder as I can see. Maybe jobViewHolder should look like this:
jName = (TextView) j.findViewById(R.id.job_name);
jDate = (TextView) j.findViewById(R.id.job_due_date);
j.findViewById(R.id.job_card_layout).setOnClickListener(this); // bind the listener
Maybe your child view is placed at foreground and take all interactive from user. Let try this code in your child view xml.
android:duplicateParentState="true"
Update jobViewHolder class
Change following line
itemView.findViewById(R.id.job_card_layout).setOnClickListener(this); // bind the listener
to
j.findViewById(R.id.job_card_layout).setOnClickListener(this); // bind the listener
OR
j.setOnClickListener(this); // bind the listener
NOTE: If this doesn't work then you are referring wrong id i.e. job_card_layout
Updated JobViewHolder.java
public static class jobViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView jName;
protected TextView jDate;;
public jobViewHolder(View j) {
super(j);
jName = (TextView) j.findViewById(R.id.job_name);
jDate = (TextView) j.findViewById(R.id.job_due_date);
itemView.setOnClickListener(this); // bind the listener
}
#Override
public void onClick(View view) {
if (clickListener != null) {clickListener.onClick(view, getLayoutPosition());}
}
}
Everytime we open the activity that bares the recycler adapter it fails to load on the first try. Exiting the activity and re-entering fixes the problem. Here is a gif for example. https://gyazo.com/32dc664dd427ef1129704a09861a3708
The Item i am spawning in:
<?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:card_view="http://schemas.android.com/tools"
android:id="#+id/show_chat_single_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="70dp"
app:cardBackgroundColor="#dcdada"
app:cardCornerRadius="0dp"
app:contentPadding="3dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/chat_persion_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/chat_persion_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:text="Name"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="#+id/chat_persion_image"
app:layout_constraintTop_toTopOf="#+id/chat_persion_image" />
<TextView
android:id="#+id/chat_persion_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="8dp"
android:text="Email"
app:layout_constraintLeft_toLeftOf="#+id/chat_persion_name"
app:layout_constraintTop_toBottomOf="#+id/chat_persion_name" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
My conversation layout:
<?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/msgs_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:theme="#style/AppTheme2"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/active_chats"
android:layout_width="match_parent"
android:layout_height="510dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="57dp">
</android.support.v7.widget.RecyclerView>
<include
android:id="#+id/include2"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
My Adapter:
public class ActiveChatConvo extends RecyclerView.Adapter<ActiveChatConvo.ViewHolder>
{
private ArrayList<User> mUsers;
private Context mContext;
public ActiveChatConvo(ArrayList<User> photos,Context dick) {
mUsers = photos;
mContext = dick;
}
private Context getContext() {
return mContext;
}
#Override
public ActiveChatConvo.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.chat_single_item, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Get the data model based on position
User user = mUsers.get(position);
// Set item views based on your views and data model
TextView name = holder.mItemName;
name.setText(user.getName());
TextView description = holder.mItemDescription;
description.setText(user.getEmail());
ImageView pic = holder.mItemImage;
Picasso.with(pic.getContext()).load(Uri.parse(user.getPic())).into(pic);
}
#Override
public int getItemCount() {
return mUsers.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mItemImage;
private TextView mItemName;
private TextView mItemDescription;
private LinearLayout layout;
final LinearLayout.LayoutParams params;
public ViewHolder(View v) {
super(v);
mItemImage = (ImageView) v.findViewById(R.id.chat_persion_image);
mItemName = (TextView) v.findViewById(R.id.chat_persion_name);
mItemDescription = (TextView) v.findViewById(R.id.chat_persion_email);
layout = (LinearLayout) itemView.findViewById(R.id.show_chat_single_item_layout);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Context context = itemView.getContext();
Intent showChatIntent = new Intent(context, ChatConversationActivity.class);
//showPhotoIntent.putExtra(PHOTO_KEY, mPhoto);
context.startActivity(showChatIntent);
}
}
}
My main class
public class ConnectionsActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mViewPager;
private TextView person_name,person_email;
private RecyclerView recyclerView;
private DatabaseReference mRef;
private LinearLayoutManager mLinearLayoutManager;
private ArrayList<User> users;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connections);
//Database
mRef = FirebaseDatabase.getInstance().getReference("Users");
mRef.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
users = new ArrayList<>();
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String name = postSnapshot.child("Name").getValue(String.class);
String email = postSnapshot.child("Email").getValue(String.class);
String pic = postSnapshot.child("image").getValue(String.class);
users.add(new User(name,email,pic));
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Recycler View
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
recyclerView.setAdapter(adapter);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
//VIEWS
toolbar = (Toolbar) findViewById(R.id.tToolbar);
if (toolbar != null)
setSupportActionBar(toolbar);
ImageView profileActivity = (ImageView) toolbar.findViewById(R.id.action_profile);
ImageView homeActivity = (ImageView) toolbar.findViewById(R.id.action_home);
profileActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent profileActivity = new Intent(ConnectionsActivity.this, ProfileActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
});
homeActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent home = new Intent(ConnectionsActivity.this, HomeActivity.class);
startActivity(home);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
});
}
}
So if the recyclerView Activity is the first activity I visit then it will not create the items.
Firebase is synchronize so it doesn't block the main thread of your application, that mean that your application continue executing, you need to notify the adapter after firebase finishing his job by using adapter.notifiyDatasetChanged() after the for loop
Replace this:
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
recyclerView.setAdapter(adapter);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
with this:
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
recyclerView.setAdapter(adapter);
You need to set adapter after you set LayoutManager.
In my case I managed to solve it I found a way to start FirebaseDatabase before opening the activity and added the cod
adapter.notifyDataSetChanged();
and then I started the firebase before opening it with this
FirebaseDatabase.getInstance().getReference().keepSynced(true);
You can use Boolean button to check if it is true adapter is made
I have a database of Users and I'm trying to use a RecyclerView to display every User entry I have. From my logs, I can tell that my RecyclerView isn't even being interacted with. Here is the class where I call it:
public class WelcomePage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_page);
FragmentManager manager = getFragmentManager();
RecyclerFragment mRecyclerFragment = new RecyclerFragment();
if(mRecyclerFragment == null) {
manager.beginTransaction()
.add(R.id.recycler, mRecyclerFragment)
.commit();
}
}
}
This is my holder class:
public class RecyclerHolder extends RecyclerView.ViewHolder{
private static final String TAG = "RecyclerHolder";
private TextView mFullName;
private TextView mBirthDate;
private TextView mHomeTown;
private TextView mBio;
User mUser;
RecyclerHolder(View itemView){
super(itemView);
mFullName = (TextView)itemView.findViewById(R.id.text_view_name);
mBirthDate = (TextView)itemView.findViewById(R.id.text_view_birthday);
mHomeTown = (TextView)itemView.findViewById(R.id.text_view_hometown);
mBio = (TextView)itemView.findViewById(R.id.text_view_bio);
public void bind(User user) {
mUser = user;
mFullName.setText(user.getFullName());
mBirthDate.setText(user.getBirthDate().toString());
mHomeTown.setText(user.getHomeTown());
mBio.setText(user.getBio());
}
}
Here's my adapter class:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerHolder> {
private ArrayList<User> mUsers;
public RecyclerAdapter(ArrayList<User> user) {
mUsers = user;
}
#Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.view_recycler, parent, false);
RecyclerHolder holder = new RecyclerHolder(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerHolder holder, int position){
holder.bind(mUsers.get(position));
}
#Override
public int getItemCount() {
return mUsers.size();
}
}
Here's my fragment class:
public class RecyclerFragment extends Fragment {
private RecyclerView mRecyclerView;
DBCursorWrapper db;
public RecyclerFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recycler, container, false);
mRecyclerView = (RecyclerView)view.findViewById(R.id.recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerAdapter adapter = new RecyclerAdapter(db.getUserList());
mRecyclerView.setAdapter(adapter);
return view;
}
}
Here is the XML for my holder:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_recycler">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
Here is my XML for my fragment:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.csc214.just4kas.project02.RecyclerFragment">
</android.support.v7.widget.RecyclerView>
Here is my XML for where I call my RecyclerView:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_layout_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.csc214.just4kas.project02.WelcomePage">
</FrameLayout>
The getUserList() has been tested individually and works perfectly!
Any help would be super appreciated! I just want a RecyclerView of all the contents in my database!! Thanks!
I think you may be using the wrong layout when you replace add it to the main activity. try (R.id.frame_layout_recycler_view) instead or R.id.recycler.
Or there might be a issue when use the database in this context.
I've got a RecyclerView which populates from an ArrayList. The output is a CardView layout.
In the Cardview, there are 2 buttons amongst other Views.
They only have to read the current value of a TextView, which by default is 1, and increase or decrease it.
The Arraylist contains 8 items.
When I run the app the UI works fine. Trouble is when I try to modify the value of the TextView.
The value is correctly increased and decreased on the CardView I'm working on, but ALSO the value is modified on another CardView. And in that second CardView, modifying its TextView value, also modifies the first one.
So, what am I doing wrong?
This is my Fragment:
public class Fragment_rosas extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_rosas,container,false);
RecyclerView recyclerview_rosas;
RecyclerView.Adapter adaptador_rv_rosas;
RecyclerView.LayoutManager lm_rosas;
List rosas = new ArrayList();
rosas.add(new Tropa(1,R.drawable.minibarbaro, getResources().getString(R.string.barbaro),7,1));
recyclerview_rosas = (RecyclerView) view.findViewById(R.id.recyclerView_tropasRosas);
recyclerview_rosas.setHasFixedSize(true);
lm_rosas = new LinearLayoutManager(getContext());
recyclerview_rosas.setLayoutManager(lm_rosas);
adaptador_rv_rosas = new AdaptadorTropa(rosas);
recyclerview_rosas.setAdapter(adaptador_rv_rosas);
return view;
}
}
And here the part of code on my Adapter:
#Override
public void onBindViewHolder(final TropaViewHolder viewHolder, int i) {
viewHolder.imagen.setImageResource(items.get(i).getImagen());
viewHolder.nombre.setText(items.get(i).getNombre());
viewHolder.maxnivel.setText(String.valueOf(items.get(i).getNivelMax()));
viewHolder.espacioencamp.setText((String.valueOf(items.get(i).getEspacioEnCamp())));
final String nombre = items.get(i).getNombre();
final int maxnivel = items.get(i).getNivelMax();
viewHolder.nivelmas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String niveltemp = viewHolder.nivel.getText().toString();
String nivelmaxtemp = viewHolder.maxnivel.getText().toString();
int nivel = Integer.parseInt(niveltemp);
int maxxnivel = Integer.parseInt(nivelmaxtemp);
int nuevonivel = nivel+1 ;
if (nuevonivel<=maxxnivel) {
viewHolder.txtv_nivel.setText(String.valueOf(nuevonivel));
}
}
});
My OnCreateViewHolder (nothing really happens here):
#Override
public TropaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cardview, viewGroup, false);
return new TropaViewHolder(v);
}
Here is the solution, as mentioned in the comment above, it addresses two problems:
1. positiontoValueMap - saves current value for each position
2. onclicklistener is passed to the ViewHolder in onCreateViewHolder
Adapter Class
public class MyAdapter extends RecyclerView.Adapter {
private Context context;
private List<String> dataList;
private Map<Integer, Integer> positionToValueMap = new HashMap<>();
public MyAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_item, null, false);
return new MyViewHolder(view, new OnRecyclerItemClickListener());
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((MyViewHolder) holder).onRecyclerItemClickListener.updatePosition(position);
((MyViewHolder) holder).position.setText("" + position);
((MyViewHolder) holder).title.setText(dataList.get(position));
int valueToDisplay = 1;
if(positionToValueMap.containsKey(position)) {
valueToDisplay = positionToValueMap.get(position);
} else {
positionToValueMap.put(position, valueToDisplay);
}
((MyViewHolder) holder).valueView.setText("value: " + valueToDisplay);
}
#Override
public int getItemCount() {
return dataList.size();
}
private class MyViewHolder extends RecyclerView.ViewHolder {
private OnRecyclerItemClickListener onRecyclerItemClickListener;
private TextView position;
private TextView title;
private TextView valueView;
public MyViewHolder(View itemView, OnRecyclerItemClickListener onRecyclerItemClickListener) {
super(itemView);
itemView.setOnClickListener(onRecyclerItemClickListener);
this.onRecyclerItemClickListener = onRecyclerItemClickListener;
this.position = (TextView) itemView.findViewById(R.id.position);
this.title = (TextView) itemView.findViewById(R.id.title);
this.valueView = (TextView) itemView.findViewById(R.id.value_view);
}
}
private class OnRecyclerItemClickListener implements View.OnClickListener {
private int position = -1;
public void updatePosition(int position) {
this.position = position;
}
#Override
public void onClick(View v) {
int oldValue = positionToValueMap.get(position); // get current value
oldValue++; // increment
positionToValueMap.put(position, oldValue); // save current value
notifyItemChanged(position); // update clicked view so that it picks up the new saved value from the positionToValueMap in onBindViewHolder
}
}
}
RecyclerView item layout
<TextView
android:id="#+id/position"
android:layout_width="30dp"
android:layout_height="50dp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#android:color/holo_green_light"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/title"
android:layout_width="50dp"
android:layout_height="50dp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#android:color/holo_green_dark"
android:layout_toRightOf="#id/position" />
<TextView
android:id="#+id/value_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#android:color/holo_green_light"
android:layout_toRightOf="#id/title"
android:layout_alignParentRight="true"/>
</RelativeLayout>
And Activity to test it out
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(new MyAdapter(getApplicationContext(), getSampleData()));
}
private static List<String> getSampleData() {
List<String> dataList = new ArrayList<>();
dataList.add("zero");
dataList.add("one");
dataList.add("two");
dataList.add("three");
dataList.add("four");
dataList.add("five");
dataList.add("six");
dataList.add("seven");
dataList.add("eight");
dataList.add("nine");
dataList.add("ten");
dataList.add("eleven");
dataList.add("twelve");
dataList.add("thirteen");
dataList.add("fourteen");
dataList.add("fifteen");
dataList.add("sixteen");
dataList.add("seventeen");
dataList.add("eighteen");
dataList.add("nineteen");
dataList.add("twenty");
return dataList;
}
}
activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"/>
</RelativeLayout>