java - 我是 android 开发新手,我在 android studio 中遇到问题
问题描述
public class Memo extends AppCompatActivity {
DBHandler dbh;
Notes items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memo);
getSupportActionBar().hide();
try{
dbh = new DBHandler(this);
}
catch (Exception ex){
ex.printStackTrace();
}
display();
}
public void display( ){ //method to display all items in the database
List<Notes> books_list = dbh.getNotes(); ////here i get the list fromm the database
///// i used a custom adapter because i needed it
final myAdapter adapter = new myAdapter(this, books_list); ///creating adapter from
myadapter to link it with the list
ListView _note_ = findViewById(R.id.list_txt);
_note_.setAdapter(adapter);
_note_.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ////
when clicking on item the hidden fields will be visible
items = (Notes) adapter.getItem(position);
String Note_content = items.getNote();
String title = items.getTitle();
String Date = items.getDate();
Intent i = new Intent(getApplicationContext() ,Note_content.class);
i.putExtra("Note ", Note_content);
i.putExtra("title", title);
i.putExtra("Date", Date);
startActivity(i);
}
});
_note_.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
items = (Notes) adapter.getItem(position);
AlertDialog.Builder builder = new AlertDialog.Builder(Memo.this);
final AlertDialog.Builder alert = new AlertDialog.Builder(Memo.this);
View m1View = getLayoutInflater().inflate(R.layout.update_custom, null);
final EditText txt_Date1 = (EditText) m1View.findViewById(R.id.txt_custom1_Date);
final EditText txt_title1 = (EditText) m1View.findViewById(R.id.txt_custom1_title);
final EditText txt_note1 = (EditText) m1View.findViewById( R.id.txt_note1);
final Button btn_cancel = (Button) m1View.findViewById(R.id.btn_cancel1);
final Button btn_okay = (Button) m1View.findViewById(R.id.btn_ok1);
alert.setView(m1View);
final AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(false);
builder.setNegativeButton("delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dbh.deleteNote(items);
display();
Toast.makeText(Memo.this, "item has been deleted", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btn_okay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String ttitle = txt_title1.getText().toString();
final String nnote = txt_note1.getText().toString();
final String dDate = txt_Date1.getText().toString();
Notes Ttnote = new Notes(ttitle, nnote, dDate);
dbh.updatenotes(Ttnote);
display();
//Toast.makeText(Memo.this, "item has been updated" + items.getId() +
" " + ttitle+ " " + nnote, Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
});
builder.setTitle("Choose Option");
builder.show();
return true;
}
});
}
我想更新注释,当我使用 Toast 显示 ttilte 、nnote 和 Ddate 时,它们是正确的,但它们没有在列表中更新,这是我在 DBHandler 中用来更新的方法,我在代码中找不到任何错误和当我运行它时它没有任何错误但我无法在列表中获得更新的注释
public void updatenotes (Notes note){ //method to update
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(); /// getting the values
values.put(" title" , note.getTitle());
values.put("note" ,note.getNote());
values.put("date", note.getDate());
db.update(TABLE_NAME_notes, values ,"id=?" ,new String[]{String.valueOf(note.getId())} );
///perform the updating query
db.close();
}
解决方案
Notes Ttnote = new Notes(ttitle, nnote, dDate);
dbh.updatenotes(Ttnote);
每次单击按钮并尝试更新数据库中的旧对象时,您都在创建新的注意对象。
尝试这个
final String ttitle = txt_title1.getText().toString();
final String nnote = txt_note1.getText().toString();
final String dDate = txt_Date1.getText().toString();
items.setTitle(ttitle);
items.setnote(nnote);
items.setDate(dDate);
在数据库中更新
再次获取列表并调用 adapter.notifyDataSetChanged()
推荐阅读
- javascript - 自动完成不更新选项,但新数组在 hits 属性中可用
- sql - 通过文本数组中的任何值在文本数组中搜索 Postgres sql
- refactoring - 为什么我不能将部分框定义移动到本地绑定中?
- android - 图像和视频的 getItemViewType
- oracle - 我需要为备用或仅安装 oracle 软件安装 oracle 数据库吗?
- python-3.x - 在服务器上的会话结束后查看 Flask App 的终端日志,但应用程序仍在后台运行
- node.js - 如何使用节点读取在 Firestore 中创建的最新文档?
- javascript - TypeScript:如何将类型显式传递给该函数的泛型
- c++ - 迭代 std::pair 的向量时使用结构绑定
* - haskell - 试图理解为什么在 Haskell 中使用 foldr 的这个函数不起作用