我有一个基本形式,该形式在mobx状态树中具有一个模型(或商店),具有添加新项目和删除项目的属性。我将模型实例作为道具传递给组件。
class HomeBrandCVC extends ArrayAdapter<Brand> {
private Context context;
private ArrayList<Brand> brandArrayList;
private Map<Integer, File> fileMap = new HashMap<>();
public HomeBrandCVC(Context c, ArrayList<Brand> brandArrayList) {
super(c, R.layout.homecvc, brandArrayList);
this.context = c;
this.brandArrayList = brandArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.homecvc, parent, false);
TextView nameLabel = (TextView) rowView.findViewById(R.id.homecvc_nameLabel);
nameLabel.setVisibility(View.INVISIBLE);
final Brand brand = brandArrayList.get(position);
nameLabel.setText(brand.name);
ImageView viewImage = (ImageView) rowView.findViewById(R.id.homecvc_viewImage);
Log.d("hellllo", String.valueOf(position));
if (fileMap.containsKey(position)) {
viewImage.setImageBitmap(BitmapFactory.decodeFile(fileMap.get(position).getAbsolutePath()));
} else {
viewImage.setImageResource(R.drawable.loadingimage);
new MyLeanCloudApp.GetImageFromDiskOrUrl(brand.imageFile, new MyLeanCloudApp.ImageHandlerGetImagesResponse() {
@Override
public void processFinish(File file) {
if (context != null) {
if (file != null) {
Picasso.with(context).load(file).into(viewImage);
fileMap.put(position, file);
}
}
}
}).execute();
}
return rowView;
}
}
我想测试何时单击表单上的提交按钮,调用实例上的addItem()方法并添加新项目。这是我到目前为止的内容:
My Component:
render() {
const {itemList} = this.props;
return (
<div className="container">
<h1>Shopping Items Calculator</h1>
<form
onSubmit={e => {
e.preventDefault();
itemList.addItem({
name: this.state.name,
quantity: parseInt(this.state.quantity, 10),
price: parseFloat(this.state.price),
});
this.setState({
name: '',
quantity: '',
price: '',
});
}}>
My model:
const ItemList = types
.model('ItemList', {
items: types.array(Item),
})
.actions(self => ({
addItem(item) {
self.items.push(item);
},
deleteItem(item) {
return destroy(item);
},
))
.views(self => ({
totalItemsPrice() {
return self.items.reduce((sum, item) => sum + item.total(), 0);
},
}));
我正在使用玩笑和酵素。感谢您的任何帮助,谢谢!