OnClickListener Recyclerview - android

my item_layout xml (see it like a template) contain a cardview with some texts and one button, when I run the application. the recyclerview shows two card views with different text (actor names, actors movies) all its ok, but the problem is when I made the button with an onclicklister method to show other activities. the result is when I click on button of the first card view and the button of the second cardview it shows the same result.
what I want is when I click on button on the first cardview it shows Activity number 2, and when I click on the button on the second cardview must shows Activity number 3.
ps: I want different result when I click on button of each cardview
xml :
<RelativeLayout
android:longClickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="7dp">
<ImageView
android:id="#+id/profileImage"
android:layout_width="70dp"
android:layout_height="50dp"
app:civ_border_color="#7f89e9"
android:layout_marginLeft="5dp"
android:background="#drawable/contact1"
android:layout_alignTop="#+id/txtCelebName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_marginTop="8dp"
android:id="#+id/txtCelebName"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/profileImage"
android:text="Large Text"
android:layout_marginLeft="18dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_marginLeft="18dp"
android:textSize="13dp"
android:id="#+id/txtCelebMovie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtCelebName"
android:layout_toRightOf="#+id/profileImage"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/textView"
android:layout_marginLeft="10dp"
android:layout_marginTop="27dp"
android:layout_below="#+id/profileImage"
android:text="heur/travaille : 5:00 pm - 8:00 am." />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="emplacement de travaille : cité12 - cité42. "
android:id="#+id/textView2"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="statut : disponible."
android:id="#+id/textView3"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="............"
android:id="#+id/textView4"
android:layout_below="#+id/profileImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="...................................."
android:id="#+id/textView5"
android:layout_alignLeft="#+id/textView4"
android:layout_alignStart="#+id/textView4"
android:layout_below="#+id/textView2"
android:layout_marginBottom="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go"
android:id="#+id/buttonfordialog"
android:layout_alignBottom="#+id/textView4"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3" />
</RelativeLayout>
MainActivity java :
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ItemAdapter itemAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener btnListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
};
final Toolbar toolbar = (Toolbar)findViewById(R.id.MyToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapse_toolbar);
collapsingToolbarLayout.setTitle("Alert ! ");
ArrayList<Celebrity> itemList = new ArrayList<>();
fillDummyData(itemList);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
itemAdapter = new ItemAdapter(itemList, btnListener);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(itemAdapter);
}
private void fillDummyData(ArrayList<Celebrity> celebList) {
Celebrity celeb1 = new Celebrity();
celeb1.setName("Johny.D");
celeb1.setFamousMovie("Pirates ");
celeb1.setProfilePhotoLocation("#drawable/contact1");
celebList.add(celeb1);
Celebrity celeb2 = new Celebrity();
celeb2.setName("Arnold");
celeb2.setFamousMovie("The Terminator");
celeb2.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTI3MDc4NzUyMV5BMl5BanBnXkFtZTcwMTQyMTc5MQ##._V1._SY209_CR13,0,140,209_.jpg");
celebList.add(celeb2);
}
}
the Adapter :
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemHolder> {
private List<Celebrity> celebrityList;
private final View.OnClickListener btnListener;
public ItemAdapter(List<Celebrity> celebrityList, View.OnClickListener btnListener) {
this.celebrityList = celebrityList;
this.btnListener = btnListener;
}
#Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
return new ItemHolder(itemView, btnListener);
}
#Override
public void onBindViewHolder(ItemHolder holder, int position) {
Celebrity item = celebrityList.get(position);
holder.txtCelebName.setText(item.getName());
holder.txtCelebMovie.setText(item.getFamousMovie());
}
#Override
public int getItemCount() {
return celebrityList.size();
}
public class ItemHolder extends RecyclerView.ViewHolder {
private Button buttoncalling;
public TextView txtCelebName, txtCelebMovie;
public ImageView profileImage;
public ItemHolder(View view, View.OnClickListener btnListener) {
super(view);
txtCelebName = (TextView) view.findViewById(R.id.txtCelebName);
txtCelebMovie = (TextView) view.findViewById(R.id.txtCelebMovie);
profileImage = (ImageView) view.findViewById(R.id.profileImage);
buttoncalling = (Button) view.findViewById(R.id.buttonfordialog);
buttoncalling.setOnClickListener(btnListener);
}
}
}

