i'm just setup my RecyclerView but there's not appear in my emulator & there is error "E/RecyclerView: No adapter attached; skipping layout" Could you guys help me to find my failure?
This is my application layout :
Click here to see my application layout view
StockContent.Java (Fragment) :
package com.example.psmandroidapps;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.Toolbar;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class StockContent extends Fragment {
RecyclerView StockRecyclerView;
List<ModalClass> mList;
CustomAdapter customAdapter;
public StockContent() {
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View stockview = inflater.inflate(R.layout.stock_content, container, false);
StockRecyclerView = stockview.findViewById(R.id.StockRecyclerView);
customAdapter = new CustomAdapter(mList,getContext());
StockRecyclerView.setAdapter(customAdapter);
StockRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return stockview;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//just for sample only
mList = new ArrayList<>();
mList.add(new ModalClass(R.drawable.profilepicture,"Mitsubishi S-N50"));
mList.add(new ModalClass(R.drawable.logodetail,"Mitsubishi S-N50"));
mList.add(new ModalClass(R.drawable.logo,"Mitsubishi S-N50"));
mList.add(new ModalClass(R.drawable.profilepicture,"Mitsubishi S-N50"));
mList.add(new ModalClass(R.drawable.logodetail,"Mitsubishi S-N50"));
mList.add(new ModalClass(R.drawable.logo,"Mitsubishi S-N50"));
}
}
StockContent.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3FFFC">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/StockRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
Stock_CardView.XML (for recyclerview content) :
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp"
android:layout_margin="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="30dp"
android:layout_margin="15dp"
android:layout_weight="0.1">
<ImageView
android:id="#+id/img_goodspicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/profilepicture"
android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
>
<TextView
android:id="#+id/txt_goodsname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mitsubishi S-N10 (220V)"
android:textColor="#000000"
android:fontFamily="#font/sarabun_bold"
android:textSize="21dp"
android:layout_marginBottom="0dp"/>
<TextView
android:id="#+id/txt_goodstype"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jenis : Contactor"
android:textColor="#000000"
android:fontFamily="#font/sarabun_regular"
android:textSize="12.5dp" />
<TextView
android:id="#+id/txt_goodsstock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stok : 105 pcs"
android:textColor="#000000"
android:fontFamily="#font/sarabun_regular"
android:textSize="12.5dp"/>
<TextView
android:id="#+id/txt_dateofgoodsentry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tanggal masuk : 29/07/2020"
android:textColor="#000000"
android:fontFamily="#font/sarabun_regular"
android:textSize="12.5dp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
ModalClass.Java :
package com.example.psmandroidapps;
public class ModalClass {
int image;
String text;
public ModalClass(int image, String text) {
this.image = image;
this.text = text;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
CustomAdapter.Java :
package com.example.psmandroidapps;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
List<ModalClass> mList;
Context context;
public CustomAdapter(List<ModalClass> mList, Context context) {
this.mList = mList;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.stock_cardview,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.imageView.setImageResource(mList.get(position).getImage());
holder.textView.setText(mList.get(position).getText());
}
#Override
public int getItemCount() {
return mList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.img_goodspicture);
textView=itemView.findViewById(R.id.txt_goodsname);
}
}
}
Thats the code of mine.. the result is nothing, nothing showing in my emulator.. Thank you for read this question, hope you guys can help me to solve my problem here. Thank you guys! have a nice day
Related
When using a custom adapter in Firebase, I ran into a problem that everything works, but the pictures in the sheet are not displayed. Something incomprehensible is highlighted, but not her. It is necessary that they be in a normal sheet and they can be seen. I think it's because of the context in Picasso. The guide I followed had a wish(mContext) method that I can't use now. Please help, I don't understand what is the problem. I am attaching the image_item.xml code
<?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"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Name"
android:textColor="#android:color/black"
android:textSize="20sp" />
<ImageView
android:id="#+id/image_view_upload"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
activity_images
<?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=".ImagesActivity">
<ProgressBar
android:id="#+id/progress_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private List<Upload> mUploads;
public ImageAdapter(Context context, List<Upload> uploads) {
mContext = context;
mUploads = uploads;
}
#Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
}
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private ProgressBar mProgressCircle;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mProgressCircle = findViewById(R.id.progress_circle);
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new ImageAdapter(ImagesActivity.this, mUploads);
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
mProgressCircle.setVisibility(View.INVISIBLE);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ImagesActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
mProgressCircle.setVisibility(View.INVISIBLE);
}
});
}
}
If i complete those edit text with what i want,it will show the info to my fire data base but in the recycler view never show the third holder(holder.textViewZ.setText(model.getWhy). Do you have any idea why is this happening?
This is my main activity
package com.example.incercarefirebase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.ColorSpace;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.SuccessContinuation;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.net.IDN;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button btnSaveRecycler;
private EditText etNAME, etSurrname, etZ;
private FirebaseRecyclerOptions<Model> options;
private DatabaseReference rootDatabaseref;
private FirebaseRecyclerAdapter<Model, MyViewHolder> adapter;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNAME = findViewById(R.id.etNAME);
etSurrname = findViewById(R.id.etSurrname);
etZ = findViewById(R.id.etZ);
btnSaveRecycler = findViewById(R.id.btnSaveRecycler);
recyclerView = findViewById(R.id.recyclerviewID);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
rootDatabaseref = FirebaseDatabase.getInstance().getReference().child("Users");
btnSaveRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int id = Integer.parseInt(etNAME.getText().toString());
String name = etSurrname.getText().toString();
String why = etZ.getText().toString();
String key = rootDatabaseref.push().getKey();
rootDatabaseref.child(key).child("ID").setValue(id);
rootDatabaseref.child(key).child("Name").setValue(name);
rootDatabaseref.child(key).child("Why").setValue(why);
}
});
options = new FirebaseRecyclerOptions.Builder<Model>().setQuery(rootDatabaseref, Model.class).build();
adapter = new FirebaseRecyclerAdapter<Model, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull Model model) {
holder.textviewName.setText(model.getName());
holder.textviewID.setText(model.getID());
holder.textViewZ.setText(model.getWhy());
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view_layout, parent, false);
return new MyViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
This is my viewHolder
package com.example.incercarefirebase;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textviewID, textviewName, textViewZ;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
textviewID = itemView.findViewById(R.id.textViewID);
textviewName = itemView.findViewById(R.id.textViewName);
textViewZ = itemView.findViewById(R.id.textViewZ);
}
}
This is my model
package com.example.incercarefirebase;
public class Model {
private int ID;
private String Name;
private String Why;
public Model() {
}
public Model(int ID, String name, String why) {
this.ID = ID;
Name = name;
Why = why;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getWhy() {
return Why;
}
public void setWhy(String why) {
Why = why;
}
}
This is the single_view_layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"
android:textSize="30dp" />
<TextView
android:id="#+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name..."
android:textSize="30dp" />
<TextView
android:id="#+id/textViewZ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaaaa"
android:textSize="30dp" />
</LinearLayout>
This is the main 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=".MainActivity">
<EditText
android:id="#+id/etNAME"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="ID"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etSurrname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPassword" />
<EditText
android:id="#+id/etZ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/btnSaveRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerviewID"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
So, I have followed this tutorial (https://www.youtube.com/watch?v=cS5l_hPwC10) on how to do a card stack and it works fine even with 2 cardstacks at the same time. now I want to add a button so i can reset both stacks and/or a button to get to the previous card
Here is all the code I have related to the card stack. Tell me if you need more since this is the first time I am trying to programm something
hauptanwendung_fragment.xml
<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:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentHauptanwendung">
<com.wenchao.cardstack.CardStack
android:id="#+id/card_stack"
android:layout_width="280dp"
android:layout_height="170dp"
android:clipChildren="false"
android:clipToPadding="false" />
<com.wenchao.cardstack.CardStack
android:id="#+id/card_stack2"
android:layout_width="280dp"
android:layout_height="170dp"
android:clipChildren="false"
android:clipToPadding="false" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/stern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
app:srcCompat="#drawable/ic_stern02" />
<Space
android:layout_width="50sp"
android:layout_height="match_parent"
android:layout_marginRight="15sp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/stift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
app:srcCompat="#drawable/ic_bearbeiten01" />
</LinearLayout>
</LinearLayout>
card_layout.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">
<ImageView
android:id="#+id/image_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</LinearLayout>
card_layout2.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">
<ImageView
android:id="#+id/image_content2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</LinearLayout>
FragmentHauptanwendung.java
package com.example.ideenfinder3;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.wenchao.cardstack.CardStack;
import java.util.Objects;
public class FragmentHauptanwendung extends Fragment implements CardStack.CardEventListener {
View view;
private CardsDataAdapter card_adapter;
private CardsDataAdapter2 card_adapter2;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hauptanwendung_fragment, container, false);
initImages();
initImages2();
CardStack card_stack = (CardStack) view.findViewById(R.id.card_stack);
card_stack.setContentResource(R.layout.card_layout);
card_stack.setStackMargin(20);
card_stack.setAdapter(card_adapter);
card_stack.setListener(this);
CardStack card_stack2 = (CardStack) view.findViewById(R.id.card_stack2);
card_stack2.setContentResource(R.layout.card_layout2);
card_stack2.setStackMargin(20);
card_stack2.setAdapter(card_adapter2);
card_stack2.setListener(this);
return view;
}
private void initImages2() {
card_adapter2 = new CardsDataAdapter2(Objects.requireNonNull(getActivity()).getApplicationContext(),0);
card_adapter2.add(R.drawable.logo);
card_adapter2.add(R.drawable.schriftzug);
}
private void initImages() {
card_adapter = new CardsDataAdapter(Objects.requireNonNull(getActivity()).getApplicationContext(),0);
card_adapter.add(R.drawable.bild01);
card_adapter.add(R.drawable.bild02);
card_adapter.add(R.drawable.bild03);
card_adapter.add(R.drawable.bild04);
card_adapter.add(R.drawable.bild05);
}
#Override
public boolean swipeEnd(int i, float v) {
return v > 300;
}
#Override
public boolean swipeStart(int i, float v) {
return false;
}
#Override
public boolean swipeContinue(int i, float v, float v1) {
return false;
}
#Override
public void discarded(int i, int i1) {
}
#Override
public void topCardTapped() {
}
}
CardsDataAdapter
package com.example.ideenfinder3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class CardsDataAdapter extends ArrayAdapter<Integer> {
public CardsDataAdapter(Context context, int resource) {
super(context, resource);
}
#Override
public View getView(int position, final View ImgcontentView, ViewGroup parent){
ImageView ImgV = (ImageView) (ImgcontentView.findViewById(R.id.image_content));
ImgV.setImageResource(getItem(position));
return ImgcontentView;
}
}
CardsDataAdapter2
package com.example.ideenfinder3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class CardsDataAdapter2 extends ArrayAdapter<Integer> {
public CardsDataAdapter2(#NonNull Context context, int resource) {
super(context, resource);
}
#Override
public View getView(int position, final View ImgcontentView2, ViewGroup parent){
ImageView ImgV2 = (ImageView) (ImgcontentView2.findViewById(R.id.image_content2));
ImgV2.setImageResource(getItem(position));
return ImgcontentView2;
}
}
Just keep handles to CardStack instead:
private CardStack cardStack01;
private CardStack cardStack02;
And let the Fragment implement getters and setters for these.
I made a listview with adapter and each listview item contains four RadioButtons.
What I want to do is to have a button on the listview containing activity.And when I click on that button it will retrieve all checkstate of radiobuttons of each item and send them to other activities.
But I don't know how to retrieve that data although I searched a lot . this is the Activity.java file
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.width;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.util.TypedValue.applyDimension;
/**
* Created by jack on 30/08/17.
*/
public class TempActivity extends AppCompatActivity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s7_1);
ArrayList<String> titlesArray = new ArrayList<>();
titlesArray.add("إيه رأيك في البني آدم اللي شرح الدورة");
titlesArray.add("ايه رأيك في المكان اللي تمت فيه الدورة");
titlesArray.add("الجهة المنظمة اللي عملت الدورة");
titlesArray.add("محتوى الدورة");
titlesArray.add("تقييم عام");
//Initialization of Adapter and ListView and hooking them together
RateAdapter rateAdapter = new RateAdapter(this , titlesArray);
listView = (ListView)findViewById(R.id.rate_listview);
listView.setAdapter(rateAdapter);
Toast.makeText(getBaseContext() , listView.getCount()+"" , Toast.LENGTH_SHORT).show();
}
}
This is my adapter
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
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.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class RateAdapter extends ArrayAdapter {
View listItemView;
String currentTitle;
TextView titleTV ;
Context context;
public String a ="test";
public RateAdapter(Context context, ArrayList<String> arrayList) {
super(context, 0, arrayList);
this.context = context;
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.n, parent, false);
}
//Declaration for the current title
currentTitle = (String) getItem(position);
//Initializations //Note if radiobuttons are declared as global variables the items are missed up :D
final RadioButton radioVa = (RadioButton) listItemView.findViewById(R.id.rateitems_va);
final RadioButton radioVb = (RadioButton) listItemView.findViewById(R.id.rateitems_vb);
final RadioButton radioVc = (RadioButton) listItemView.findViewById(R.id.rateitems_vc);
final RadioButton radioVd = (RadioButton) listItemView.findViewById(R.id.rateitems_vd);
titleTV = (TextView)listItemView.findViewById(R.id.rateitems_title);
//Setting the title to the TextView
titleTV.setText(currentTitle);
//Setting oncheck listener to permit only one check
//for Va
radioVa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVc.setChecked(false);
}
}
});
//
radioVb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVa.setChecked(false);
radioVc.setChecked(false);
radioVd.setChecked(false);
}
}
});
radioVc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVa.setChecked(false);
radioVb.setChecked(false);
radioVd.setChecked(false);
}
}
});
radioVd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVb.setChecked(false);
radioVc.setChecked(false);
radioVa.setChecked(false);
}
}
});
return listItemView;
}
}
This is the listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.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="160dp"
android:orientation="vertical"
android:layoutDirection="rtl"
android:background="#fff"
android:paddingBottom="10dp">
<TextView
android:id="#+id/rateitems_title"
android:gravity="center_vertical"
android:paddingRight="15dp"
tools:text="ًWhat do you think about me "
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold"
android:background="#277db3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>
<LinearLayout
android:orientation="vertical"
android:padding="30dp"
android:layout_below="#id/rateitems_title"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:background="#drawable/course_items_border_trans"
android:id="#+id/rateitems_va"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="ممتاز"
android:textSize="15sp"
android:paddingRight="5dp"/>
<RadioButton
android:id="#+id/rateitems_vb"
android:background="#drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="جيد جدا"
android:textSize="15sp"
android:paddingRight="5dp"/>
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rateitems_vc"
android:background="#drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="جيد"
android:textSize="15sp"
android:paddingRight="5dp"/>
<RadioButton
android:id="#+id/rateitems_vd"
android:background="#drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="ضعيف"
android:textSize="15sp"
android:paddingRight="5dp"/>
</RadioGroup>
</LinearLayout>
</android.widget.RelativeLayout>
and this is the activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.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"
android:layoutDirection="rtl"
android:background="#fff"
android:paddingBottom="10dp">
<TextView
android:gravity="center_vertical"
android:paddingLeft="15dp"
tools:text="List test"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:background="#277db3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>
<ListView
android:id="#+id/rate_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="#+id/the_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.widget.LinearLayout>
Use a model something like
Class Rate{
String title;
int rate;
Rates(String title){
this.title = title;
}
public void setRate(int rate){
this.rate = rate;
}
public String getTitle(){
return this.title;
}
}
Now, inside activity do something like:
List<Rates> rates = new ArrayList<>();
rates.add(new Rates("إيه رأيك في البني آدم اللي شرح الدورة"));
rates.add(new Rates("ايه رأيك في المكان اللي تمت فيه الدورة");
rates.add(new Rates("الجهة المنظمة اللي عملت الدورة"));
rates.add(new Rates("محتوى الدورة"));
rates.add(new Rates("تقييم عام"));
List<String> strings = new ArrayList<>(rates.size());
for (Rate rate : rates) {
strings.add(rate != null ? rate.getTitle() : null);
}
//Initialization of Adapter and ListView and hooking them together
RateAdapter rateAdapter = new RateAdapter(this , titlesArray);
Now, inside adapter class, Handle the Event for radio buttons. To pass data back to Activity, created an interface to update respective index.
interface RadioClickListener{
void radioClicked(int position, int rate);
}
...
RadioClickListener radioClickListener;
...
public RateAdapter(Context context, ArrayList<String> arrayList) {
super(context, 0, arrayList);
this.context = context;
radioClicked = (RadioClickListener) context;
}
So, when you click the ratioButton, item at respective index is updated by calling
//pass the position and rate for that item
radioClickListener.radioClicked(position, "1");
And finally implement the interface to the Activity to update the List.
public class TempActivity extends AppCompatActivity implements RateAdapter.RadioClickListener {
...
#Override
public void radioClicked(int position, int rate){
rates.get(position).setRate(rate);
}
Now, you have the updated rates List, So to pass it to another activity parcel is with the intent and send it.
I'm trying to inflate a RecyclerView which has as StaggeredGrid Layout, but it is not showing anything. I've pretty much copied previous code I've used before for the RecyclerView so I'm kind of stumped.
In MuseumStoriesViewHolder.onCreateViewHolder() the return of holder has the following value ViewHolder{337ec22b position=-1 id=-1, oldPos=-1, pLpos:-1 unboundundefined adapter position no parent} I'm not sure if this is realated, but it was something that seemed off to me.
It also might help to know that the fragment I'm inflating this RecyclerView is a nested Fragment.
Any help would be greatly appreciated.
MuseumFragment
package com.example.android.radiobuttontestproject.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.radiobuttontestproject.R;
import com.example.android.radiobuttontestproject.adapters.MuseumStoriesAdapter;
import com.example.android.radiobuttontestproject.helpers.pojo.StoryObject;
import com.example.android.radiobuttontestproject.test.SampleDataFactory;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MuseumFragment extends Fragment {
private List<StoryObject> storyObjectList;
private StaggeredGridLayoutManager storyGridLayoutManager;
private MuseumStoriesAdapter storyAdapter;
#Bind(R.id.stories_recycler_view) RecyclerView storiesRecyclerView;
public static MuseumFragment newInstance() {
MuseumFragment fragment = new MuseumFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public MuseumFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_museum, container, false);
ButterKnife.bind(this, view);
//Sets up the stories
SampleDataFactory sampleDataFactory = new SampleDataFactory();
storyObjectList = sampleDataFactory.getSampleStories(
getResources().getStringArray(R.array.test_titles_for_grid_museum1),
getResources().getStringArray(R.array.test_desc_for_grid_museum1));
storyGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
storiesRecyclerView.setLayoutManager(storyGridLayoutManager);
storyAdapter = new MuseumStoriesAdapter(getActivity().getApplicationContext(), storyObjectList);
storiesRecyclerView.setAdapter(storyAdapter);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
MuseumStoriesAdapter
package com.example.android.radiobuttontestproject.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 android.widget.Toast;
import com.example.android.radiobuttontestproject.R;
import com.example.android.radiobuttontestproject.helpers.pojo.StoryObject;
import java.util.List;
public class MuseumStoriesAdapter extends RecyclerView.Adapter<MuseumStoriesAdapter.MuseumStoriesViewHolder> {
private List<StoryObject> itemList;
private LayoutInflater inflater;
private Context context;
public MuseumStoriesAdapter(Context context, List<StoryObject> itemList) {
this.itemList = itemList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public MuseumStoriesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = inflater.inflate(R.layout.view_box_small, viewGroup, false);
MuseumStoriesViewHolder holder = new MuseumStoriesViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MuseumStoriesViewHolder holder, int position) {
holder.title.setText(itemList.get(position).getTitle());
holder.desc.setText(itemList.get(position).getDescription());
}
#Override
public int getItemCount() {
return itemList.size();
}
class MuseumStoriesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView type,title,desc;
public MuseumStoriesViewHolder(View itemView) {
super(itemView);
//Tried Butterknife, but it doesn't seem like it was working in the view holder. - Peter
type = (TextView) itemView.findViewById(R.id.small_box_type);
title = (TextView) itemView.findViewById(R.id.small_box_title);
desc = (TextView) itemView.findViewById(R.id.small_box_desc);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
}
fragment_museum.xml
<ScrollView
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="com.example.android.radiobuttontestproject.fragments.MuseumFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:id="#+id/museum_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/header_margin"
android:gravity="center"
android:textSize="#dimen/font_larger"
android:text="#string/museum_header" />
<android.support.v7.widget.RecyclerView
android:id="#+id/stories_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
view_box_small.xml
<?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:layout_margin="#dimen/small_box_margin"
android:background="#color/small_box_background_color">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--TODO Make layout_height wrap contenet -->
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#color/test_color2"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/abc_btn_rating_star_off_mtrl_alpha"
/>
</FrameLayout>
<TextView
android:id="#+id/small_box_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_small"
android:textColor="#color/font_red"
android:text="Object"
/>
<TextView
android:id="#+id/small_box_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_large"
android:textColor="#color/font_black"
android:text="Sample Text Here"
/>
<TextView
android:id="#+id/small_box_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/font_normal"
android:textColor="#color/font_black"
android:textStyle="italic"
android:text="Sample Text Here"
/>
</LinearLayout>