[英]I am new to android development and I have a problem in 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);
您正在創建新的 Object 按鈕,每次單擊按鈕並嘗試更新數據庫中的舊版本。
嘗試這個
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.