define OnClickListener inside onBindViewHolder();
String [] activities={"FirstActivity","SecondActivity","ThirdActivity"};
String packageNamePrefix="com.example."
//String packageNamePrefix="YOUR_PACKAGE_NAME"
#Override
public void onBindViewHolder(ItemHolder holder,final int position) {
Celebrity item = celebrityList.get(position);
holder.txtCelebName.setText(item.getName());
holder.txtCelebMovie.setText(item.getFamousMovie());
holder.buttoncalling.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new
Intent(MainActivity.this,Class.forName(packageNamePrefix+activities[position]));
startActivity(intent);
}
});
}
it will launch FirstActivity,SecondActivity,ThirdActivity respectively.
I hope it will help.

There are many ways to implement OnClickListener inside RecyclerView adapter.
You can implement click listener inside onBindViewHolder() method like below
holder.buttoncalling.setOnClickListener(new View.OnClickListener(){
// perform click operation
});
You can also implement it inside you ItemHolder like this
buttoncalling.setOnClickListener( new View.OnClickListener(){
// perform click operation
});
and use getAdapterPosition() whenever you need item clicked position as recommended in official docs.
You can also make interface callbacks to your Activity and pass position along with it for refrence.

You should use inline onclickListener like below;
YOURBUTTON.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your codes goes here...
}
});

Related

How to update TextView outside Recyclerview after updating database

I have an activity that is separated into two sections:
This first one is a Relative layout, in which I have a TextView that displays the total amount based on data stored on the database.
The second part is a RecyclerView, which displays all the records.
On the adapter I can update and delete the information from the database and I use notifyItemRemoved(position) and notifyItemChanged(position) to reflect the change on the reclyclerview.
My problem is that I couldn't find a way to update the TextView on the Relative Layout, I tried to use notifyDataSetChanged() on the Activity but it doesn't reload the list with the sum of values from the database. If I use it from the adapter I couldn't find a way to access the TextView on the activity. I also tried to set an observer on the adapter, but it doesn't reflect the changes.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
tripRecordList = getStandardAmountList(tripRecordList);
Collections.reverse(tripRecordList);
mAmount.setText(String.valueOf(nf.format(getAmountStd(tripRecordList))));
}
}
});
Following are the code fragments.
Activity layout:
<?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="match_parent"
android:background="#FFFFFF"
tools:context=".Activities.ViewTripRecord">
<RelativeLayout
android:id="#+id/viewrecordLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/viewrecordSummaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="#dimen/cardview_compat_inset_shadow"
android:gravity="center"
android:text="#string/trip_summary"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp" />
<TextView
android:id="#+id/viewrecordTotalAmountText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/viewrecordSummaryText"
android:layout_alignParentStart="true"
android:padding="3dp"
android:text="#string/amount"
android:textSize="15sp"
android:textStyle="italic|bold" />
<TextView
android:id="#+id/viewrecordTotalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/viewrecordSummaryText"
android:layout_alignParentStart="true"
android:layout_marginStart="95dp"
android:padding="3dp"
android:textSize="15sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/viewrecordRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/viewrecordTotalAmountText"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_trip_record);
setTitle(getResources().getString(R.string.view_expenses));
dbHandler = new DbHandler(this);
tripID = getIntent().getIntExtra("TripID",0);
tripRecordList = dbHandler.getTripRecords(tripID);
mAmount = findViewById(R.id.viewrecordTotalAmount);
recyclerView = findViewById(R.id.viewrecordRecyclerView);
setLayout();
}
private void setLayout(){
NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
if(tripRecordList.size()>0) {
tripRecordList = getStandardAmountList(tripRecordList);
Collections.reverse(tripRecordList);
mAmount.setText(String.valueOf(nf.format(getAmountStd(tripRecordList))));
}
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ViewTripRecordAdapter(tripRecordList, this);
recyclerView.setAdapter(adapter);
}
Adpater:
public class TripRecordView extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView stdAmount;
private ImageButton infoButton, deleteButton, updateButton;
public TripRecordView(#NonNull View itemView) {
super(itemView);
stdAmount = itemView.findViewById(R.id.viewtriptrecordStdAmount);
deleteButton = itemView.findViewById(R.id.viewtriptrecordDelete);
deleteButton.setOnClickListener(this);
updateButton = itemView.findViewById(R.id.viewtriptrecordUpdateButton);
updateButton.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
final int position = getAdapterPosition();
switch (v.getId()){
case R.id.viewtriptrecordDelete:
deleteRecord(tripRecord2,position);
break;
case R.id.viewtriptrecordUpdateButton:
updateRecord(tripRecord2,position,recordView );
break;
}
}
}
public void deleteRecord(TripRecord deletedRecord, int position){
dbHandler = new DbHandler(context);
dbHandler.deleteTripRecord(deletedRecord.getTripID(),deletedRecord.getId());
tripRecordList.remove(position);
notifyItemRemoved(position);
}
public void updateRecord(final TripRecord updatedRecord, final int position, final View view){
EditText mAmount;
mAmount = view.findViewById(R.id.insertrecordAmount);
updatedRecord.setAmount(Double.valueOf(mAmount.getText().toString()));
dbHandler.updateTripRecord(updatedRecord);
notifyItemChanged(position);
}
Thanks,
Odair

