Java 如何修复充气异常:充气类com.cepheen.elegantnumberbutton.view.elegantnumberbutton时出错
优雅等级: XMLJava 如何修复充气异常:充气类com.cepheen.elegantnumberbutton.view.elegantnumberbutton时出错,java,android,Java,Android,优雅等级: XML 我希望在每种蔬菜上都有输出优雅的数字按钮。您也能分享一下您的布局吗?看起来您的版面中没有自定义视图的设置。请使用ViewHolder发布版面您可以发布“ElegantNumberButton”类代码共享您的ElegantNumberButton类吗 public class MainHome extends AppCompatActivity { private AppBarConfiguration mAppBarConfiguration; Fireba
我希望在每种蔬菜上都有输出优雅的数字按钮。您也能分享一下您的布局吗?看起来您的版面中没有自定义视图的设置。请使用ViewHolder发布版面您可以发布“ElegantNumberButton”类代码共享您的ElegantNumberButton类吗
public class MainHome extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
FirebaseDatabase firebaseDatabase;
DatabaseReference reference;
ProgressDialog progressDialog;
private List<Vegitable> vegitablesList;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
//Buttons
Button addButton;
ElegantNumberButton countButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_home);
progressDialog = new ProgressDialog(MainHome.this);
progressDialog.setMessage("Please waiting...!");
progressDialog.show();
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Vegitables");
setSupportActionBar(toolbar);
recyclerView =(RecyclerView)findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//get firebase
firebaseDatabase=FirebaseDatabase.getInstance();
reference=firebaseDatabase.getReference("vegetables");
vegitablesList=new ArrayList<>();
loadVegitables();
progressDialog.dismiss();
//Buttons for quntity and add to cart
countButton=(ElegantNumberButton)findViewById(R.id.number_counter);
if(countButton != null) {
countButton.setOnClickListener(new ElegantNumberButton.OnClickListener() {
@Override
public void onClick(View view) {
String num = countButton.getNumber();
Toast.makeText(MainHome.this, "Quantity:" + num, Toast.LENGTH_SHORT).show();
}
});
}
}
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.layout_listitems, parent, false);
return new MyViewHolder(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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="2dp"
android:elevation="@dimen/cell"
android:id="@+id/parent_layout">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher_foreground" />
<TextView
android:id="@+id/nameVegitable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="10dp"
android:textColor="#5396FA"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:id="@+id/inrlogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:textColor="#B87461"/>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="53dp"
android:textColor="#F31F6C"
android:textSize="20sp" />
<Button
android:id="@+id/add_btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="265dp"
android:layout_marginTop="25dp"
android:textSize="15sp"
android:onClick="addCart"
android:text="ADD"
android:background="@drawable/design_button"/>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="@+id/number_counter"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginStart="265dp"
android:layout_marginTop="25dp"
android:background="@drawable/design_button"
android:textSize="8sp"
android:visibility="invisible"
app:initialNumber="0"
app:finalNumber="20"/>
</androidx.cardview.widget.CardView>
</RelativeLayout>
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.example.greenfresh.model.Vegitable;
import com.squareup.picasso.Picasso;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Vegitable> listVegitables;
private Context context;
public MyAdapter(List<Vegitable> listVegitables, Context context) {
this.listVegitables = listVegitables;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.layout_listitems, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
int resourceId = context.getResources().getIdentifier(listVegitables.get(position).getImage(), "drawable",
context.getPackageName());
holder.image.setImageResource(resourceId);
holder.vegitableName.setText(listVegitables.get(position).getName());
holder.price.setText(listVegitables.get(position).getPrice());
holder.qunType.setText(listVegitables.get(position).getQunType());
}
@Override
public int getItemCount() {
return listVegitables.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public TextView vegitableName;
public TextView price;
public TextView qunType;
public ElegantNumberButton countButton;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
image=(ImageView)itemView.findViewById(R.id.image);
vegitableName=(TextView)itemView.findViewById(R.id.nameVegitable);
price=(TextView)itemView.findViewById(R.id.price);
qunType=(TextView)itemView.findViewById(R.id.inrlogo);
countButton=(ElegantNumberButton)itemView.findViewById(R.id.number_counter);
}
}
}
countButton=(ElegantNumberButton)findViewById(R.id.number_counter);
if(countButton != null) {
countButton.setOnClickListener(new ElegantNumberButton.OnClickListener() {
@Override
public void onClick(View view) {
String num = countButton.getNumber();
Toast.makeText(MainHome.this, "Quantity:" + num, Toast.LENGTH_SHORT).show();
}
});
}