I want to get the id when I click a RecyclerView item












0















when clicked the item of recycleview.i want to get the ID of the item of and use them in another Service in Main activity recycleview is working fine just want get id from adapter and use it to main activity here is my code of adapter and main Activity class in which show the



    private void loadrent() {
l.setVisibility(View.GONE);

shimmerLayout.setVisibility(View.VISIBLE);
shimmerLayout.startShimmerAnimation();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URLs.user_rent_data_guest,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Integer a;
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
//converting the string to json array object
JSONObject jsonResponse = new JSONObject(response);
JSONArray array =jsonResponse.getJSONArray("data");
// JSONArray arrays =jsonResponse.getJSONArray("0");
//traversing through all the object
if(array != null && array.length() > 0 ) {
l.setVisibility(View.INVISIBLE);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product =array.getJSONObject(i);
//JSONObject products =arrays.getJSONObject(i);
//adding the product to product lists
productList.add(new Rent(
product.getInt("id"),
product.getString("tags"),
product.getString("address"),
product.getString("details"),
product.getString("amount"),
product.getString("cover_image"),
product.getString("location"),
product.getString("contact"),
product.getString("status"),
product.getString("no_of_bedrooms"),
product.getString("no_of_bathrooms"),
product.getString("property_floor"),
product.getString("created_at"),
product.getString("property_name")
));
}
}
else {
l.setVisibility(View.VISIBLE);
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
//creating adapter object and setting it to recyclerview
adapter = new rentgestAdapter(SubmitPost.this,productList);
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
});

//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}



public class rentgestAdapter extends
RecyclerView.Adapter<rentgestAdapter.rentgestAdapterholder> {


private Context mCtx;
private List<Rent> productList;
private String time,s;
Date currentTimes;
Integer soft;
Date date1,date2;
Date today;

public rentgestAdapter(Context mCtx, List<Rent> productList) {
this.mCtx = mCtx;
this.productList = productList;
}

@Override
public rentgestAdapterholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new rentgestAdapterholder(view);
}


@Override
public void onBindViewHolder(rentgestAdapterholder holder, int position) {
final Rent product = productList.get(position);


//loading the image
//loading the image

holder.lsItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Integer s=product.getId();


}
});
AssetManager am = mCtx.getApplicationContext().getAssets();
Typeface face= Typeface.createFromAsset(am,"fonts/nobellights.ttf");
Typeface face1= Typeface.createFromAsset(am,"fonts/NobelBold.ttf");
holder.textViewamunt.setTypeface(face1);
holder.name.setTypeface(face);
holder.textViewtag.setTypeface(face);
holder.bed.setTypeface(face);
holder.bath.setTypeface(face);
holder.floor.setTypeface(face);
holder.textViewaddress.setTypeface(face);
holder.textViewdetail.setTypeface(face);


holder.textViewamunt.setText(String.valueOf(product.getPrice())+" PKR");
holder.name.setText(product.getname());
holder.textViewaddress.setText(String.valueOf(product.get_address()));
// holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
if(product.get_sale_tag().equals(""))
{
holder.textViewtag.setVisibility(View.INVISIBLE);
}
else{
holder.textViewtag.setVisibility(View.VISIBLE);
holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
}
holder.textViewdetail.setText(String.valueOf(product.getdetails()));
if(product.getbed().equals(""))
{
holder.bed.setVisibility(View.INVISIBLE);

}
else{
holder.bed.setVisibility(View.VISIBLE);
holder.bed.setText(String.valueOf(product.getbed())+" Bed");

}
if(product.getBath().equals(""))
{
holder.bath.setVisibility(View.INVISIBLE);

}
else{
holder.bath.setVisibility(View.VISIBLE);
holder.bath.setText(String.valueOf(product.getBath())+" Bath");

}
if(product.getFloor().equals(""))
{
holder.floor.setVisibility(View.INVISIBLE);

}
else{
holder.floor.setVisibility(View.VISIBLE);
holder.floor.setText(String.valueOf(product.getFloor())+" Floor");

}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
try {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
dateFormatter.setLenient(false);
today = new Date();
s = dateFormatter.format(today);
date1 = simpleDateFormat.parse(product.getcreated_at());
date2 = simpleDateFormat.parse(s);
printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
holder.t.setText(String.valueOf(time));
holder.shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
Log.e("ssssss", "click " + checked);
}
});
}