Having-clicks inside RecyclerAdapter on my Recyclerview

I was trying to make responsive clicks events inside my RecyclerAdapter. I can't understand the logic behind on it since my data is auto created by API, therefore i have some buttons in my list and I've like to have response by clicking on it.
This is my code:
My Adapter class:
public class EppViewCartAdapter extends RecyclerView.Adapter<EppViewCartAdapter.MyViewHolder> {
View itemView;
ImageView add,less;
private List<EppViewCartDetails> orderData;
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView subject,category,brand,neww,newd,old,discount,count,color;
ImageView image;
public MyViewHolder(View view) {
super(view);
subject=view.findViewById(R.id.view_cart_item_subject);
category=view.findViewById(R.id.view_cart_item_category);
brand=view.findViewById(R.id.view_cart_item_brand);
neww=view.findViewById(R.id.view_cart_item_neww);
old=view.findViewById(R.id.view_cart_item_old);
discount=view.findViewById(R.id.view_cart_item_discount);
newd=view.findViewById(R.id.view_cart_item_new_depends_count);
count=view.findViewById(R.id.view_cart_item_count);
image=view.findViewById(R.id.view_cart_item_image);
add=itemView.findViewById(R.id.view_btn_add);
less=itemView.findViewById(R.id.view_btn_less);
}
}
public EppViewCartAdapter(List<EppViewCartDetails> preorderList) {
this.orderData = preorderList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_cart_card_view, parent, false);
//TODO: I was trying to make a set onlcick listener here but it has errors.
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Add Click response properly.", Toast.LENGTH_SHORT).show();
}
});
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
EppViewCartDetails data= orderData.get(position);
double amount = Double.parseDouble(data.getCount())*Double.parseDouble(data.getNeww());
DecimalFormat formatter = new DecimalFormat("#,###.00");
holder.subject.setText(data.getSubject());
holder.category.setText(data.getCategory());
holder.brand.setText(data.getBrand());
holder.neww.setText("₱ "+formatter.format(amount));
holder.newd.setText(data.getNewd());
holder.old.setText(data.getOld());
holder.old.setPaintFlags(holder.old.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
holder.discount.setText(data.getDiscount());
holder.count.setText(data.getCount());
Picasso.with(itemView.getContext()).load("https://eppteststorage.blob.core.windows.net/images/"+data.getImage()).placeholder(R.drawable.small_logo).into(holder.image, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Log.e("tae tae"," PAkultie");
}
#Override
public void onError() {
}
}
);
}
#Override
public int getItemCount() {
return orderData.size();
}
}
My error trying my OnclickEvent:(upon my //TODO:)
Process: eppmobile.intellismart.com.EPP, PID: 18675
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at eppmobile.intellismart.com.EPP.ViewCart.EppViewCartAdapter.onCreateViewHolder(EppViewCartAdapter.java:58)
at eppmobile.intellismart.com.EPP.ViewCart.EppViewCartAdapter.onCreateViewHolder(EppViewCartAdapter.java:22)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
My Activity class:
public class EppViewCart extends Fragment {
View inflatedView = null;
EppViewCartDetails items;
private List<EppViewCartDetails> dataitems = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
inflatedView = inflater.inflate(R.layout.fragment_epp_view_cart, container, false);
retriveSQLITE();
return inflatedView;
}
private void retriveSQLITE() {
SqLiteDB sql = new SqLiteDB(getContext());
SQLiteDatabase db = sql.getWritableDatabase();
Cursor c = sql.retrieveAddToCart(db);
ArrayList<AddToCartModel> get = new ArrayList<>();
while (c.moveToNext()) {
final AddToCartModel details = new AddToCartModel();
details.setID(c.getString(c.getColumnIndexOrThrow("ID")));
details.setDateTime(c.getString(c.getColumnIndexOrThrow("DateTime")));
details.setID_ProductMasterListV(c.getString(c.getColumnIndexOrThrow("ID_ProductMasterListV")));
details.setImageFile(c.getString(c.getColumnIndexOrThrow("ImageFile")));
details.setOrdered(c.getString(c.getColumnIndexOrThrow("Ordered")));
get.add(details);
APICaller service = EppMainFragmentPropertyClient.getRetrofit().create(APICaller.class);
View_Response gets = new View_Response("1", details.getID(), details.getID_ProductMasterListV());
Call<List<View_Data_Properties>> call = service.getView(gets);
call.enqueue(new Callback<List<View_Data_Properties>>() {
#Override
public void onResponse(Call<List<View_Data_Properties>> call, Response<List<View_Data_Properties>> response) {
//Discount
String q = String.format("%.2f", Double.parseDouble(response.body().get(0).getLess()));
long l = Math.round(Double.parseDouble(q));
String percent = l + "%";
double amount = Double.parseDouble(response.body().get(0).getNewPrice());
DecimalFormat formatter = new DecimalFormat("#,###.00");
double amount2 = Double.parseDouble(response.body().get(0).getSRP());
DecimalFormat formatter2 = new DecimalFormat("#,###.00");
items = new EppViewCartDetails(
response.body().get(0).getID(),
response.body().get(0).getName(),
response.body().get(0).getCategory(),
response.body().get(0).getProductsBrand(),
response.body().get(0).getNewPrice(),
"₱ " + formatter.format(amount),
"₱ " + formatter2.format(amount2),
"-" + percent,
details.getOrdered(), response.body().get(0).getHexValue(),
response.body().get(0).getImageFile());
dataitems.add(items);
prepareItems();
}
#Override
public void onFailure(Call<List<View_Data_Properties>> call, Throwable t) {
}
});
}
}
private void prepareItems() {
//Items
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
RecyclerView recyclerView = inflatedView.findViewById(R.id.view_cart_recycler);
recyclerView.setLayoutManager(layoutManager);
EppViewCartAdapter mAdapter = new EppViewCartAdapter(dataitems);
recyclerView.setAdapter(mAdapter);
}
}
My CardView:
<?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="wrap_content">
<RelativeLayout
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/radius_all_two"
android:layout_width="match_parent"
android:layout_height="190dp">
<ImageView
android:layout_width="110dp"
android:layout_height="120dp"
android:src="#drawable/bestseller"
android:id="#+id/view_cart_item_image"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JBL- Grip 100"
android:id="#+id/view_cart_item_subject"
android:textColor="#000"
android:textSize="14dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="25dp"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_marginTop="15dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category"
android:textColor="#089bcc"
android:textSize="11dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="48dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Headphones"
android:id="#+id/view_cart_item_category"
android:textSize="11dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="48dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brand"
android:textColor="#089bcc"
android:textSize="11dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="65dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JBL"
android:id="#+id/view_cart_item_brand"
android:textSize="11dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="65dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P 1,290.00"
android:id="#+id/view_cart_item_neww"
android:textColor="#00c1ab"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginLeft="140dp"
android:layout_marginTop="83dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P 2,580.00"
android:textSize="11dp"
android:id="#+id/view_cart_item_old"
android:layout_marginLeft="140dp"
android:layout_marginTop="106dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="- 50%"
android:textSize="11dp"
android:id="#+id/view_cart_item_discount"
android:layout_marginLeft="210dp"
android:textColor="#ba0101"
android:layout_marginTop="106dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P 1,290.00"
android:textSize="11dp"
android:layout_marginLeft="140dp"
android:id="#+id/view_cart_item_new_depends_count"
android:layout_marginTop="123dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:textSize="12dp"
android:layout_marginLeft="240dp"
android:textColor="#000"
android:layout_marginTop="123dp"/>
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:src="#drawable/left_arrow"
android:text="x"
android:id="#+id/view_cart_item_increment"
android:textSize="12dp"
android:layout_marginLeft="250dp"
android:layout_marginTop="123dp"/>
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:src="#drawable/right_arrow"
android:text="x"
android:id="#+id/view_cart_item_decrement"
android:textSize="12dp"
android:layout_marginLeft="304dp"
android:layout_marginTop="123dp"/>
<TextView
android:layout_width="31dp"
android:layout_height="16dp"
android:textSize="11dp"
android:textAlignment="center"
android:text="100"
android:id="#+id/view_cart_item_count"
android:layout_marginLeft="270dp"
android:layout_marginTop="123dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color"
android:textSize="11dp"
android:textColor="#089bcc"
android:layout_marginLeft="140dp"
android:layout_marginTop="147dp"/>
<ImageView
android:layout_width="34dp"
android:layout_height="17dp"
android:textSize="12dp"
android:id="#+id/view_cart_variant"
android:background="#color/colorPrimaryDark"
android:layout_marginLeft="175dp"
android:layout_marginTop="145dp"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
My Cart_view :
enter image description here
(if you see the "<" and ">" sign, I was trying to add responsive there but it is running in RecyclerView with my adapter so that I didn't know if where could I put my onclick-event.)
My View in recyclerview(I set into comment my //TODO:)
(It is running but no fuction on add and subtract.)
enter image description here
Also, I've try this to (holder.add.setOn...)
enter image description here
And this:(MyViewHolder)
enter image description here
This is null pointer exception you need to understand.
you should replace itemView.findViewById(R.id.view_btn_add) with view.findViewById(R.id.view_btn_add)
because the adapter layout name is view not itemview
just look your code
public MyViewHolder(View view) {
super(view);
}
and you need add click listener either in MyViewHolder() or in onBindViewHolder()
move this code
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Add Click response properly.", Toast.LENGTH_SHORT).show();
}
});
I found the answer, in my other view cart data, there are the same ID in my xml so that ive just rename my buttons in my xml and inherit it on my class.
My viewCart:
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:src="#drawable/left_arrow"
android:text="x"
android:id="#+id/view_cart_item_increment1"
android:textSize="12dp"
android:layout_marginLeft="250dp"
android:layout_marginTop="123dp"/>
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:src="#drawable/right_arrow"
android:text="x"
android:id="#+id/view_cart_item_decrement1"
android:textSize="12dp"
android:layout_marginLeft="304dp"
android:layout_marginTop="123dp"/>
My Adapter:
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_cart_card_view, parent, false);
add=itemView.findViewById(R.id.view_cart_item_increment1);
less=itemView.findViewById(R.id.view_cart_item_decrement1);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
EppViewCartDetails data= orderData.get(position);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Add Click response properly.", Toast.LENGTH_SHORT).show();
}
});
}
The crash is occurs because you are setting click listener on null add view.
First of check your layout file for the all defined IDs in MyViewHolder class. i can't find view_btn_add id in your layout file. So assign it with appropriate view.
And then in your onCreateViewHolder method, you are setting up onClick listener on add view before initialising it.
So move your add.setOnClickListener(...) inside MyViewHolder class after defining all views. So your crash will gone.

