gpt4 book ai didi

android - 在我的 Recyclerview 上点击 RecyclerAdapter

转载 作者:行者123 更新时间:2023-11-29 14:16:29 26 4
gpt4 key购买 nike

我试图在我的 RecyclerAdapter 中创建响应式点击事件。我无法理解其背后的逻辑,因为我的数据是由 API 自动创建的,因此我的列表中有一些按钮,我希望通过单击它来获得响应。

这是我的代码:

我的适配器类:

    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();
}

}

我在尝试我的 OnclickEvent 时出错:(在我的//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)

我的 Activity 类:

    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);

}

}

我的卡片 View :

<?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>

我的购物车 View :

enter image description here

(如果你看到“<”和“>”标志,我试图在那里添加响应,但它与我的适配器一起在 RecyclerView 中运行,所以我不知道我是否可以将我的 onclick 事件放在哪里。 )

我在 recyclerview 中的 View (我设置评论我的//TODO :)(它正在运行,但没有加减功能。)

enter image description here

此外,我已经尝试过(holder.add.setOn ...)

enter image description here

还有这个:(MyViewHolder)

enter image description here

最佳答案

这是你需要了解的空指针异常。您应该将 itemView.findViewById(R.id.view_btn_add) 替换为 view.findViewById(R.id.view_btn_add)

因为适配器布局名称是view而不是itemview

看看你的代码

 public MyViewHolder(View view) {
super(view);
}

并且您需要在 MyViewHolder()onBindViewHolder()

中添加点击监听器

移动这段代码

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Add Click response properly.", Toast.LENGTH_SHORT).show();
}
});

关于android - 在我的 Recyclerview 上点击 RecyclerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57916152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com