2011-05-02 216 views
2

我想從ListView中刪除所選項目,但我不能。我嘗試了很多方法,但他們不適合我。從ListView中刪除所選項目

這是我的適配器列表 - 項目來自SD卡。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
         FindFilesByType.this, android.R.layout.test_list_item, 
         Ringtones); 

我該如何解決這個問題?

回答

3

您可以從底層列表中刪除該項目,並通知適配器數據集已更改。

查看this method的例子。

1
@Override 
      public View getView(final int position, View convertView, ViewGroup parent) { 
       // TODO Auto-generated method stub 
       LayoutInflater inflater=cntx.getLayoutInflater(); 
       View row=inflater.inflate(R.layout.bookmark_row,null); 

       TextView tv=(TextView)row.findViewById(R.id.txtToc); 
       final TextView tv2=(TextView)row.findViewById(R.id.txtPgNo); 
       mCursor.moveToPosition(position); 
       System.out.println("Count Cursor"+mCursor.getCount()); 
       if(mCursor.getCount()<=0) 
       { 
        tv.setText("No Bookmark Found"); 
       } 
       else 
       { 
       tv.setText(mCursor.getString(1).toString()); 
       tv2.setTag(mCursor.getString(0).toString()); 
       tv2.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         System.out.println(position); 
         db_conn.onDelete("tab_book", mCursor.getColumnName(0),Integer.parseInt((String) tv2.getTag())); 
//      Toast.makeText(ctx,"Bookmark Deleted", Toast.LENGTH_SHORT).show(); 
         mToastTextView=new toastTextview(0, 0, ctx,listAct); 
         mToastTextView.showMessage("Bookmark Deleted"); 
         refresh(); 
        } 
       }); 
       row.setTag(mCursor.getString(2).toString()); 
       } 
       return row; 
      } 


     public void refresh() 
     { 
      mCursor=db_conn.onQueryGetCursor("tab_book",mItems,null, null, null, null, null); 
      mCursor.moveToPosition(0); 
      notifyDataSetChanged(); 
      if(mCursor.getCount()<=0) 
      { 
//    Toast.makeText(ctx, "No Bookmark", Toast.LENGTH_SHORT).show(); 
       try { 
        mFlingAct.dialogBookmark.dismiss(); 
       } catch (Exception e) { 
        // TODO: handle exception3 
        e.printStackTrace(); 
       } 

      } 

     } 
2

貫徹onitemclickListener並獲得項目(ID獲得)在一個ArrayAdapter點擊,然後調用 arrayList.remove([ID]); adapter.notifyDataSetChanged();