@Override
public int getItemCount() {
return productList.size();
}
public String printDifference(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();

System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);


long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;

long elapsedDays = different / daysInMilli;
different = different % daysInMilli;

long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;

long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;

long elapsedSeconds = different / secondsInMilli;

System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
time= elapsedDays+" Days "+" "+elapsedHours+" Hours "+" "+elapsedMinutes+" Minutes";
return time;
}

class rentgestAdapterholder extends RecyclerView.ViewHolder {
ShineButton shineButton ;

TextView textViewamunt, textViewdetail, textViewaddress, textViewtag,bed,bath,floor,t,name;
ImageView imageView;
public android.support.v7.widget.CardView lsItem;



public rentgestAdapterholder(View itemView) {
super(itemView);
shineButton= (ShineButton) itemView.findViewById(R.id.po_image2);
textViewamunt = (TextView) itemView.findViewById(R.id.pkramount);
name = (TextView) itemView.findViewById(R.id.name);
textViewdetail = (TextView)itemView.findViewById(R.id.detail);
t = (TextView)itemView.findViewById(R.id.t);
bed = (TextView) itemView.findViewById(R.id.bed);
bath = (TextView) itemView.findViewById(R.id.bath);
floor = (TextView) itemView.findViewById(R.id.floor);
textViewtag = (TextView) itemView.findViewById(R.id.tag);
textViewaddress = (TextView) itemView.findViewById(R.id.address);
imageView = (ImageView) itemView.findViewById(R.id.simage);
lsItem = (android.support.v7.widget.CardView) itemView.findViewById(R.id.lsItem);

}
}
}









share|improve this question

























  • Use an interface to pass the id back to the activity.

    – teh_raab
    Jan 2 at 16:53
















0















when clicked the item of recycleview.i want to get the ID of the item of and use them in another Service in Main activity recycleview is working fine just want get id from adapter and use it to main activity here is my code of adapter and main Activity class in which show the



    private void loadrent() {
l.setVisibility(View.GONE);

shimmerLayout.setVisibility(View.VISIBLE);
shimmerLayout.startShimmerAnimation();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URLs.user_rent_data_guest,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Integer a;
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
//converting the string to json array object
JSONObject jsonResponse = new JSONObject(response);
JSONArray array =jsonResponse.getJSONArray("data");
// JSONArray arrays =jsonResponse.getJSONArray("0");
//traversing through all the object
if(array != null && array.length() > 0 ) {
l.setVisibility(View.INVISIBLE);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product =array.getJSONObject(i);
//JSONObject products =arrays.getJSONObject(i);
//adding the product to product lists
productList.add(new Rent(
product.getInt("id"),
product.getString("tags"),
product.getString("address"),
product.getString("details"),
product.getString("amount"),
product.getString("cover_image"),
product.getString("location"),
product.getString("contact"),
product.getString("status"),
product.getString("no_of_bedrooms"),
product.getString("no_of_bathrooms"),
product.getString("property_floor"),
product.getString("created_at"),
product.getString("property_name")
));
}
}
else {
l.setVisibility(View.VISIBLE);
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
//creating adapter object and setting it to recyclerview
adapter = new rentgestAdapter(SubmitPost.this,productList);
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
});

//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}



public class rentgestAdapter extends
RecyclerView.Adapter<rentgestAdapter.rentgestAdapterholder> {


private Context mCtx;
private List<Rent> productList;
private String time,s;
Date currentTimes;
Integer soft;
Date date1,date2;
Date today;

public rentgestAdapter(Context mCtx, List<Rent> productList) {
this.mCtx = mCtx;
this.productList = productList;
}

@Override
public rentgestAdapterholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new rentgestAdapterholder(view);
}