How to increment the value of edittext for each row in a recyclerview?

I am developing a shopping cart and I want to increase the number of Items purchased when a user clicks a button in each row of the recyclerview.
I have tried to increment the edit text when a user clicks the increase button but it updates value of all edittext in the recyclerview.
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="Add"
android:textColor="#FFF" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:id="#+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textColor="#FFF" />
<EditText
android:layout_weight="1"
android:id="#+id/numberOfItems"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/add"
android:inputType="phone" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:id="#+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/numberOfItems"
android:text="+"
android:textColor="#FFF" />
<android.support.v7.widget.AppCompatButton
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dc3434"
android:text="Remove"
android:textColor="#FFF" />
</LinearLayout>
adapter
public class BreadAdapter extends RecyclerView.Adapter<BreadAdapter.ViewHolder> {
public List<BreadModel> breads;
Context context;
private int quntity= 0;
public BreadAdapter(List<BreadModel> breads, Context context) {
this.breads = breads;
this.context = context;
}
#NonNull
#Override
public BreadAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_bread_recyclerview, parent, false));
}
#Override
public void onBindViewHolder(#NonNull final BreadAdapter.ViewHolder holder, int position) {
final BreadModel bread_title = breads.get(position);
holder.tv_title.setText(bread_title.getName());
holder.tv_price.setText(String.valueOf(bread_title.getPrice()));
Log.i("info", bread_title.getImg());
GlideApp.with(context)
.load(bread_title.getImg())
holder.increase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
quntity= quntity+ 1;
holder.et_numberOfItems.setText(String.valueOf(quntity));
}
});
}
public void update(){
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return breads.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final EditText et_numberOfItems;
TextView tv_title;
TextView tv_price;
ImageView img_breads;
CardView cv_bread;
AppCompatButton add;
AppCompatButton remove;
AppCompatButton increase;
AppCompatButton decrease;
public ViewHolder(View itemView) {
super(itemView);
et_numberOfItems = (EditText) itemView.findViewById(R.id.numberOfItems);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
tv_price = (TextView) itemView.findViewById(R.id.tv_price);
img_breads = (ImageView) itemView.findViewById(R.id.breadImageViews);
cv_bread = (CardView) itemView.findViewById(R.id.breadCardView);
add = (AppCompatButton) itemView.findViewById(R.id.add);
remove = (AppCompatButton) itemView.findViewById(R.id.remove);
increase = (AppCompatButton) itemView.findViewById(R.id.increase);
decrease = (AppCompatButton) itemView.findViewById(R.id.decrease);
}
}
}
Here is my adapter code please have look at it and help me thanks.
This is the screen shot
If I click on the plus(+) button it increases the number of item on all the edittext instead of that particular item

