如何完全删除listView上的行

时间:2014-02-14 13:11:52

标签: android sqlite listview android-listview

我为列表视图制作了一个上下文菜单,我遇到了问题

所以当我使用删除时,它不会完全删除

首先列表视图已删除,但是当我转到其他活动并返回listView

我仍然在listview上删除的项目

所以这是我的代码

MainActivity.java

public class MainActivity extends ListActivity {
private ReminderDB dba;
String title, subject, content;
NoteAdapter noteAdapter;
ArrayList<NoteEntry> notes;
NoteEntry conNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
    dba = new ReminderDB(this);
    dba.open();
    setContentView(R.layout.reminder_main);
    super.onCreate(savedInstanceState);
    noteAdapter = new NoteAdapter(this);
    this.setListAdapter(noteAdapter);
    registerForContextMenu(this.getListView());
}


private class NoteAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    public NoteAdapter(Context context){
        mInflater = LayoutInflater.from(context);
        notes = new ArrayList<NoteEntry>();
        getData();
    }
    public void getData(){
        Cursor c = dba.getNote();
        startManagingCursor(c);

        if(c.moveToFirst()){
            do {
                String title = c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String subject = c.getString(c.getColumnIndex(Constants.SUBJECT_NAME));
                String content = c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
                NoteEntry temp = new NoteEntry(title,content,subject);
                notes.add(temp);
            }while(c.moveToNext());
        }

    }
    private Holder holder;
    @Override
    public int getCount() {
        return notes.size();
    }

    @Override
    public NoteEntry getItem(int i) {
        return notes.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if(view == null||view.getTag() == null){
            view = mInflater.from(getApplicationContext()).inflate(R.layout.note_row,null);
            holder = new Holder();
            holder.subject = (TextView) view.findViewById(R.id.Note_content);
            holder.title = (TextView) view.findViewById(R.id.subject_name);
            view.setTag(holder);
        }else{
            holder = (Holder) view.getTag();

        }
        holder.mNote = getItem(position);
        holder.title.setText(holder.mNote.getTitle());
        holder.subject.setText(holder.mNote.getSubject());

        view.setTag(holder);
        return view;

    }


    private class Holder {
        NoteEntry mNote;
        public TextView subject;
        public TextView title;
     }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.create_button:
            newActivities();
            return true;
        case R.id.action_settings:
            openSetting();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
private void newActivities() {
    Intent newActivities = new Intent(getApplicationContext(),NewActivities.class);
    startActivity(newActivities);

}

private void openSetting() {
    // TODO Auto-generated method stub

}
@Override
public void onBackPressed(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    NoteEntry selectNote = (NoteEntry)l.getItemAtPosition(position);
     title = selectNote.getTitle();
     subject = selectNote.getSubject();
     content = selectNote.getContent();

    Intent intent = new Intent(getApplicationContext(),ShowNote.class);
    intent.putExtra("subject",subject);
    intent.putExtra("title",title);
    intent.putExtra("content",content);
    startActivity(intent);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu,menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    switch (item.getItemId()){
        case R.id.view:
            viewNote(info.position);
            return true;
        case R.id.edit:
            editNote(info.position);
            return true;
        case R.id.delete:
            deleteNote(info.position);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

private void viewNote(int position) {
    NoteEntry selectNote = (NoteEntry) this.getListView().getItemAtPosition(position);
    title = selectNote.getTitle();
    subject = selectNote.getSubject();
    content = selectNote.getContent();

    Intent intent = new Intent(getApplicationContext(),ShowNote.class);
    intent.putExtra("subject",subject);
    intent.putExtra("title",title);
    intent.putExtra("content",content);
    startActivity(intent);
}

private void deleteNote(int position) {
    dba.open();
    dba.deleteNote(position);
    dba.close();
    this.notes.remove(position);
    noteAdapter.notifyDataSetChanged();
}

 private void editNote(int position) {
   }
 }

NewActivities.java

public class NewActivities extends Activity {
EditText subET, titleET, contentET;
TextView tutorial;
Button submit;
ReminderDB dba;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_activities);
    dba = new ReminderDB(this);
    dba.open();
    tutorial = (TextView) findViewById(R.id.tutorial);
    subET = (EditText)findViewById(R.id.edit_subject);
    titleET = (EditText)findViewById(R.id.edit_note);
    contentET = (EditText)findViewById(R.id.edit_content);
    submit = (Button) findViewById(R.id.submit_button);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                try{
                    saveToDB();
                }catch(Exception e){
                    e.printStackTrace();
                }

        }
    });


}
public void saveToDB(){
    dba.insertNotes(titleET.getText().toString(), contentET.getText().toString()
            , subET.getText().toString());
    dba.close();
    titleET.setText("");
    contentET.setText("");
    subET.setText("");
    Intent i = new Intent(NewActivities.this,MainActivity.class);
    startActivity(i);
    Toast.makeText(getApplicationContext(), "Create Successfull",
            Toast.LENGTH_LONG).show();
  }
 }

ReminderDB.java

public class ReminderDB {
private SQLiteDatabase db;
private final Context context;
private final NoteDBHelper dbhelper;
private String[] allcolumns = {Constants.SUBJECT_NAME,Constants.TITLE_NAME,Constants.CONTENT_NAME};

public ReminderDB(Context c){
    context = c;
    dbhelper = new NoteDBHelper(context);
    db = dbhelper.getWritableDatabase();
}
public void close(){
    db.close();
}
public void open() throws SQLiteException {
        db = dbhelper.getWritableDatabase();
}
public long insertNotes(String title,String content,String S_name){
        ContentValues newTaskValue = new ContentValues();
        newTaskValue.put(Constants.TITLE_NAME, title);
        newTaskValue.put(Constants.CONTENT_NAME, content);
        newTaskValue.put(Constants.SUBJECT_NAME, S_name);

        return db.insert(Constants.TABLE_NAME, null,newTaskValue );
}
public Cursor getNote(){

    Cursor cursor =  db.query(Constants.TABLE_NAME,allcolumns,null,null,null,null,null);

    return cursor;
}
public void deleteNote(int position){
    db.delete(Constants.TABLE_NAME,Constants.NOTE_ID+" = "+position,null);
  }
}

thx求助:))

2 个答案:

答案 0 :(得分:0)

试试这段代码,

 btn.setOnClickListener(new OnClickListener() 
 {
    @Override
    public void onClick(View v) 
    {
        // delete query fire here whatever related to requirements.
        mList.remove(position);//you can delete your item here
        notifyDataSetChanged();
    }
});

此方法用于 notifyDataSetChanged()刷新来自BaseAdapter的getView()方法。

答案 1 :(得分:0)

试试这个。

您无法保证列表中的位置与您的ID相同。你的id很可能是一个自动增量索引。 (我在这里看不到db create语句..)这意味着当你删除一行时你的应用程序只会删除一行错误的行..

将此函数传递给来自数据库的记录的ID。我使用视图标签来存储这些id。

public void deleteNote(int id) {
  String[] whereArgs = new String[1];
  whereArgs[0] = String.valueOf(id);

  int deletedRowCount = db.delete(Constants.TABLE_NAME, Constants.NOTE_ID + " == ?", whereArgs);

  if (deletedRowCount > 0) {
    Log.d("DB", deletedRowCount + " record(s) deleted.");
  }
}