@Override
public void onBindViewHolder(rentgestAdapterholder holder, int position) {
final Rent product = productList.get(position);


//loading the image
//loading the image

holder.lsItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Integer s=product.getId();


}
});
AssetManager am = mCtx.getApplicationContext().getAssets();
Typeface face= Typeface.createFromAsset(am,"fonts/nobellights.ttf");
Typeface face1= Typeface.createFromAsset(am,"fonts/NobelBold.ttf");
holder.textViewamunt.setTypeface(face1);
holder.name.setTypeface(face);
holder.textViewtag.setTypeface(face);
holder.bed.setTypeface(face);
holder.bath.setTypeface(face);
holder.floor.setTypeface(face);
holder.textViewaddress.setTypeface(face);
holder.textViewdetail.setTypeface(face);


holder.textViewamunt.setText(String.valueOf(product.getPrice())+" PKR");
holder.name.setText(product.getname());
holder.textViewaddress.setText(String.valueOf(product.get_address()));
// holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
if(product.get_sale_tag().equals(""))
{
holder.textViewtag.setVisibility(View.INVISIBLE);
}
else{
holder.textViewtag.setVisibility(View.VISIBLE);
holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
}
holder.textViewdetail.setText(String.valueOf(product.getdetails()));
if(product.getbed().equals(""))
{
holder.bed.setVisibility(View.INVISIBLE);

}
else{
holder.bed.setVisibility(View.VISIBLE);
holder.bed.setText(String.valueOf(product.getbed())+" Bed");

}
if(product.getBath().equals(""))
{
holder.bath.setVisibility(View.INVISIBLE);

}
else{
holder.bath.setVisibility(View.VISIBLE);
holder.bath.setText(String.valueOf(product.getBath())+" Bath");

}
if(product.getFloor().equals(""))
{
holder.floor.setVisibility(View.INVISIBLE);

}
else{
holder.floor.setVisibility(View.VISIBLE);
holder.floor.setText(String.valueOf(product.getFloor())+" Floor");

}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
try {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
dateFormatter.setLenient(false);
today = new Date();
s = dateFormatter.format(today);
date1 = simpleDateFormat.parse(product.getcreated_at());
date2 = simpleDateFormat.parse(s);
printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
holder.t.setText(String.valueOf(time));
holder.shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
Log.e("ssssss", "click " + checked);
}
});
}

@Override
public int getItemCount() {
return productList.size();
}
public String printDifference(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();

System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);


long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;

long elapsedDays = different / daysInMilli;
different = different % daysInMilli;

long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;

long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;

long elapsedSeconds = different / secondsInMilli;

System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
time= elapsedDays+" Days "+" "+elapsedHours+" Hours "+" "+elapsedMinutes+" Minutes";
return time;
}

class rentgestAdapterholder extends RecyclerView.ViewHolder {
ShineButton shineButton ;

TextView textViewamunt, textViewdetail, textViewaddress, textViewtag,bed,bath,floor,t,name;
ImageView imageView;
public android.support.v7.widget.CardView lsItem;



public rentgestAdapterholder(View itemView) {
super(itemView);
shineButton= (ShineButton) itemView.findViewById(R.id.po_image2);
textViewamunt = (TextView) itemView.findViewById(R.id.pkramount);
name = (TextView) itemView.findViewById(R.id.name);
textViewdetail = (TextView)itemView.findViewById(R.id.detail);
t = (TextView)itemView.findViewById(R.id.t);
bed = (TextView) itemView.findViewById(R.id.bed);
bath = (TextView) itemView.findViewById(R.id.bath);
floor = (TextView) itemView.findViewById(R.id.floor);
textViewtag = (TextView) itemView.findViewById(R.id.tag);
textViewaddress = (TextView) itemView.findViewById(R.id.address);
imageView = (ImageView) itemView.findViewById(R.id.simage);
lsItem = (android.support.v7.widget.CardView) itemView.findViewById(R.id.lsItem);

}
}
}









share|improve this question

























  • Use an interface to pass the id back to the activity.

    – teh_raab
    Jan 2 at 16:53














0












0








0








