public void delete(View v) { int id = getIntent().getIntExtra(NoteDb.ID,0); mSql.delete(NoteDb.TABLE_NAME," _id = " + id,null); finish(); }
时间: 2024-02-26 10:52:27 浏览: 13
这是 Show 类中的 delete() 方法。首先通过 getIntent() 方法获取从上一个 Activity 传来的笔记的 id 值并将其赋值给变量 id。然后调用 mSql 的 delete() 方法从 NoteDb.TABLE_NAME 表中删除 _id 等于 id 的记录。最后调用 finish() 方法关闭当前 Activity。这段代码实现了删除笔记的功能。
相关问题
public void delete(View v) { int id = getIntent().getIntExtra(NoteDb.ID,0); mSql.delete(NoteDb.TABLE_NAME," _id = " + id,null); finish(); }怎么添加一个修改的方法
要添加一个修改笔记的方法,可以在 Show 类中添加一个新的方法,例如 update() 方法。具体实现可以参考以下代码:
```
public void update(View v) {
int id = getIntent().getIntExtra(NoteDb.ID, 0);
String content = mTextview.getText().toString(); // 获取修改后的笔记内容
String time = getTime(); // 获取修改后的笔记时间,可以根据实际需要进行实现
ContentValues values = new ContentValues();
values.put(NoteDb.CONTENT, content);
values.put(NoteDb.TIME, time);
mSql.update(NoteDb.TABLE_NAME, values, "_id = " + id, null); // 更新数据库中的笔记记录
finish();
}
private String getTime() {
// 实现获取当前时间的方法,具体实现可以根据实际需要进行实现
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
```
在 update() 方法中,首先获取笔记的 id 值和修改后的笔记内容,然后调用 getTime() 方法获取修改后的笔记时间。接着创建一个 ContentValues 对象,将修改后的笔记内容和时间设置到其中。最后调用 mSql 的 update() 方法更新数据库中的笔记记录,并关闭当前 Activity。
需要注意的是,getTime() 方法中的 SimpleDateFormat 对象用于格式化时间,具体格式可以根据实际需求进行修改。另外,update() 方法中的实现方式仅供参考,具体实现可以根据实际需求进行修改。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit_layout); myToolbar = findViewById(R.id.my_Toolbar); setSupportActionBar(myToolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); //设置toolbar取代actionbar myToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoSetMessage(); setResult(RESULT_OK, intent); finish(); } }); et = findViewById(R.id.et); Intent getIntent = getIntent(); openMode = getIntent.getIntExtra("mode", 0); if (openMode == 3) {//打开已存在的note id = getIntent.getLongExtra("id", 0); old_content = getIntent.getStringExtra("content"); old_time = getIntent.getStringExtra("time"); old_Tag = getIntent.getIntExtra("tag", 1); et.setText(old_content); et.setSelection(old_content.length()); } }这段代码怎么改成fragment的
要将这段代码改成Fragment,需要将其中的Activity相关的代码改成Fragment相关的代码。具体实现如下:
1. 首先,将类定义修改为继承Fragment类,如下:
```
public class EditFragment extends Fragment {
```
2. 将onCreate方法中的super.onCreate(savedInstanceState)删除,因为Fragment没有这个方法,如下:
```
@Override
public void onCreate(Bundle savedInstanceState) {
// 删除super.onCreate(savedInstanceState);
// ...
}
```
3. 将setContentView方法删除,因为Fragment没有布局文件,布局文件应该在Activity中实现,如下:
```
// 删除setContentView(R.layout.edit_layout);
```
4. 将findViewById方法中的参数修改为Fragment的布局文件中的控件ID,如下:
```
myToolbar = view.findViewById(R.id.my_Toolbar);
et = view.findViewById(R.id.et);
```
5. 将getIntent方法修改为getArguments方法,因为Fragment没有getIntent方法,如下:
```
Bundle args = getArguments();
openMode = args.getInt("mode", 0);
if (openMode == 3) {//打开已存在的note
id = args.getLong("id", 0);
old_content = args.getString("content");
old_time = args.getString("time");
old_Tag = args.getInt("tag", 1);
et.setText(old_content);
et.setSelection(old_content.length());
}
```
6. 将setSupportActionBar方法和getSupportActionBar方法删除,因为Fragment没有这些方法,如下:
```
// 删除setSupportActionBar(myToolbar);
// 删除getSupportActionBar().setHomeButtonEnabled(true);
// 删除getSupportActionBar().setDisplayHomeAsUpEnabled(true);
```
7. 将setNavigationOnClickListener方法修改为setOnClickListener方法,因为Fragment没有setNavigationOnClickListener方法,如下:
```
myToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
autoSetMessage();
setResult(RESULT_OK, intent);
getActivity().finish();
}
});
```
注意:在Fragment中需要使用getActivity()方法来获取所在的Activity。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)