我想在ListView中显示。像创建产品清单一样,通过单击按钮逐个添加列表中的项目。我要两种布局来做。一个用于添加项目,第二个用于添加新项目。我的代码只行不通,并且未显示错误。
我尝试了两个活动。在两个活动代码中丢失了ArrayList。每次仅显示新的一项。它没有工作,所以我切换到单个活动和两个布局。任何一种解决方案都可以接受。在将来,我必须将其存储在firebase中。在运行时,它不会崩溃。它显示“ I / TextInputLayout:添加的EditText不是TextInputEditText。请改用该类。” 这是实现我想要的正确方法。public class PurchaseBillCreate extends AppCompatActivity {
public ArrayAdapter<String> adapter;
public ArrayList<String> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.purchase_bill_create_activity);
Button buttonOnBill = findViewById(R.id.add_item_btn);
ListView listView = findViewById(R.id.listview_createBill);
arrayList = new ArrayList<>();
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
buttonOnBill.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PurchaseAddItem();
}
});
}
public void PurchaseAddItem() {
setContentView(R.layout.purchase_add_item_activity);
Button buttonAddItemListView = findViewById(R.id.btn_save_item);
buttonAddItemListView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText itemTemp = findViewById(R.id.add_item_name_temp);
EditText quantityTemp = findViewById(R.id.add_item_quantity_temp);
EditText costTemp = findViewById(R.id.add_item_cost_temp);
String item = itemTemp.getText().toString();
String quantity = quantityTemp.getText().toString();
String cost = costTemp.getText().toString();
arrayList.add(item + " " + quantity + " " + cost);
setContentView(R.layout.purchase_bill_create_activity);
adapter.notifyDataSetChanged();
}
});
}
}