I'm trying to implement recycler view in my android app. The data to be shown is got from a server, the data includes a timestamp that should also be displayed.
The code that is setting up the data with the recycler view is as follows:
try{
JSONArray jsonArray = new JSONArray(response);
if(jsonArray.length()!=0) {
TimeStamp = new String[jsonArray.length()];
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
TimeStamp[i] = jsonObject.getString("TimeStamp1");
int ij = TimeStamp[i].indexOf('T');
String s = TimeStamp[i].substring(0,ij-1);
String s2 = TimeStamp[i].substring(ij+1,TimeStamp[i].length()-1);
sb.append(s+"\n"+s2+"\n");
String mod = s+"\n"+s2+"\n";
dataset.add(new DataEntryModel(1,mod,10,1));
}
CustomAdapter myAdapter = new CustomAdapter(dataset);
recyclerView.setAdapter(myAdapter);
resultView.setText(sb.toString());
}else{
}
}catch (Exception e){
sb.append(e.toString());
resultView.setText(sb.toString());
}
As you can see from the screenshot for some reason the recycler view is not displaying the full string. I displayed the string in a textview above the button. That seems to be working fine. What is causing the recycler view to not render the items properly. If I try to render s2 first then the time until the the second : is displayed.
The layout for the card :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:clickable="true"
card_view:cardElevation="1dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textBlack"
android:text="UserName"
android:id="#+id/userNametext"
android:layout_alignBaseline="#+id/userName"
android:layout_alignBottom="#+id/userName"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp" />
<TextView
android:text="Loc of userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:textColor="#color/textBlack"
android:id="#+id/userName"
android:layout_toLeftOf="#+id/userNametext"
android:layout_marginTop="8dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:text="Time Of Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Time_of_Entry"
android:layout_marginStart="15dp"
android:textColor="#color/textBlack"
android:layout_below="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="50dp" />
<TextView
android:text="Loc of Time"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textColor="#color/textBlack"
android:id="#+id/timestamp_of_user"
android:layout_alignBaseline="#+id/Time_of_Entry"
android:layout_alignBottom="#+id/Time_of_Entry"
android:layout_alignStart="#+id/userName"
android:layout_marginStart="14dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The adapter for the recycler view is
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
ArrayList<DataEntryModel> dataSet;
CardView cards;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView UserName, TimeStamp;
public MyViewHolder(View itemView){
super(itemView);
this.UserName = (TextView)itemView.findViewById(R.id.userName);
TimeStamp = (TextView)itemView.findViewById(R.id.timestamp_of_user);
cards = (CardView)itemView.findViewById(R.id.card_view);
}
#Override
public void onClick(View view) {
}
}
public CustomAdapter(ArrayList<DataEntryModel> data){
dataSet = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_fragment,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TextView userName = holder.UserName;
TextView TimeStamp = holder.TimeStamp;
userName.setText(SingletonDataClass.SharedPrefsUserNameForSession);
TimeStamp.setText(dataSet.get(position).getTimeStamp());
}
#Override
public int getItemCount() {
return dataSet.size();
}
}
What is the reason for this odd behavior of the recycler view? How can I fix this problem?
Related
I'm trying to put a RecyclerView inside a CardView and create items dynamically.
Obviously, in XML, RecyclerView is set inside CardView tag, but as you can see picture, RecyclerView item is created outside CardView.
(RecyclerView's items are the parts expressed as kg and set in the photo. And CardView is also an item of another RecyclerView.)
What's the problem?
XML
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="TEST"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/light_blue_400"/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/ll1">
<com.google.android.material.button.MaterialButton
android:id="#+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="4dp"
style="#style/RoutineButtonStyle"
android:text="DELETE"
app:icon="#drawable/ic_delete"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="4dp"
android:layout_marginEnd="8dp"
style="#style/RoutineButtonStyle"
android:text="ADD"
app:icon="#drawable/ic_add"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/ll2"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Let me know if you need any other code.
Here is the Activity of your add/delete CardView:
//Item List if you want to add one or more Items
private List<MyItem> myItems = new ArrayList<>();
private Adapter smallCardAdapter;
private int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.rv);
//Creates a new Object in the Adapter.class
smallCardAdapter = new Adapter();
recyclerView.setAdapter(smallCardAdapter);
CardView cardView = findViewById(R.id.cardView);
MaterialButton delete = findViewById(R.id.delete);
MaterialButton add = findViewById(R.id.add);
//Add onClickListener
add.setOnClickListener(v -> {
try {
size = myItems.size();
//if the value is 0, a new item is created
//if you want to delete the card after 0 than you have to change the value to 0
if(size == 0){
//Creates a new Object in the MyItem.class
myItems.add(new MyItem("Set","1kg"));
}else{
MyItem myItem = myItems.get(0);
//Replace kg with "" to make a Integer
String secondString = myItem.getSecondString().replace("kg","");
Integer value = Integer.valueOf(secondString);
value++;
myItem.setSecondString(value+"kg");
myItems.set(0,myItem);
}
updateAdapter();
}catch (Exception e){
Log.w("MainActivity","Add Button OnClickListener");
e.printStackTrace();
}
});
//Delete onClickListener
delete.setOnClickListener(v->{
try {
size = myItems.size();
//If the value is 0 then canceled to avoid errors
if(size == 0){
return;
}
MyItem myItem = myItems.get(0);
//Replace kg with "" to make a Integer
String secondString = myItem.getSecondString().replace("kg","");
Integer value = Integer.valueOf(secondString);
//if the value is one or lower
if(value <= 1){
//The card will deleted after updateAdapter();
myItems.clear();
}else{
value--;
myItem.setSecondString(value+"kg");
myItems.set(0,myItem);
}
updateAdapter();
}catch (Exception e){
Log.w("MainActivity","Delete Button OnClickListener");
e.printStackTrace();
}
});
}
private void updateAdapter(){
try {
smallCardAdapter.setItems(myItems);
}catch (Exception e){
Log.w("MainActivity","updateAdapter");
e.printStackTrace();
}
}
The custom Item called MyItem:
public class MyItem {
String firstString;
String secondString;
public MyItem(String firstString,String secondString){
this.firstString = firstString;
this.secondString = secondString;
}
public String getFirstString() {
return firstString;
}
public void setFirstString(String firstString) {
this.firstString = firstString;
}
public String getSecondString() {
return secondString;
}
public void setSecondString(String secondString) {
this.secondString = secondString;
}
}
The Adapter.java creates a smallCard:
public class Adapter extends RecyclerView.Adapter<Adapter.SmallCardHolder> {
private List<MyItem> items = new ArrayList<>();
#NonNull
#Override
public SmallCardHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// I created a new smallcard for the CardView
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.smallcard, parent, false);
SmallCardHolder smallCardHolder = new SmallCardHolder(itemView);
return smallCardHolder;
}
#Override
public void onBindViewHolder(#NonNull final SmallCardHolder holder, int position) {
//get the current item of the position
MyItem myItem = items.get(position);
String firstString = myItem.getFirstString();
String secondString = myItem.getSecondString();
holder.textViewSet.setText(firstString);
holder.textViewkg.setText(secondString);
}
#Override
public int getItemCount () {
return items.size();
}
public void setItems(List <MyItem> items) {
this.items = items;
notifyDataSetChanged();
}
class SmallCardHolder extends RecyclerView.ViewHolder {
//Here are the TextView's of the smallcard (smallcard.xml)
private TextView textViewSet;
private TextView textViewkg;
public SmallCardHolder(#NonNull View itemView) {
super(itemView);
textViewSet = itemView.findViewById(R.id.set);
textViewkg = itemView.findViewById(R.id.kg);
}
}
}
Create a new layout with name called smallcard.xml for the Adapter.java:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"
android:layout_centerVertical="true"
android:textSize="18sp"
android:layout_marginStart="5dp"
android:textColor="?attr/colorPrimary"
android:textStyle="bold"/>
<TextView
android:id="#+id/kg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kg"
android:layout_toRightOf="#id/set"
android:layout_marginLeft="55dp"
android:layout_centerVertical="true"
android:textColor="?attr/colorPrimary"
android:textSize="16sp"
android:layout_marginRight="10dp"
android:maxLines="1"/>
</RelativeLayout>
</RelativeLayout>
In your XML you have to give the CardView a name. I called it cardView. Here is the code android:id="#+id/cardView"
Result:
You will find more information as comments in the code.
I am making an android cv app but I want to implement the UI shown in the screenshot.
screenshot of ui I want
below current UI from real device
current ui
The XML layout where I have implemented my UI, which consists of an ImageView and some TextViews which shows subjects. I have implemented all the tasks but UI is not showing how I want it to show.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<ImageView
android:id="#+id/educationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/education_information"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/education_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="#string/education_information"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="#+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_duration"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/institution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_institution"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/degree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_degree"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<Space
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/subjectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/university_subjects"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/subjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/university_subjects"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<include
layout="#layout/subject_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/subjects"
android:layout_marginTop="60dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I have created another adapter and created dummy data in subjectivity
below adapter class
public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below subject XML where I have hosted RecyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
below subject_list.xml where I have host items
<?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"
android:background="#color/colorBlust"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:padding="5dp"
android:src="#drawable/computer_science"
android:layout_marginStart="10dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/computers_science"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#color/colorWhite" />
</LinearLayout>
I have created fake data in order to host other images and texts
below fakeModel class
public class FakeData {
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSubjects() {
return subjects;
}
public void setSubjects(String subjects) {
this.subjects = subjects;
}
String image;
String subjects;
}
below adapter class where I have extended with RecyclerView
public class SubjectAdapter extends RecyclerView.Adapter {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below My Subject class where I have implemented fake images and data
public class SubjectActivity extends Activity {
List<FakeData> fakeData;
int [] subjectImage = {R.drawable.computer_science,
R.drawable.data_structure,
};
ListView list;
String[] subjectText = {
"Computer Science",
"Data Structure",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subject);
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.list);
SubjectAdapter adapter = new SubjectAdapter(SubjectActivity.this, subjectImage,
subjectText, fakeData);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
}
}
This looks like you only have used Android Studio's drag and drop feature to position the UI elements. The problem is, Android Studio shows those UI elements in a generic device, which wont match all devices. When I was starting off with Android, this document helped me a lot to understand how elements in the UI must be placed.
https://developer.android.com/studio/write/layout-editor
You XML is malformed. Your LinearLayout orientation is wrong:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
It should be vertical.
Then validate its content. You have multiple RelativeLayout with orientation attribute.
It should be LinearLayout instead. RelativeLayouts don’t have orientation.
Besides those errors, your layout is very complex and have a deep hierarchy. This will lead to performance issues. My suggestion to you is to learn how to use ConstraintLayout.
The learning curve is a bit high, but it will be worth it!
I'm using a CardView Layout in a RecyclerView, JSON will load data and fill the adapter. My layout includes a ImageView, a TextView and everything is fine.
This is the Adapter class (UPDATED):
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final int TYPE_HEADER = 4;
private static final int TYPE_ITEM = 1;
Context context;
List<CarData> getCarData; // getDataAdapter
ImageLoader imageThumbLoader;
public RecyclerViewAdapter(List<CarData> getCarData, Context context){
super();
this.getCarData = getCarData;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
// if (!isPositionHeader(position)) {
CarData getCarData1 = getCarData.get(position - 1);
imageThumbLoader = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageThumbLoader.get(getCarData1.getImageThumb(),
ImageLoader.getImageListener(
Viewholder.imageThumb,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.imageThumb.setImageUrl(getCarData1.getImageThumb(), imageThumbLoader);
Viewholder.titleName.setText(getCarData1.getTitleName());
Viewholder.doorName.setText(Html.fromHtml("<b>Số cửa:</b> " + getCarData1.getDoorName()));
Viewholder.seatName.setText(Html.fromHtml("<b>Số ghế:</b> " + getCarData1.getSeatName()));
Viewholder.cityName.setText(Html.fromHtml(getCarData1.getCityName()));
Viewholder.districtName.setText(Html.fromHtml(getCarData1.getDistrictName()));
// }
}
public int getDataAdapter() {
return getCarData == null ? 0 : getCarData.size();
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
#Override
public int getItemCount() {
return getDataAdapter(); // + 1
}
private boolean isPositionHeader(int position) {
return position == 0;
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView titleName;
public NetworkImageView imageThumb;
public TextView doorName;
public TextView seatName;
public TextView cityName;
public TextView districtName;
public ViewHolder(View itemView) {
super(itemView);
titleName = (TextView) itemView.findViewById(R.id.titleName);
imageThumb = (NetworkImageView) itemView.findViewById(R.id.imageThumb);
doorName = (TextView) itemView.findViewById(R.id.doorName);
seatName = (TextView) itemView.findViewById(R.id.seatName);
cityName = (TextView) itemView.findViewById(R.id.cityName);
districtName = (TextView) itemView.findViewById(R.id.districtName);
}
}
}
My layout:
<?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"
android:id="#+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="3dp"
card_view:contentPadding="3dp"
card_view:cardCornerRadius="3dp"
card_view:cardMaxElevation="3dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/imageThumb"
android:layout_width="400dp"
android:layout_height="230dp"
android:src="#mipmap/ic_launcher"
android:gravity="top"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Image View"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageThumb"
android:layout_toEndOf="#+id/imageThumb"
android:layout_marginLeft="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/doorName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/seatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/cityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/districtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<TextView
android:id="#+id/checkStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="KIỂM TRA TÌNH TRẠNG XE"
android:textColor="#color/color_primary_green_dark"
android:textStyle="bold"
android:textSize="12dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/textView_item32"
android:layout_toEndOf="#+id/textView_item32"
android:layout_marginLeft="20dp"/>
</LinearLayout>
But when I load data for the first time, there is a redundant item. Look at the following attachment:
The exception has happened when updated code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.michalz.hideonscrollexample, PID: 9794
java.lang.ArrayIndexOutOfBoundsException: length=18; index=-1
at java.util.ArrayList.get(ArrayList.java:310)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter$override.onBindViewHolder(RecyclerViewAdapter.java:54)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter$override.access$dispatch(RecyclerViewAdapter.java)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:0)
at pl.michalz.hideonscrollexample.RecyclerViewAdapter.onBindViewHolder(RecyclerViewAdapter.java:20)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
How do I remove this redundant item in the CardView?
You have + 1 in your overridden getItemCount() method. It causes that you have one extra item in your list.
Then you have the method isPositionHeader, which you call in the onBindViewHolder method to find out, if the position is a header position. You are filling your cardview with data, if it is not the header position. But if it is the header position, you just skip it. So the first item, which is blank, is the header using the same cardview, as the other items, in fact.
Try to remove the + 1 and this condition if (!isPositionHeader(position)).
I am getting a Json output from the server.
This is my Json look like
{
"feed": [
{
"order_no": "70000004",
"quotation_no": "abc004a"
},
{
"order_no": "70000003",
"quotation_no": "abc003a"
},
{
"order_no": "70000001",
"quotation_no": "abc001a"
},
{
"order_no": "70000001",
"quotation_no": "abc001b"
}
]
}
I know how to populate this in a recyclerview. But what I want is grouping this json using order_no and populate. For example there are two 70000001. So i only need to show one 70000001 order no.
This is my custom row .xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/white">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="2dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dbdbda"
android:orientation="vertical">
<TextView
android:id="#+id/parent_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:text="Parent Title"
android:textColor="#color/primaryColorText"
android:textStyle="bold" />
<TextView
android:id="#+id/parent_q_item_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="2dp"
android:text="Q Title"
android:textColor="#color/secondaryColorText"
android:textSize="12sp" />
<TextView
android:id="#+id/parent_q_item_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="2dp"
android:text="Q Title"
android:textColor="#color/secondaryColorText"
android:textSize="12sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
After grouping, I need to show only one order_no in parent_list_item (70000001) and under that I need to show one quotation_no in the first text view and second quotation_no in the second text view under same order_no
My adapter class
public class OrderQuatationHistory extends RecyclerView.Adapter<OrderQuatationHistory.ItemViewHolder> {
private LayoutInflater layoutInflater;
private ArrayList<OrderHistoryData> orderHistoryDataArrayList;
public OrderQuatationHistory() {
}
public OrderQuatationHistory(Context context) {
orderHistoryDataArrayList = new ArrayList<>();
layoutInflater = LayoutInflater.from(context);
}
public void setOrderHistoryList(ArrayList<OrderHistoryData> orderHistoryDataArrayList) {
this.orderHistoryDataArrayList = orderHistoryDataArrayList;
notifyItemRangeChanged(0, orderHistoryDataArrayList.size());
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.custom_order_history_p_row, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
OrderHistoryData orderHistoryData = orderHistoryDataArrayList.get(position);
holder.orderNo.setText(orderHistoryData.getOrderNo());
holder.quotationNo.setText(orderHistoryData.getQuotationNo());
}
#Override
public int getItemCount() {
return orderHistoryDataArrayList.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView orderNo,quotationNo;
public ItemViewHolder(View itemView) {
super(itemView);
orderNo = (TextView) itemView.findViewById(R.id.parent_list_item);
quotationNo = (TextView) itemView.findViewById(R.id.parent_q_list);
}
}
}
Try this... For avoiding of duplicate value,you can use this Method.
ArrayList<Integer> jSonOrder=new ArrayList();
int order_no= assigned values that you have retrieved from JSon.
if (!jSonOrder.contains(order_no)) {
jSonOrder.add(order_no);
}
I have a recycler view in which I populate a cardview consists of an image and two text fields. It works fine. What i want to know is there some way to remove an image from cardview if it is null (and not showing the blank space for image)and just show the text fields for that specific item in recycler view.
I am parsing a JSON service containing images and text. But some of the images are missing while parsing data and I want to remove that image space while displaying data.
Below is my Cardview.xml i am using inside Recyclerview adapter.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#android:color/black"
android:clickable="true"
android:contextClickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:longClickable="true"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:background="#android:color/black"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textIsSelectable="true"
android:textSize="30dp"
android:textStyle="bold" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/heading"
android:background="#CCC" />
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view"
android:background="#000000"
android:clickable="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
/>
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/date"
android:background="#CCC" />
<TextView
android:id="#+id/brief"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view1"
android:background="#000000"
android:clickable="true"
android:contextClickable="true"
android:elegantTextHeight="true"
android:hyphenationFrequency="full"
android:linksClickable="true"
android:text="Large Text"
android:textAlignment="viewStart"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textColorHighlight="#ffffff"
android:textStyle="italic" />
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/brief"
android:background="#CCC" />
</RelativeLayout>
</android.support.v7.widget.CardView>
The RecyclerViewAdapter class is :
private LayoutInflater inflater;
Context context;
List<Data> dataArray;
private int lastPosition = -1;
public RecyclerViewAdapter(Context context) {
//this.dataArray = dataArray;
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setDataArray(List<Data> dataArray) {
this.dataArray = dataArray;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);
View view = inflater.inflate(R.layout.cardview, parent, false);
CustomViewHolder holder = new CustomViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
Data current = dataArray.get(position);
holder.textView1.setText(current.heading);
holder.textView2.setText(current.date);
holder.textView3.setText(current.brief);
// Using picasso to fetch image as the user scrolls down ... No need to store
// all the images during start up.
Uri uri = Uri.parse(current.getLImage());
//System.out.println("URIiii issss::::"+uri);
Picasso.with(context).load(uri).into(holder.image);
// Animation
setAnimation(holder.relativeLayout, position);
}
#Override
public int getItemCount() {
return dataArray.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView image;
RelativeLayout relativeLayout;
TextView textView1, textView2, textView3;
public CustomViewHolder(View itemView) {
super(itemView);
textView1 = (TextView) itemView.findViewById(R.id.heading);
textView2 = (TextView) itemView.findViewById(R.id.date);
textView3 = (TextView) itemView.findViewById(R.id.brief);
image = (ImageView) itemView.findViewById(R.id.imageView);
}
#Override
public void onClick(View v) {
}
}
private void setAnimation(View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition || position < lastPosition) {
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
}
What i really want is to remove the imageview item inside carview if it is null. I am relatively new to android, so any guideline would be highly appreciated.
Try with this.
If your Image URI is null.
if(uri == null){
Picasso.with(this.context).cancelRequest(holder.image);
holder.image.setVisibility(View.GONE);
}
Hope this helps you.
check two things for this,
first check your limage is null or not, if it is null than hide your image view.
one another thing is check image is already exists on your server url. if not exists on server hide your image view.
you can try below code for that,
if(current.getLImage() == null || !imageExists(**yourImageUrl**)){
Picasso.with(this.context).cancelRequest(holder.image);
holder.image.setVisibility(View.GONE);
}
public static boolean imageExists(String URLName){
HttpClient client= new DefaultHttpClient();
HttpHead headMethod = new HttpHead(urlToImage);
HttpResponse response = client.execute(headMethod);
if(response.getStatusLine().getStatusCode== HttpStatus.SC_NOT_FOUND) {
return false;
} else {
return true;
}
}