Recyclerview call contacts Android studio

I have an android app where I have used RecyclerView that shows a list of contacts. Now what I want is that when selecting the contacts, call the indicated number.
For now all it do is show the contacts, but when i click on it it calls, it does not do any action. I do not know how to get it to mark
I tried with onClick() and with button item.But Unsuccessful
My code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
Context mContext;
List<Contact> mData;
Dialog myDialog;
Button button;
public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
this.mContext = mContext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v ;
v = LayoutInflater.from(mContext).inflate(R.layout.single_item_contact,parent,false);
final MyViewHolder vHolder = new MyViewHolder(v);
// Dialog ini
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_contact);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
vHolder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(mContext,"Text Click item : "+String.valueOf(vHolder.getAdapterPosition()),Toast.LENGTH_SHORT).show();
TextView dialog_name_tv = (TextView) myDialog.findViewById(R.id.dialog_name_id);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
myDialog.show();
}
});
return vHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_phone.setText(mData.get(position).getPhone());
holder.img.setImageResource(mData.get(position).getPhoto());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private LinearLayout item_contact;
private TextView tv_name;
private TextView tv_phone;
private ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
item_contact = (LinearLayout) itemView.findViewById(R.id.contact_item_id);
tv_name = (TextView) itemView.findViewById(R.id.name_contact);
tv_phone = (TextView) itemView.findViewById(R.id.phone_contact);
img = (ImageView) itemView.findViewById(R.id.img_contact);
}
}
}
Aquí muestro mi XML de como tengo.
help please
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#232323"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="Contact Name"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/dialog_phone_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:text="Phone Number"
android:textColor="#android:color/white"
android:textSize="23sp" />
<Button
android:id="#+id/dialog_btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="10dp"
android:background="#android:color/white"
android:drawableLeft="#drawable/dialog_call_black"
android:drawablePadding="20dp"
android:padding="20dp"
android:text="Llamar"
android:textAlignment="textStart"
android:textSize="25dp" />
<Button
android:id="#+id/dialog_btn_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/white"
android:drawableLeft="#drawable/dialog_message_black"
android:drawablePadding="20dp"
android:padding="20dp"
android:text="Mensaje"
android:textAlignment="textStart"
android:textSize="25dp" />
</LinearLayout>
<ImageView
android:id="#+id/dialog_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/ic_contacts"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
You need to put setOnClickListener() into OnBindViewHolder().So far what i understand is you have Recycler View in which on item click you want to open a dialog box dialog_contact and this dialog box contains Contact information. After opening this dialog you want to call that person by clicking call button present in dialog.
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_phone.setText(mData.get(position).getPhone());
holder.img.setImageResource(mData.get(position).getPhoto());
holder.item_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDialog = new Dialog(mContext);
myDialog.setContentView(R.layout.dialog_contact);
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
TextView dialog_name_tv = (TextView)myDialog.findViewById(R.id.dialog_name_id);
Button dialog_btn_call= (Button )myDialog.findViewById(R.id.dialog_btn_call);
TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());
dialog_btn_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your code here
myDialog.dismiss();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +mData.get(holder.getAdapterPosition()).getPhone()));
mContext.startActivity(intent);
}
});
}
});
}
Edit:-