when clicked the item of recycleview.i want to get the ID of the item of and use them in another Service in Main activity recycleview is working fine just want get id from adapter and use it to main activity here is my code of adapter and main Activity class in which show the



    private void loadrent() {
l.setVisibility(View.GONE);

shimmerLayout.setVisibility(View.VISIBLE);
shimmerLayout.startShimmerAnimation();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URLs.user_rent_data_guest,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Integer a;
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
//converting the string to json array object
JSONObject jsonResponse = new JSONObject(response);
JSONArray array =jsonResponse.getJSONArray("data");
// JSONArray arrays =jsonResponse.getJSONArray("0");
//traversing through all the object
if(array != null && array.length() > 0 ) {
l.setVisibility(View.INVISIBLE);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product =array.getJSONObject(i);
//JSONObject products =arrays.getJSONObject(i);
//adding the product to product lists
productList.add(new Rent(
product.getInt("id"),
product.getString("tags"),
product.getString("address"),
product.getString("details"),
product.getString("amount"),
product.getString("cover_image"),
product.getString("location"),
product.getString("contact"),
product.getString("status"),
product.getString("no_of_bedrooms"),
product.getString("no_of_bathrooms"),
product.getString("property_floor"),
product.getString("created_at"),
product.getString("property_name")
));
}
}
else {
l.setVisibility(View.VISIBLE);
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
//creating adapter object and setting it to recyclerview
adapter = new rentgestAdapter(SubmitPost.this,productList);
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
});

//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}



public class rentgestAdapter extends
RecyclerView.Adapter<rentgestAdapter.rentgestAdapterholder> {


private Context mCtx;
private List<Rent> productList;
private String time,s;
Date currentTimes;
Integer soft;
Date date1,date2;
Date today;

public rentgestAdapter(Context mCtx, List<Rent> productList) {
this.mCtx = mCtx;
this.productList = productList;
}

@Override
public rentgestAdapterholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new rentgestAdapterholder(view);
}


@Override
public void onBindViewHolder(rentgestAdapterholder holder, int position) {
final Rent product = productList.get(position);


//loading the image
//loading the image

holder.lsItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Integer s=product.getId();


}
});
AssetManager am = mCtx.getApplicationContext().getAssets();
Typeface face= Typeface.createFromAsset(am,"fonts/nobellights.ttf");
Typeface face1= Typeface.createFromAsset(am,"fonts/NobelBold.ttf");
holder.textViewamunt.setTypeface(face1);
holder.name.setTypeface(face);
holder.textViewtag.setTypeface(face);
holder.bed.setTypeface(face);
holder.bath.setTypeface(face);
holder.floor.setTypeface(face);
holder.textViewaddress.setTypeface(face);
holder.textViewdetail.setTypeface(face);


holder.textViewamunt.setText(String.valueOf(product.getPrice())+" PKR");
holder.name.setText(product.getname());
holder.textViewaddress.setText(String.valueOf(product.get_address()));
// holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
if(product.get_sale_tag().equals(""))
{
holder.textViewtag.setVisibility(View.INVISIBLE);
}
else{
holder.textViewtag.setVisibility(View.VISIBLE);
holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
}
holder.textViewdetail.setText(String.valueOf(product.getdetails()));
if(product.getbed().equals(""))
{
holder.bed.setVisibility(View.INVISIBLE);

}
else{
holder.bed.setVisibility(View.VISIBLE);
holder.bed.setText(String.valueOf(product.getbed())+" Bed");

}
if(product.getBath().equals(""))
{
holder.bath.setVisibility(View.INVISIBLE);

}
else{
holder.bath.setVisibility(View.VISIBLE);
holder.bath.setText(String.valueOf(product.getBath())+" Bath");

}
if(product.getFloor().equals(""))
{
holder.floor.setVisibility(View.INVISIBLE);

}
else{
holder.floor.setVisibility(View.VISIBLE);
holder.floor.setText(String.valueOf(product.getFloor())+" Floor");

}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
try {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
dateFormatter.setLenient(false);
today = new Date();
s = dateFormatter.format(today);
date1 = simpleDateFormat.parse(product.getcreated_at());
date2 = simpleDateFormat.parse(s);
printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
holder.t.setText(String.valueOf(time));
holder.shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
Log.e("ssssss", "click " + checked);
}
});
}

