我是Android新手,我从列表视图中删除项目后刷新了我的列表视图。我已经尝试了所有的东西而且无法让它工作,所以我正在寻求帮助。我知道我必须在我的适配器上使用notifyDataSetChanged,但我不知道在哪里放它。先谢谢你们。 整个项目上传到zippyshare:http://www6.zippyshare.com/v/TZVGTFJp/file.html
public class MainActivity extends AppCompatActivity {
DBHandler handler;
Context context = this;
ListView listView;
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
EditText etName = (EditText) findViewById(R.id.etNameID);
EditText etPhone = (EditText) findViewById(R.id.etPhoneID);
handler = new DBHandler(this);
printDatabase();
if (listView != null) {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
PopupMenu popup = new PopupMenu(context, view);
popup.getMenuInflater().inflate(R.menu.menu_delete_popup, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.deleteItemID:
handler.deleteContact(id);
printDatabase();
break;
}
return true;
}
});
return true;
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.addContactID) {
openAddDialog();
}
return super.onOptionsItemSelected(item);
}
public void printDatabase() {
ArrayList array_list = handler.getAllCotacts();
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
}
public void openAddDialog() {
android.app.AlertDialog.Builder makeDialog = new android.app.AlertDialog.Builder(this);
LayoutInflater li = getLayoutInflater();
View v = li.inflate(R.layout.contacts_add_dialog, null);
makeDialog.setView(v);
//tu idu varijable
final EditText etName = (EditText) v.findViewById(R.id.etNameID);
final EditText etPhone = (EditText) v.findViewById(R.id.etPhoneID);
makeDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String textFromEtName = etName.getText().toString();
String textFromEtPhone = etPhone.getText().toString();
handler.insertContact(textFromEtName, textFromEtPhone);
printDatabase();
}
});
makeDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
android.app.AlertDialog ad = makeDialog.create();
ad.show();
}
}
这是DBHandler代码:
public class DBHandler extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_PHONE = "phone";
public DBHandler(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"CREATE TABLE contacts " +
"(id INTEGER PRIMARY KEY, name TEXT,phone TEXT,email TEXT, street TEXT,place TEXT)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String name, String phone)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String phone)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public void deleteContact(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(CONTACTS_TABLE_NAME, CONTACTS_COLUMN_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
}
public ArrayList<String> getAllCotacts()
{
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
}
答案 0 :(得分:0)
更新您的 printDatabase 功能,如下所示:
BEGIN
答案 1 :(得分:0)
删除一个元素后,从函数中调用另一个元素,从数据库中检索结果
答案 2 :(得分:0)
您在此处使用索引编号行
添加项目array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
在这里,您尝试使用列表视图
中视图对象的索引编号删除行handler.deleteContact(ID);
因此,您必须使用 CONTACTS_COLUMN_ID
而不是使用CONTACTS_COLUMN_NAME。array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_ID)));
即使您这样做也可能无效,因为您无法保证Listview中的视图编号与表中的id匹配。 id号可能是99,视图号始终以0开头。
这是一个原始的解决方案 第1步
proc/self/maps
第2步 让它全球化
HashMap array_list
第3步
handler.deleteContact(array_list.get(ID)获得(&#34;指数&#34));
最重要的是,这是一个原始的解决方案,我认为编写自己的适配器是最好的解决方案。