我有一个应用程序,其中有ListView
三列。 ListView Adapter
填充了来自Firebase
的数据(产品名称,价格,数量)。 firebase
的数据来自另一个activity
但在我的ListView
我希望第三列(即数量)添加NumberPicker
来选择每种产品的数量。我还想要,例如,如果在firebase
中有可用的quantity : 3
并且用户选择4
,则会显示一条错误消息,显示类似quantity not available
的内容。
对象类代码:
public class Produse {
public String produsId;
public String produs;
public String pret;
public String cantitate;
public Produse(){
}
public Produse(String produsId, String produs, String pret, String cantitate) {
this.produsId = produsId;
this.produs = produs;
this.pret = pret;
this.cantitate = cantitate;
}
public String getProdusId() {
return produsId;
}
public String getProdus() {
return produs;
}
public String getPret() {
return pret;
}
public String getCantitate() {
return cantitate;
}
}
ListView适配器代码
public class ProduseList extends ArrayAdapter<Produse> {
private Activity context;
private List<Produse> produseList;
public ProduseList(Activity context, List<Produse> produseList) {
super(context, R.layout.list_layout, produseList);
this.context = context;
this.produseList = produseList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
TextView textViewProdus = (TextView) listViewItem.findViewById(R.id.textViewProdus);
TextView textViewPret = (TextView) listViewItem.findViewById(R.id.textViewPret);
TextView textViewCantitate = (TextView) listViewItem.findViewById(R.id.textViewCantitate);
Produse produse = produseList.get(position);
textViewProdus.setText(produse.getProdus());
textViewPret.setText(produse.getPret());
textViewCantitate.setText(produse.getCantitate());
return listViewItem;
}
}
列的布局:
<TextView
android:id="@+id/textViewProdus"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_marginTop="2.5dp"
android:gravity="center_horizontal"
android:descendantFocusability="blocksDescendants"
android:text="Produs"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"
android:textSize="14.5dp"
android:padding="7dp"
android:layout_weight="1" />
<TextView
android:id="@+id/textViewPret"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="2.5dp"
android:gravity="center_horizontal"
android:descendantFocusability="blocksDescendants"
android:text="Suma"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
android:textSize="14dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textViewCantitate"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="2.5dp"
android:gravity="center_horizontal"
android:descendantFocusability="blocksDescendants"
android:text="0"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
android:textSize="14dp"
android:padding="7dp"
android:layout_weight="1"/>
在我的代码中产品名称= produs,price = pret和quantity = cantitate 。
希望你明白的人我真的相信你。
答案 0 :(得分:0)
尝试以下代码,您应该编辑类似&#34;密钥数量产品&#34;你存放在firebase上。
public class ProduseList extends ArrayAdapter<Produse> {
private Activity context;
private List<Produse> produseList;
public ProduseList(Activity context, List<Produse> produseList) {
super(context, R.layout.list_layout, produseList);
this.context = context;
this.produseList = produseList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
TextView textViewProdus = (TextView) listViewItem.findViewById(R.id.textViewProdus);
TextView textViewPret = (TextView) listViewItem.findViewById(R.id.textViewPret);
final NumberPicker numCantitate = (NumberPicker) listViewItem.findViewById(R.id.textViewCantitate);
Produse produse = produseList.get(position);
textViewProdus.setText(produse.getProdus());
textViewPret.setText(produse.getPret());
numCantitate.setMaxValue(100);
numCantitate.setMinValue(0);
numCantitate.setWrapSelectorWheel(true);
//Set a value change listener for NumberPicker
numCantitate.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, final int oldVal, final int newVal){
//get your valid quantity from firebase server
// Get a reference to our posts
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/quantity/product");
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int validQuantity = dataSnapshot.getValue(Integer.class);
if(newVal <= validQuantity){
//Display the newly selected number from picker
numCantitate.setValue(newVal);
}else {
numCantitate.setValue(oldVal);
Toast.makeText(getContext(), "Wrong quantity.....", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
});
return listViewItem;
}
}