我试着用这个教程做一个简单的库存应用程序,允许添加/查看/更新/删除(https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-1-introduction)它使用room、viewmodel、livedata在sqllitedb中存储数据。问题是,每当我试图打开“note”或“listing”时,“quantity”和“price”都会自动将自己设置为“priority”值。在尝试编辑单个列表时,我只能更新“string”类型,即description和title,而“int”类型的price和quantity与numberpicker设置的优先级相同。
我是一个非常新的编码,所以任何帮助或提示的方向进行将非常感谢。
下面是我的addeditnoteactivity.java:
public class AddEditNoteActivity extends AppCompatActivity {
public static final String EXTRA_ID =
"com.example.stock_management.EXTRA_ID";
public static final String EXTRA_TITLE =
"com.example.stock_management.EXTRA_TITLE";
public static final String EXTRA_DESCRIPTION =
"com.example.stock_management.EXTRA_ID.EXTRA_DESCRIPTION";
public static final String EXTRA_PRIORITY =
"com.example.stock_management.EXTRA_ID.EXTRA_PRIORITY";
public static final String EXTRA_PRICE =
"com.example.stock_management.EXTRA_ID.EXTRA_PRIORITY";
public static final String EXTRA_QUANTITY =
"com.example.stock_management.EXTRA_ID.EXTRA_PRIORITY";
private EditText editTextTitle;
private EditText editTextDescription;
private NumberPicker numberPickerPriority;
private EditText editTextPrice;
private EditText editTextQuantity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
editTextTitle = findViewById(R.id.edit_text_title);
editTextDescription = findViewById(R.id.edit_text_description);
editTextPrice = findViewById(R.id.edit_text_price);
editTextQuantity = findViewById(R.id.edit_text_quantity);
numberPickerPriority = findViewById(R.id.number_picker_priority);
numberPickerPriority.setMinValue(1);
numberPickerPriority.setMaxValue(10);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
Intent intent = getIntent();
if (intent.hasExtra(EXTRA_ID)) {
setTitle("Edit Note");
editTextTitle.setText(intent.getStringExtra(EXTRA_TITLE));
editTextDescription.setText(intent.getStringExtra(EXTRA_DESCRIPTION));
numberPickerPriority.setValue(intent.getIntExtra(EXTRA_PRIORITY, 1));
editTextPrice.setText(String.format(Locale.getDefault(), "%d", intent.getIntExtra(EXTRA_PRICE, 1)));
editTextQuantity.setText(String.format(Locale.getDefault(), "%d", intent.getIntExtra(EXTRA_QUANTITY, 1)));
} else {
setTitle("Add Note");
}
}
private void saveNote() {
String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();
int price = Integer.parseInt(editTextPrice.getText().toString());
int quantity = Integer.parseInt(editTextQuantity.getText().toString());
int priority = numberPickerPriority.getValue();
if (title.trim().isEmpty() || description.trim().isEmpty()) {
Toast.makeText(this, "details", Toast.LENGTH_SHORT).show();
return;
}
Intent data = new Intent();
data.putExtra(EXTRA_TITLE, title);
data.putExtra(EXTRA_PRICE, price);
data.putExtra(EXTRA_QUANTITY, quantity);
data.putExtra(EXTRA_DESCRIPTION, description);
data.putExtra(EXTRA_PRIORITY, priority);
int id = getIntent().getIntExtra(EXTRA_ID, -1);
if (id != -1) {
data.putExtra(EXTRA_ID, id);
}
setResult(RESULT_OK, data);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.add_note_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_note:
saveNote();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
以及activitymain.java中相应的相关部分:
adapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
@Override
public void onItemClick(Note note) {
Intent intent = new Intent(MainActivity.this, AddEditNoteActivity.class);
intent.putExtra(AddEditNoteActivity.EXTRA_ID, note.getId());
intent.putExtra(AddEditNoteActivity.EXTRA_TITLE, note.getTitle());
intent.putExtra(AddEditNoteActivity.EXTRA_PRICE, note.getPrice());
intent.putExtra(AddEditNoteActivity.EXTRA_QUANTITY, note.getQuantity());
intent.putExtra(AddEditNoteActivity.EXTRA_DESCRIPTION, note.getDescription());
intent.putExtra(AddEditNoteActivity.EXTRA_PRIORITY, note.getPriority());
startActivityForResult(intent, EDIT_NOTE_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_NOTE_REQUEST && resultCode == RESULT_OK) {
String title = data.getStringExtra(AddEditNoteActivity.EXTRA_TITLE);
String description = data.getStringExtra(AddEditNoteActivity.EXTRA_DESCRIPTION);
int price = data.getIntExtra(AddEditNoteActivity.EXTRA_PRICE, 0);
int quantity = data.getIntExtra(AddEditNoteActivity.EXTRA_QUANTITY, 0);
int priority = data.getIntExtra(AddEditNoteActivity.EXTRA_PRIORITY, 1);
Note note = new Note(title, description, priority, price, quantity);
noteViewModel.insert(note);
Toast.makeText(this, "Product saved", Toast.LENGTH_SHORT).show();
} else if (requestCode == EDIT_NOTE_REQUEST && resultCode == RESULT_OK) {
int id = data.getIntExtra(AddEditNoteActivity.EXTRA_ID, -1);
if (id == -1) {
Toast.makeText(this, "Product can't be updated", Toast.LENGTH_SHORT).show();
return;
}
String title = data.getStringExtra(AddEditNoteActivity.EXTRA_TITLE);
String description = data.getStringExtra(AddEditNoteActivity.EXTRA_DESCRIPTION);
int priority = data.getIntExtra(AddEditNoteActivity.EXTRA_PRIORITY, 1);
int price = data.getIntExtra(AddEditNoteActivity.EXTRA_PRICE, 0);
int quantity = data.getIntExtra(AddEditNoteActivity.EXTRA_QUANTITY, 0);
Note note = new Note(title, description, priority, price, quantity);
note.setId(id);
noteViewModel.update(note);
Toast.makeText(this, "Product updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Product not saved", Toast.LENGTH_SHORT).show();
}
}
activity\u add\u note.xml如下所示:
tools:context=".AddEditNoteActivity">
<TextView
android:id="@+id/edit_text_top"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="sans-serif-black"
android:hint="Add/Edit a new product:"
android:inputType="text"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="#243949" />
<EditText
android:id="@+id/edit_text_title"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="Item Name"
android:inputType="text" />
<EditText
android:id="@+id/edit_text_price"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="Price:"
android:inputType="numberSigned" />
<EditText
android:id="@+id/edit_text_quantity"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="Quantity"
android:inputType="numberSigned" />
<EditText
android:id="@+id/edit_text_description"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="State"
android:inputType="textMultiLine" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="58dp"
android:text="Priority:"
android:textAppearance="@android:style/TextAppearance.Medium" />
<NumberPicker
android:id="@+id/number_picker_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
暂无答案!
目前还没有任何答案,快来回答吧!