@Override
public int getItemCount() {
return productList.size();
}
public String printDifference(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();

System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);


long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;

long elapsedDays = different / daysInMilli;
different = different % daysInMilli;

long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;

long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;

long elapsedSeconds = different / secondsInMilli;

System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
time= elapsedDays+" Days "+" "+elapsedHours+" Hours "+" "+elapsedMinutes+" Minutes";
return time;
}

class rentgestAdapterholder extends RecyclerView.ViewHolder {
ShineButton shineButton ;

TextView textViewamunt, textViewdetail, textViewaddress, textViewtag,bed,bath,floor,t,name;
ImageView imageView;
public android.support.v7.widget.CardView lsItem;



public rentgestAdapterholder(View itemView) {
super(itemView);
shineButton= (ShineButton) itemView.findViewById(R.id.po_image2);
textViewamunt = (TextView) itemView.findViewById(R.id.pkramount);
name = (TextView) itemView.findViewById(R.id.name);
textViewdetail = (TextView)itemView.findViewById(R.id.detail);
t = (TextView)itemView.findViewById(R.id.t);
bed = (TextView) itemView.findViewById(R.id.bed);
bath = (TextView) itemView.findViewById(R.id.bath);
floor = (TextView) itemView.findViewById(R.id.floor);
textViewtag = (TextView) itemView.findViewById(R.id.tag);
textViewaddress = (TextView) itemView.findViewById(R.id.address);
imageView = (ImageView) itemView.findViewById(R.id.simage);
lsItem = (android.support.v7.widget.CardView) itemView.findViewById(R.id.lsItem);

}
}
}









share|improve this question
















when clicked the item of recycleview.i want to get the ID of the item of and use them in another Service in Main activity recycleview is working fine just want get id from adapter and use it to main activity here is my code of adapter and main Activity class in which show the



    private void loadrent() {
l.setVisibility(View.GONE);

shimmerLayout.setVisibility(View.VISIBLE);
shimmerLayout.startShimmerAnimation();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URLs.user_rent_data_guest,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Integer a;
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
//converting the string to json array object
JSONObject jsonResponse = new JSONObject(response);
JSONArray array =jsonResponse.getJSONArray("data");
// JSONArray arrays =jsonResponse.getJSONArray("0");
//traversing through all the object
if(array != null && array.length() > 0 ) {
l.setVisibility(View.INVISIBLE);
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product =array.getJSONObject(i);
//JSONObject products =arrays.getJSONObject(i);
//adding the product to product lists
productList.add(new Rent(
product.getInt("id"),
product.getString("tags"),
product.getString("address"),
product.getString("details"),
product.getString("amount"),
product.getString("cover_image"),
product.getString("location"),
product.getString("contact"),
product.getString("status"),
product.getString("no_of_bedrooms"),
product.getString("no_of_bathrooms"),
product.getString("property_floor"),
product.getString("created_at"),
product.getString("property_name")
));
}
}
else {
l.setVisibility(View.VISIBLE);
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
//creating adapter object and setting it to recyclerview
adapter = new rentgestAdapter(SubmitPost.this,productList);
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
shimmerLayout.stopShimmerAnimation();
shimmerLayout.setVisibility(View.GONE);
}
});

//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}



public class rentgestAdapter extends
RecyclerView.Adapter<rentgestAdapter.rentgestAdapterholder> {


private Context mCtx;
private List<Rent> productList;
private String time,s;
Date currentTimes;
Integer soft;
Date date1,date2;
Date today;

public rentgestAdapter(Context mCtx, List<Rent> productList) {
this.mCtx = mCtx;
this.productList = productList;
}

@Override
public rentgestAdapterholder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new rentgestAdapterholder(view);
}