Code optimisation. (Architecture)

I'm making a quiz app. User has to finish the phrase shown on display and write the name of the car in the edittext, after pushing on button, if the answer right, edittext become green, if doesn't, become red. If all answers right (green), intent move on next activity.
The question is: how to optimize my code, I don't like how it's look like? If I decide to add some more options it wouldn't be readable.
public class MainActivity extends AppCompatActivity {
EditText et_one_one, et_one_two, et_one_three;
Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_one_one = (EditText) findViewById(R.id.et_one_one);
et_one_two = (EditText) findViewById(R.id.et_one_two);
et_one_three = (EditText) findViewById(R.id.et_one_three);
buttonCheck = (Button) findViewById(R.id.buttonCheck);
buttonCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean allAnswersCorrect = true;
String t1 = et_one_one.getText().toString().toLowerCase();
String t2 = et_one_two.getText().toString().toLowerCase();
String t3 = et_one_three.getText().toString().toLowerCase();
if (t1.equals("maserati")){
et_one_one.setBackgroundColor(Color.GREEN);
}
else {
allAnswersCorrect = false;
et_one_one.setBackgroundColor(Color.RED);
}
if (t2.equals("mercedes")){
et_one_two.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_two.setBackgroundColor(Color.RED);
}
if (t3.equals("bmw")){
et_one_three.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_three.setBackgroundColor(Color.RED);
}
if(allAnswersCorrect) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
});
}
}
In my Layout I use ScrollView:
<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="match_parent" tools:context=".MainActivity"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/task1"
android:id="#+id/textView1"
android:textSize="20sp"
android:textColor="#010101" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_one"
android:id="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_one"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_two"
android:id="#+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_two"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_three"
android:id="#+id/textView4" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_three"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_four"
android:id="#+id/textView5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_four"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textSize="20sp"
android:text="#string/one_five"
android:id="#+id/textView6" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et_one_five"
android:inputType="textCapSentences"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/buttonCheck" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I would strongly suggest a nice library called Butterknife by JakeWharton :
http://jakewharton.github.io/butterknife/
In your case your code would look like this :
public class MainActivity extends AppCompatActivity {
#Bind(R.id.et_one_one) EditText et_one_one;
#Bind(R.id.et_one_two) EditText et_one_two;
#Bind(R.id.et_one_three) EditText et_one_three;
#Bind(R.id.buttonCheck) Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
...
}
#OnClick(R.id.submit)
public void submit(View view) {
// check code here
}
Also you can group all your edit texts in a group :
#Bind({ R.id.et_one_one, R.id.et_one_two, R.id.et_one_three })
List<EditText> nameViews;
And apply some setters or actions on them :
...
ButterKnife.apply(nameViews, LOWERCASE);
...
static final ButterKnife.Action<View> LOWERCASE= new ButterKnife.Action<View>() {
#Override public void apply(View view, int index) {
// TODO set the text of the view to lowercase and disable textview
}
};
You can use RecyclerView to implement this. With it, you can dynamically add as much EditText for cars as you want. The sample is shown as below:
In your Activity:
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private String[] answerArray;
Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
answerArray = new String[]{
"maserati",
"mercedes",
"bmw"
... (you can add as much as you want)
}
buttonCheck = (Button) findViewById(R.id.buttonCheck);
}
For RecyclerView.Adapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
String[] mAnswerArray;
public static class ViewHolder extends RecyclerView.ViewHolder {
public EditText editText;
public ViewHolder(View v) {
super(v);
editText = (EditText) v.findViewById(R.id.editText);
}
}
public MyRecyclerAdapter(String[] answerArray) {
this.mAnswerArray = answerArray;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position){
super.onBindViewHolder(holder, position);
final String answer = mAnswerArray.get(position);
if ( holder.editText.getText().toString().toLowerCase().equals(answer) ) {
holder.editText.setBackgroundColor(Color.GREEN);
} else {
holder.editText.setBackgroundColor(Color.RED);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_list_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public int getItemCount() {
return mAnswerArray.size();
}
}
For "my_list_item.xml", you just need to put inside and as for the ButtonCheck you can also pass an array or flag into adapter to record the state of each answer (correct or wrong) in order to decide whether to go to SecondActivity.

Categories

Resources