- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在我的 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 :
(如果你看到“<”和“>”标志,我试图在那里添加响应,但它与我的适配器一起在 RecyclerView 中运行,所以我不知道我是否可以将我的 onclick 事件放在哪里。 )
我在 recyclerview 中的 View (我设置评论我的//TODO :)(它正在运行,但没有加减功能。)
此外,我已经尝试过(holder.add.setOn ...)
还有这个:(MyViewHolder)
最佳答案
这是你需要了解的空指针异常。您应该将 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/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!