从列表中删除数据后如何更新列表视图?

时间:2017-04-18 07:42:33

标签: java android sqlite

我是android的新手。在我的应用程序中有一个listView,其中有一个问题列表,当我从it.please帮助中删除任何问题时,我想动态更新该列表。

public class admin extends Activity {
static int itemPosition;
ListView lv;
List<Question> quesList2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.admin);
    lv=(ListView) findViewById(R.id.listView);
    QuizHelper db = new QuizHelper(this);  // my question bank class
    quesList2 = db.getAllQuestions2();
    int a= quesList2.size();
    String[] testArray = new String[a];
    for (int i = 0; i < testArray.length; i++) {
        testArray[i]=quesList2.get(i).getQUESTION();
    }
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1,     
    android.R.id.text1,testArray);

    // Assign adapter to ListView
    lv.setAdapter(adapter);
    registerForContextMenu(lv);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {
            // TODO Auto-generated method stub
            itemPosition     = position;
        }
    });
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,  
ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, v.getId(), 0, "Delete");//groupId, itemId, order, title
}
@Override
public boolean onContextItemSelected(MenuItem item){
    QuizHelper db = new QuizHelper(this);
    if(item.getTitle()=="Delete"){
     db.deleteDB();
        Toast.makeText(this, "Successfully deleted", 
    Toast.LENGTH_LONG).show();
        lv.invalidateViews();
    }
    else{
        return false;
    }
    return true;
 }

 }

这是deletehelper.java类中的delete函数。

protected void deleteDB(){
    dbase = this.getWritableDatabase();
    dbase.execSQL("DELETE FROM " + TABLE_QUEST + " WHERE " + KEY_ID + " =  
  '"+admin.itemPosition+"'");
 }

4 个答案:

答案 0 :(得分:3)

您应该使用 notifyDataSetChanged()

  

通知附加的观察者基础数据已经存在   已更改,任何反映数据集的视图都应自行刷新。

代码

if(item.getTitle()=="Delete"){
 db.deleteDB();
 adapter.notifyDataSetChanged();
}

答案 1 :(得分:1)

您可以使用adapter.notifyDataSetChanged,但这并不能确保您的数据真正被删除!有数据库操作时可能会出错!

更简洁的方法是设置自定义侦听器并从数据库类通知视图这是一个成功的操作。

创建一个界面,

public interface DatabaseListener {
    void deleteSuccess();
    void deleteError();
}

指定要通知的侦听器,

public class admin extends Activity implements DatabaseListener{
...your codes
//you need to implement the interface methods

@Override
public void deleteSuccess(){
  adapter.notifyDataSetChanged();
  Toast.makeText(this, "Successfully deleted",Toast.LENGTH_LONG).show();
}

@Override
public void deleteError(){
  Toast.makeText(this, "Failed to delete",Toast.LENGTH_LONG).show();
}


}

通知听众,

protected void deleteDB(){
    dbase = this.getWritableDatabase();
    try {
        dbase.execSQL("DELETE FROM " + TABLE_QUEST + " WHERE " + KEY_ID + " ='"+admin.itemPosition+"'");
        //verify if the data is deleted
        //get the databaseListener object from your Database class constructor.

        databaseListener.deleteSuccess();
    }catch(Exception e){
        databaseListener.deleteError();
    }
}

答案 2 :(得分:0)

无论你在哪里打电话

 deleteDB() 

方法,调用

adapter.notifyDataSetChanged() 

之后

答案 3 :(得分:0)

请考虑使用recyclerview而不是列表视图。

如果您不知道如何使用它,这是一个很棒的指南:https://guides.codepath.com/android/using-the-recyclerview