2010-12-18 11 views
3

Meine App enthält eine Listenansicht. Jeder Eintrag in der Listenansicht hat eine Schaltfläche zum Löschen. Die Schaltfläche "Löschen" konnte jedoch ein Element in der Listenansicht nicht löschen. Bitte sagen Sie mir, wie ich ein Element in einer Listenansicht und auch in database.my löschen kann.So löschen Sie ein Element aus der Listenansicht in Android

Meine Quelle:

public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sharefolioedit); 
     Log.e("string",s.toString()); 
     add = (ImageButton) findViewById(R.id.add); 
     add.setOnClickListener(this); 
     done = (Button) findViewById(R.id.done); 
     done.setOnClickListener(this); 

     getList(); 

     } 


    public void onCreate1(Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState); 
      delete= (ImageButton) findViewById(R.id.delete); 
      delete.setOnClickListener(this); 
    } 
    protected void onListItemClick(ListView l, ImageButton delete, int position,long id) { 
      super.onListItemClick(l, delete, position, id); 
      ID=id; 
       final Integer index =l.getSelectedItemPosition(); 
      Log.e("delete method","delete"); 

       Log.e("ID value",index.toString()); 
       new AlertDialog.Builder(ShareFolioEditActivity.this) 
       .setTitle("Delete") 
      .setMessage("Are you sure ??? ") 
        .setNeutralButton("no",null) 
        .setPositiveButton("yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
      confirmdelete(index);     
      } 
     })  .show(); 
      } 

      public void myClickHandler(View v) { 
      { 
//    int position = 0; 
//    onListItemClick(lv, delete, position, ID); 
//    ID=id; 
       Log.e("delete method","delete"); 

       final Integer a = new Integer(v.getId()); 
       Log.e("ID value",a.toString()); 
       new AlertDialog.Builder(ShareFolioEditActivity.this) 
       .setTitle("Delete") 
      .setMessage("Are you sure ??? ") 
        .setNeutralButton("no",null) 
        .setPositiveButton("yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
      confirmdelete(a);     
      } 
     })  .show(); 
      } 
     } 

    public void onClick(View v) { 
     if(v.equals(add)) 
     { 
      Intent in=new Intent(ShareFolioEditActivity.this,AddCategory.class); 
      startActivity(in); 
     } 
     if(v.equals(done)) 
     { 
      Intent i=new Intent(ShareFolioEditActivity.this,ShareFolioActivity.class); 
      startActivity(i); 
     } 
    } 

    public void confirmdelete(int index) 
    { 
     db.delete("sharelist", "_id="+index, null); 
     Toast.makeText(this, "row deleted"+index, Toast.LENGTH_SHORT).show(); 
     Bundle b=null; 
     onCreate(b); 
    } 

    public void getList() 
    { int x = 0; 
     String where=null; 
     csh=new createSqliteHelper(this); 
     try{ 

      //db.open(); 
     db=csh.getReadableDatabase(); 
     }catch(Exception e){ 
      System.out.println(e); 
     } 
     if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { 
      where=getIntent().getStringExtra(SearchManager.QUERY); 
      x=1; 
     } 
     if(x==1) 
     { 
     cur=db.rawQuery("SELECT * FROM sharelist where category LIKE '%"+where+"%'",null); 
     }else{ 
     cur=db.rawQuery("SELECT * FROM sharelist ORDER BY category",null);} 
     Log.e("cursor",cur.toString()); 
    SimpleCursorAdapter list=new SimpleCursorAdapter(this,R.layout.edititems,cur,s,i); 
    getListView(); 
    setListAdapter(list); 

    } 
} 

Das sharefolioedit xml Layout ist

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text" 
    android:text="CATEGORIES" 
    android:layout_centerHorizontal="true" 
    /> 

    <Button 
     android:id="@+id/done" 
     android:text="Done" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_width="90px" 
     android:layout_height="20px" 
     android:layout_above="@id/android:list" /> 
     <ImageButton 
     android:id="@+id/add" 
     android:text="Add" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_width="80px" 
     android:layout_height="20px" 
     android:layout_above="@id/android:list" 
     android:src="@drawable/addicon"/> 

    <ListView android:layout_width="500px" 
     android:layout_height="700px" 
     android:id="@id/android:list" 
     android:layout_alignParentBottom="true" 
     /> 
</RelativeLayout> 

Die edititems.xml ist

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <ImageButton 
    android:id="@+id/delete" 
    android:layout_width="70px" 
    android:paddingLeft="2px" 
    android:paddingRight="2px" 
    android:paddingTop="2px" 
    android:text="Delete" 
    android:layout_height="wrap_content" 
    android:onClick="myClickHandler" 
    android:src="@drawable/removeicon" 
    /> 

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="10sp"> 

    <TextView 
    android:id="@+id/category" 
    android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    android:text="" 
    android:layout_toRightOf="@+id/catagory" 
    /> 
    </RelativeLayout> 
</LinearLayout> 

In edititems.xml i auch eine Löschtaste definiert, so wenden Sie sich bitte überprüfe und sag mir Lösung.

Antwort

4

In Ihrem confirmdelete Verfahren, nach der db.delete Verwendung ein AsyncTask die Datenbank abzufragen und eine neues Cursor im doInBackground Verfahren erhalten. Dann in der onPostExecute Methode Anruf changeCursor auf Ihrem SimpleCursorAdapter, die Sie list genannt haben, die ein Klassenlevelfeld anstelle eines lokalen Feldes sein sollte, also können alle Ihre Methoden auf ihn zurückgreifen. Dies teilt dem ListView mit, dass sich die Daten im Adapter geändert haben und er sich selbst aktualisieren muss. Sie müssen onCreate in Ihrer confirmdelete Methode nicht unten aufrufen. Hier

ist ein Code:

private class UpdateCursor extends AsyncTask<Void, Void, Cursor> { 
    protected Cursor doInBackground(URL... urls) { 
     Cursor result = db.query(...); 
     return result; 
    } 

    protected void onPostExecute(Cursor result) { 
     list.changeCursor(result); 
    } 
} 

Good Luck.

+0

In meinem Fall erstelle ich Adapter wieder und dann habe ich es auf ListView gesetzt. –

Verwandte Themen