@Override
public void onBindViewHolder(rentgestAdapterholder holder, int position) {
final Rent product = productList.get(position);


//loading the image
//loading the image

holder.lsItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Integer s=product.getId();


}
});
AssetManager am = mCtx.getApplicationContext().getAssets();
Typeface face= Typeface.createFromAsset(am,"fonts/nobellights.ttf");
Typeface face1= Typeface.createFromAsset(am,"fonts/NobelBold.ttf");
holder.textViewamunt.setTypeface(face1);
holder.name.setTypeface(face);
holder.textViewtag.setTypeface(face);
holder.bed.setTypeface(face);
holder.bath.setTypeface(face);
holder.floor.setTypeface(face);
holder.textViewaddress.setTypeface(face);
holder.textViewdetail.setTypeface(face);


holder.textViewamunt.setText(String.valueOf(product.getPrice())+" PKR");
holder.name.setText(product.getname());
holder.textViewaddress.setText(String.valueOf(product.get_address()));
// holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
if(product.get_sale_tag().equals(""))
{
holder.textViewtag.setVisibility(View.INVISIBLE);
}
else{
holder.textViewtag.setVisibility(View.VISIBLE);
holder.textViewtag.setText(String.valueOf(product.get_sale_tag()));
}
holder.textViewdetail.setText(String.valueOf(product.getdetails()));
if(product.getbed().equals(""))
{
holder.bed.setVisibility(View.INVISIBLE);

}
else{
holder.bed.setVisibility(View.VISIBLE);
holder.bed.setText(String.valueOf(product.getbed())+" Bed");

}
if(product.getBath().equals(""))
{
holder.bath.setVisibility(View.INVISIBLE);

}
else{
holder.bath.setVisibility(View.VISIBLE);
holder.bath.setText(String.valueOf(product.getBath())+" Bath");

}
if(product.getFloor().equals(""))
{
holder.floor.setVisibility(View.INVISIBLE);

}
else{
holder.floor.setVisibility(View.VISIBLE);
holder.floor.setText(String.valueOf(product.getFloor())+" Floor");

}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
try {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
dateFormatter.setLenient(false);
today = new Date();
s = dateFormatter.format(today);
date1 = simpleDateFormat.parse(product.getcreated_at());
date2 = simpleDateFormat.parse(s);
printDifference(date1, date2);
} catch (ParseException e) {
e.printStackTrace();
}
holder.t.setText(String.valueOf(time));
holder.shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(View view, boolean checked) {
Log.e("ssssss", "click " + checked);
}
});
}

@Override
public int getItemCount() {
return productList.size();
}
public String printDifference(Date startDate, Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();

System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);


long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;

long elapsedDays = different / daysInMilli;
different = different % daysInMilli;

long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;

long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;

long elapsedSeconds = different / secondsInMilli;

System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
time= elapsedDays+" Days "+" "+elapsedHours+" Hours "+" "+elapsedMinutes+" Minutes";
return time;
}

class rentgestAdapterholder extends RecyclerView.ViewHolder {
ShineButton shineButton ;

TextView textViewamunt, textViewdetail, textViewaddress, textViewtag,bed,bath,floor,t,name;
ImageView imageView;
public android.support.v7.widget.CardView lsItem;



public rentgestAdapterholder(View itemView) {
super(itemView);
shineButton= (ShineButton) itemView.findViewById(R.id.po_image2);
textViewamunt = (TextView) itemView.findViewById(R.id.pkramount);
name = (TextView) itemView.findViewById(R.id.name);
textViewdetail = (TextView)itemView.findViewById(R.id.detail);
t = (TextView)itemView.findViewById(R.id.t);
bed = (TextView) itemView.findViewById(R.id.bed);
bath = (TextView) itemView.findViewById(R.id.bath);
floor = (TextView) itemView.findViewById(R.id.floor);
textViewtag = (TextView) itemView.findViewById(R.id.tag);
textViewaddress = (TextView) itemView.findViewById(R.id.address);
imageView = (ImageView) itemView.findViewById(R.id.simage);
lsItem = (android.support.v7.widget.CardView) itemView.findViewById(R.id.lsItem);

}
}
}






java android android-recyclerview adapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:16









Fantômas

32.8k156490




32.8k156490










asked Jan 2 at 16:50









Usama HassanUsama Hassan

