I am trying to make calendar something like this:
It looks like crap because it has no styles/ proper margins (I'd look after that later), Never mind these things
It works perfectly on first run, But when I press previous or next button then instead of generating new view it gives output like the second Image:
Never mind the text between previous and next button, this is hard coded for test purpose (to adjust margin/padding)
Part marked with blue pen is added just below the previous view , I want previous views to be cleared when buttons are pressed. Here is my Activity:
/*
* Copyright (c) $today.year.Copyright goes here.
*/
package neppro.com.busapp.Adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import java.util.zip.Inflater;
import neppro.com.busapp.R;
import neppro.com.busapp.interfaces.DataCarrierForAdapter;
import neppro.com.busapp.model.BusSearchResultWrapper;
/**
* Created by RaazDk on 11/7/2015.
*/
public class DatePickerAdapter extends RecyclerView.Adapter<DatePickerAdapter.CalendarViewHolder>
implements DataCarrierForAdapter{
private List<String> mMonthDays;
private LayoutInflater layoutInflater;
public DatePickerAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
}
#Override
public CalendarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View CalendarSingleItem = layoutInflater
.inflate(R.layout.calendar_single_date_layout, parent, false);
CalendarViewHolder holder = new CalendarViewHolder(CalendarSingleItem);
return holder;
}
#Override
public void onBindViewHolder(CalendarViewHolder holder, int position) {
holder.CalendarSingleItem.setText(mMonthDays.get(position).toString());
}
#Override
public int getItemCount() {
return mMonthDays.size();
}
#Override
public void busSearchResultDataForAdapter(List<BusSearchResultWrapper.AvailableBus> busList,
String fromLocation, String toLocation) {
}
#Override
public void navMenuAdapterDataCarrier(Context context, RecyclerView recyclerView,
Object adapterInstance, String[] menuText,
String[] menuIcons) {
}
#Override
public void CalendarDataCarrier(List<String> monthDays) {
this.mMonthDays = monthDays;
}
public class CalendarViewHolder extends RecyclerView.ViewHolder {
TextView CalendarSingleItem;
public CalendarViewHolder(View itemView) {
super(itemView);
CalendarSingleItem = (TextView) itemView.findViewById(R.id.calendar_single_item);
}
}
}
Layout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="20"
android:layout_column="4"
android:id="#+id/journey_date_picker">
<TextView
android:id="#+id/current_month"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/ActiveBg"
android:layout_column="0"
android:gravity="center"
android:layout_row="0"
android:textStyle="bold"
android:textSize="40sp"
android:textColor="#color/color_white"
/>
<TextView
android:id="#+id/current_date"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/ActiveBg"
android:gravity="center"
android:layout_column="0"
android:layout_row="1"
android:textStyle="bold"
android:textSize="40sp"
android:textColor="#color/color_white"
/>
<TextView
android:id="#+id/current_year"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/ActiveBg"
android:layout_column="0"
android:layout_row="2"
android:gravity="center"
android:textStyle="bold"
android:textSize="40sp"
android:textColor="#color/color_white"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_column="0"
android:gravity="center"
android:layout_row="3"
android:orientation="horizontal">
<neppro.com.busapp.customView.CustomTextView
app:cstmFontFace="fontawesome-webfont.ttf"
android:id="#+id/prev_month"
android:textSize="30sp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="#string/icon_prev_month"
android:layout_marginLeft="#dimen/input_margintop_bottom"
android:layout_marginRight="#dimen/input_margintop_bottom"
android:layout_width="wrap_content"
android:textColor="#color/ActiveBg"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/current_day_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="09-aug-2014"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="#dimen/common_margin_padding_for_text"
android:layout_marginRight="#dimen/common_margin_padding_for_text"/>
<neppro.com.busapp.customView.CustomTextView
app:cstmFontFace="fontawesome-webfont.ttf"
android:id="#+id/next_month"
android:textSize="30sp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginLeft="#dimen/input_margintop_bottom"
android:layout_marginRight="#dimen/input_margintop_bottom"
android:layout_width="wrap_content"
android:textColor="#color/ActiveBg"
android:text="#string/icon_next_month"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/calendar_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</GridLayout>
And RecyclerView adapter:
/*
* Copyright (c) $today.year.Copyright goes here.
*/
package neppro.com.busapp.Adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import java.util.zip.Inflater;
import neppro.com.busapp.R;
import neppro.com.busapp.interfaces.DataCarrierForAdapter;
import neppro.com.busapp.model.BusSearchResultWrapper;
/**
* Created by RaazDk on 11/7/2015.
*/
public class DatePickerAdapter extends RecyclerView.Adapter<DatePickerAdapter.CalendarViewHolder>
implements DataCarrierForAdapter{
private List<String> mMonthDays;
private LayoutInflater layoutInflater;
public DatePickerAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
}
#Override
public CalendarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View CalendarSingleItem = layoutInflater
.inflate(R.layout.calendar_single_date_layout, parent, false);
CalendarViewHolder holder = new CalendarViewHolder(CalendarSingleItem);
return holder;
}
#Override
public void onBindViewHolder(CalendarViewHolder holder, int position) {
holder.CalendarSingleItem.setText(mMonthDays.get(position).toString());
}
#Override
public int getItemCount() {
return mMonthDays.size();
}
#Override
public void busSearchResultDataForAdapter(List<BusSearchResultWrapper.AvailableBus> busList,
String fromLocation, String toLocation) {
}
#Override
public void navMenuAdapterDataCarrier(Context context, RecyclerView recyclerView,
Object adapterInstance, String[] menuText,
String[] menuIcons) {
}
#Override
public void CalendarDataCarrier(List<String> monthDays) {
this.mMonthDays = monthDays;
}
public class CalendarViewHolder extends RecyclerView.ViewHolder {
TextView CalendarSingleItem;
public CalendarViewHolder(View itemView) {
super(itemView);
CalendarSingleItem = (TextView) itemView.findViewById(R.id.calendar_single_item);
}
}
}
Related
fragment_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
tools:context=".LeaderboardsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
row_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"?
android:layout_height="wrap_content"
android:background="#color/cream">
<TextView
android:id="#+id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="#font/gotham_bold"
android:text="#1"
android:textColor="#color/brown"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView7" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/index"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/menu_profile2" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="#font/gotham_bold"
android:text="Name of Winner"
android:textColor="#color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView7"
app:layout_constraintStart_toEndOf="#+id/imageView7"
app:layout_constraintTop_toTopOf="#+id/imageView7" />
<TextView
android:id="#+id/coins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/gotham_bold"
android:text="1230"
android:textColor="#color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/name" />
</android.constraintlayout.widget.ConstraintLayout>
LeaderBoardAdapter.java
package com.android.myquiz;
import android.content.Context;
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.android.myquiz.databinding.RowLeaderboardBinding;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class LeaderBoardAdapter extends
RecyclerView.Adapter<LeaderBoardAdapter.LeaderboardViewHolder> {
Context context;
ArrayList<User> users;
public LeaderBoardAdapter(Context context, ArrayList<User> users){
this.context = context;
this.users = users;
}
#NonNull
#NotNull
#Override
public LeaderboardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(context).inflate(R.layout.row_leaderboard, parent,
false);
return new LeaderboardViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull LeaderboardViewHolder holder, int position) {
User user = users.get(position);
holder.binding.name.setText(user.getName1());
holder.binding.coins.setText(String.valueOf(user.getCoins()));
holder.binding.index.setText(String.format("#%d", position+1));
}
#Override
public int getItemCount() {
return users.size();
}
public class LeaderboardViewHolder extends RecyclerView.ViewHolder {
TextView id;
RowLeaderboardBinding binding;
public LeaderboardViewHolder(#NonNull View itemView) {
super(itemView);
binding = RowLeaderboardBinding.bind(itemView);
}
}
}
LeaderboardFragments.java
package com.android.myquiz;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.myquiz.databinding.FragmentLeaderboardsBinding;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import org.w3c.dom.Document;
import java.util.ArrayList;
public class LeaderboardsFragment extends Fragment {
public LeaderboardsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentLeaderboardsBinding binding;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentLeaderboardsBinding.inflate(inflater, container,false);
FirebaseFirestore database = FirebaseFirestore.getInstance();
final ArrayList<User> users = new ArrayList<>();
final LeaderBoardAdapter adapter = new LeaderBoardAdapter(getContext(), users);
binding.recyclerView.setAdapter(adapter);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
database.collection("users")
.orderBy("coins", Query.Direction.DESCENDING).get().addOnSuccessListener(new
OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot snapshot : queryDocumentSnapshots){
User user = snapshot.toObject(User.class);
users.add(user);
}
adapter.notifyDataSetChanged();
}
});
return binding.getRoot();
}
}
this is my code I want to retrieve data from the firebase real-time database in recyclerView in fragments.
Okay, so looks like you are able to fetch the data from firebase, but are making a mistake in setting the data.
When you are calling adapter.notifyDataSetChanged(), you need to take care that you are passing the new users list, to the adapter.
You need to create a method inside your adapter class,
public void setUsersList(ArrayList<User> users) {
this.users = users;
notifyDatasetChanged();
}
and call it inside your fragment like, adapter.setUsersList(users), instead of calling adapter.notifyDataSetChanged().
My rows are not in the same size for some reason.I tried everything but it didn't work.By the way I'm very new to android.
I tried changing my row.xml but it did not work:
<?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:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="top">
<ImageView
android:id="#+id/imageIv"
android:layout_width="35dp"
android:layout_height="34dp"
android:background="#drawable/circulo_azul"
android:src="#drawable/transferenciablue" />
<TextView
android:id="#+id/titleTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="#id/imageIv"
android:layout_toRightOf="#id/imageIv"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
This is the activitymain.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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=".MainActivity"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:id="#+id/recyclerView"/>
</LinearLayout>
MyAdapter.java with onBindViewHolder:
package com.example.demoapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.content.Context;
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
ArrayList<Model> models; // this array list creates a list of arrays which parameters define in my model class
public MyAdapter(Context c, ArrayList<Model> models) {
this.c = c;
this.models = models;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,null); // this line inflate my row
return new MyHolder(view); // this will return my view to holder class
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); // here i is position
myHolder.mImaeView.setImageResource(models.get(i).getImg());
}
#Override
public int getItemCount() {
return models.size();
}
}
MyHolder class:
package com.example.demoapp;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyHolder extends RecyclerView.ViewHolder {
ImageView mImaeView;
TextView mTitle;
public MyHolder(#NonNull View itemView) {
super(itemView);
this.mImaeView = itemView.findViewById(R.id.imageIv);
this.mTitle = itemView.findViewById(R.id.titleTv);
}
}
What do I need to change to make them appear proportional?Thanks
make sure that you will write this type of code in BindViewHolder:
#Override
public folder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.your_layout_name,parent,false);
return new viewHolderClass(view);
}
some time width cannot appear properly because viewGroup cannot use
Change android:layout_gravity="top" to android:layout_gravity="center"
I am using firestore recyclerview, but the items is not displayed on the app. I don't know what the problem is. I used is t before in other projects but still i don't get the mistake I made.
Display Fragment:
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import butterknife.BindView;
import butterknife.ButterKnife;
public class HospitalFragment extends Fragment {
#BindView(R.id.hospitalList)
RecyclerView hospitalList;
CollectionReference medcenters;
FirebaseFirestore ref;
HospitalAdapter hospitalAdapter;
private String TAG="TAG";
public HospitalFragment() {
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_hospital, container, false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.e(TAG, "onViewCreated: "+hospitalList);
rvSetUp();
dbSetup();
}
private void dbSetup() {
ref=FirebaseFirestore.getInstance();
medcenters=ref.collection("MedicalCenters");
Query query=medcenters.orderBy("name");
FirestoreRecyclerOptions<RVCell> options=new FirestoreRecyclerOptions.Builder<RVCell>()
.setQuery(query,RVCell.class)
.build();
hospitalAdapter=new HospitalAdapter(options,getContext());
hospitalList.setAdapter(hospitalAdapter);
}
private void rvSetUp() {
hospitalList.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getContext());
hospitalList.setLayoutManager(layoutManager);
}
#Override
public void onStart() {
super.onStart();
hospitalAdapter.startListening();
}
#Override
public void onStop() {
super.onStop();
hospitalAdapter.stopListening();
}
}
Adapter. This is the adapter which extends recyclerview Adapter :
import android.content.Context;
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.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
public class HospitalAdapter extends FirestoreRecyclerAdapter<RVCell,HospitalAdapter.ViewHolder> {
Context context;
public HospitalAdapter(#NonNull FirestoreRecyclerOptions<RVCell> options, Context context){
super(options);
this.context=context;
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull RVCell model)
{
holder.location.setText(model.getLocation());
holder.title.setText(model.getMCName());
holder.phone.setText(model.getPhno());
holder.time.setText(model.getTime());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.rv_cell, parent, false);
return new ViewHolder(v);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title,location,time,phone;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
location=itemView.findViewById(R.id.location);
time=itemView.findViewById(R.id.time);
phone=itemView.findViewById(R.id.phno);
}
}
}
Cell. This is the POJO class :
public class RVCell {
String MCName,location,time,phno,picurl,locurl;
public String getMCName() {
return MCName;
}
public void setMCName(String MCName) {
this.MCName = MCName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPhno() {
return phno;
}
public void setPhno(String phno) {
this.phno = phno;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
public String getLocurl() {
return locurl;
}
public void setLocurl(String locurl) {
this.locurl = locurl;
}
}
Main Layout. RecyclerView Layout :
<FrameLayout 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=".HospitalFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hospitalList"/>
</FrameLayout>
Cell layout. This is the layout used to diplay the items in the recycler view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Location"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/container"
android:background="#color/card_bg_color" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:background="#drawable/card_bg"
android:paddingBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_alignStart="#+id/title"
android:layout_alignLeft="#id/title"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:text="Location:"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/location"
android:layout_alignStart="#+id/location"
android:layout_alignLeft="#id/location"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:text="Time:"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/phno"
android:layout_width="333dp"
android:layout_height="wrap_content"
android:layout_below="#id/location"
android:layout_alignStart="#+id/time"
android:layout_alignLeft="#id/time"
android:layout_marginStart="-2dp"
android:layout_marginLeft="-2dp"
android:layout_marginTop="34dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:text="Phone:"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:text="Simple Title Text "
android:textColor="#color/colorPrimary"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:id="#+id/img_user"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toStartOf="#+id/container"
app:layout_constraintStart_toStartOf="#+id/container"
app:layout_constraintTop_toTopOf="#+id/container"
app:srcCompat="#mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
This the code. I searched a lot I did not find any mistakes. I also used the listeners of the recycler adapter. If any body can help it would be nice.
Which information has your TAG?
It has to have Name of your Activity, try this:
private static final String TAG = „HospitalFragment“;
I am beginner Android Developer. I want to create an Android Page which will dynamically display Three things 1. Question No. 2. Your Answer 3. Right Answer. How can I do this? It should look like this and can grow dynamically
What about something like this (Uses a listview):
Mainactivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Question> questionArrayList;
private void fillList(){
questionArrayList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
questionArrayList.add(new Question(i,"myans","yourans"));
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillList();
ListView mlIstView = (ListView)findViewById(R.id.mListview);
QuestionAdapter adapter = new QuestionAdapter(this,R.layout.listviewlayout,questionArrayList);
mlIstView.setAdapter(adapter);
}
}
Question.java
public class Question {
private int numberOfQuestion;
private String yourAnswer;
private String correctAnswer;
public Question(int numberOfQuestion, String yourAnswer, String correctAnswer) {
this.numberOfQuestion = numberOfQuestion;
this.yourAnswer = yourAnswer;
this.correctAnswer = correctAnswer;
}
public int getNumberOfQuestion() {
return numberOfQuestion;
}
public String getYourAnswer() {
return yourAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
QuestionAdapter
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by waro on 6/4/17.
*/
public class QuestionAdapter extends ArrayAdapter<Question> {
private ArrayList<Question> arrayList;
public QuestionAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<Question> objects) {
super(context, resource, objects);
arrayList = objects;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout,parent,false);
}
Question question = arrayList.get(position);
TextView questiontv = (TextView)convertView.findViewById(R.id.question);
TextView yourans = (TextView)convertView.findViewById(R.id.yourans);
TextView correctans = (TextView)convertView.findViewById(R.id.correctans);
questiontv.setText(Integer.toString(question.getNumberOfQuestion()));
yourans.setText(question.getYourAnswer());
correctans.setText(question.getCorrectAnswer());
return convertView;
}
}
activity_mail.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="com.suprentive.waro.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:id="#+id/mLinearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Your answer"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Right answer"
android:layout_weight="1"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mListview"
android:layout_below="#id/mLinearLayout">
</ListView>
</RelativeLayout>
listviewlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/question"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/yourans"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/correctans"
android:layout_weight="1"/>
</LinearLayout>
Result
Hopefully this helps you out.
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) {
}
}
}