当单击recycleview.i的项目时,我想获取该项目的ID并在Main活动的另一个服务中使用它们,recycleview工作正常,只是想从适配器获取ID并将其用于主要活动,这是我的代码适配器和其中显示
的主Activity类 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);
}
}
}
答案 0 :(得分:0)
首先:您可以创建一个用于提供回调监听器的接口:
result = [t for t in data if max(Counter(t).values()) < 3]
第二:您的适配器应保留侦听器,但活动应实现:
interface UpdateActivityListener {
void itemClicked(int id);
}
第三步::设置为您的视图默认值public rentgestAdapter(Context mCtx, List<Rent> productList, UpdateActivityListener listener) {
this.mCtx = mCtx;
this.productList = productList;
this.listener = listener;
}
,并使用传递ID呼叫监听器。
setOnClickListener