165




165













  • Use an interface to pass the id back to the activity.

    – teh_raab
    Jan 2 at 16:53



















  • Use an interface to pass the id back to the activity.

    – teh_raab
    Jan 2 at 16:53

















Use an interface to pass the id back to the activity.

– teh_raab
Jan 2 at 16:53





Use an interface to pass the id back to the activity.

– teh_raab
Jan 2 at 16:53












1 Answer
1






active

oldest

votes


















0














First: You could create a interface for providing callback listener:



interface UpdateActivityListener {
void itemClicked(int id);
}


Second: Your adapter should keep listener, but activity should implements:



public rentgestAdapter(Context mCtx, List<Rent> productList, UpdateActivityListener listener) {
this.mCtx = mCtx;
this.productList = productList;
this.listener = listener;
}


Third: Set to your view default setOnClickListener and call your listener with pass id.



view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// holder.getAdapterPosition() returns click item position
listener.itemClicked(holder.getAdapterPosition());
}
});





share|improve this answer
























  • how can i get id in Activity

    – Usama Hassan
    Jan 3 at 9:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010153%2fi-want-to-get-the-id-when-i-click-a-recyclerview-item%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First: You could create a interface for providing callback listener:



interface UpdateActivityListener {
void itemClicked(int id);
}


Second: Your adapter should keep listener, but activity should implements:



public rentgestAdapter(Context mCtx, List<Rent> productList, UpdateActivityListener listener) {
this.mCtx = mCtx;
this.productList = productList;
this.listener = listener;
}


Third: Set to your view default setOnClickListener and call your listener with pass id.



view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// holder.getAdapterPosition() returns click item position
listener.itemClicked(holder.getAdapterPosition());
}
});





share|improve this answer
























  • how can i get id in Activity

    – Usama Hassan
    Jan 3 at 9:22
















0














First: You could create a interface for providing callback listener:



interface UpdateActivityListener {
void itemClicked(int id);
}


Second: Your adapter should keep listener, but activity should implements:



public rentgestAdapter(Context mCtx, List<Rent> productList, UpdateActivityListener listener) {
this.mCtx = mCtx;
this.productList = productList;
this.listener = listener;
}


Third: Set to your view default setOnClickListener and call your listener with pass id.



view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// holder.getAdapterPosition() returns click item position
listener.itemClicked(holder.getAdapterPosition());
}
});





share|improve this answer
























  • how can i get id in Activity

    – Usama Hassan
    Jan 3 at 9:22














0












0








0







First: You could create a interface for providing callback listener:



interface UpdateActivityListener {
void itemClicked(int id);
}


Second: Your adapter should keep listener, but activity should implements:



public rentgestAdapter(Context mCtx, List<Rent> productList, UpdateActivityListener listener) {
this.mCtx = mCtx;
this.productList = productList;
this.listener = listener;
}


Third: Set to your view default setOnClickListener and call your listener with pass id.



view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// holder.getAdapterPosition() returns click item position
listener.itemClicked(holder.getAdapterPosition());
}
});





share|improve this answer













First: You could create a interface for providing callback listener:



interface UpdateActivityListener {
void itemClicked(int id);
}


Second: Your adapter should keep listener, but activity should implements:



public rentgestAdapter(Context mCtx, List<Rent> productList, UpdateActivityListener listener) {
this.mCtx = mCtx;
this.productList = productList;
this.listener = listener;
}


Third: Set to your view default setOnClickListener and call your listener with pass id.



view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// holder.getAdapterPosition() returns click item position
listener.itemClicked(holder.getAdapterPosition());
}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:01









Dmitro IvanovDmitro Ivanov

748149




748149













  • how can i get id in Activity

    – Usama Hassan
    Jan 3 at 9:22



















  • how can i get id in Activity

    – Usama Hassan
    Jan 3 at 9:22

















how can i get id in Activity

– Usama Hassan
Jan 3 at 9:22





how can i get id in Activity

– Usama Hassan
Jan 3 at 9:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010153%2fi-want-to-get-the-id-when-i-click-a-recyclerview-item%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith