2012-06-15 3 views
0

바코드를 스캔하고 db에 저장 한 다음 목록을 통해 관리하는 앱을 만들고 있습니다. 그래서, 이러한 클래스입니다 그목록에서 항목 삭제

홈페이지 내 문제

@Override 
    public void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scanner_menu); 

     Button addButton = (Button) findViewById (R.id.addMenuButton); 
     addButton.setOnClickListener (new OnClickListener(){ 
      public void onClick (View v){ 
       startActivity(new Intent(CodiceBarreActivity.this, AggiungiCodiceActivity.class)); 
      } 
      }); 

     Button listButton = (Button) findViewById (R.id.ViewlistButton); 
     listButton.setOnClickListener (new OnClickListener(){ 
      public void onClick (View v){ 
       startActivity(new Intent(CodiceBarreActivity.this, ProdottiSelectionActivity.class)); 
      } 
      }); 

    } 

static final class ProductData { 
    long _id; 
    String barcode; 
    String format; 
    String title; 
    String price; 
} 

}

도우미

private SQLiteDatabase db; 

public ProductDatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    StringBuilder sql = new StringBuilder(); 

    sql.append("create table ") 
     .append(PRODUCT_TABLE) 
     .append("( ") 
     .append(" _id integer primary key,") 
     .append(" barcode text,") 
     .append(" format text,") 
     .append(" title text,") 
     .append(" price text") 
     .append(") "); 

    db.execSQL(sql.toString());  

    Log.d(TAG, PRODUCT_TABLE + "table created"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("drop table if exists " + PRODUCT_TABLE);  
     Log.d(TAG, PRODUCT_TABLE + "table dropped"); 
     onCreate(db); 
} 

데시벨

private static final String PRODUCT_TABLE = "products"; 
    private static final BigDecimal ONE_HUNDRED = new BigDecimal("100"); 
    private SQLiteDatabase db; 

    public CodiciDatabase(Context context) { 
     ProductDatabaseHelper helper = new ProductDatabaseHelper(context); 
     db = helper.getWritableDatabase(); 
    } 

    public boolean insert(ProductData product) { 
     ContentValues vals = new ContentValues(); 
     vals.put("_id", product._id); 
     vals.put("barcode", product.barcode); 
     vals.put("format", product.format); 
     vals.put("title", product.title); 
     vals.put("price", product.price); 

     return db.insert(PRODUCT_TABLE, null, vals) != -1; 
    } 

관심에 관심 에드 클래스

private ProductDatabaseHelper dbHelper; 
private static final String PRODUCT_TABLE = "products"; 
ProductData product = new ProductData(); 

@Override 
protected void onCreate (Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dbHelper = new ProductDatabaseHelper(this); 
    } 

@Override 
protected void onResume(){ 
    super.onResume(); 
    SQLiteDatabase db = dbHelper.getReadableDatabase(); 

     Cursor cursor = db.rawQuery("select * from " + PRODUCT_TABLE, null); 

     setListAdapter(new CursorAdapter(this, cursor, true) { 
     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent){ 
      TextView textView = new TextView(ProdottiSelectionActivity.this); 
      return textView; 
     } 

     @Override 
     public void bindView (View view, Context context, Cursor cursor){ 

      TextView textView = (TextView) view; 
      textView.append(cursor.getString(1)); 
      textView.append("\n"); 
      textView.append(cursor.getString(2)); 
      textView.append("\n"); 
      textView.append(cursor.getString(3)); 
      textView.append("\n"); 
      textView.append(cursor.getString(4)); 
      registerForContextMenu(textView); 
      } 

     }); 
    } 

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

} 

}

난 당신이 내가 당신이 날 도울 수 있기를 바랍니다 올바른 구문을 찾고 미쳐가는 선택한 item.I'm을 삭제하도록 onListItemClick 메소드를 구현해야

! 이 같은

+1

Stackoverflow는 코드 생성 사이트가 아닙니다. – JoxTraex

+0

아니지만 그것은 사이트 전문가이며, 나는 그것을 필요로했다. 나는 조용히 귀찮게하지 않을 것이다. –

+0

코드 벽 == 불량. 내 눈이 아파. Boo hoo. –

답변

1

"관심있는 수업"에서 오류가 발생하는 것처럼 보입니다. 귀하의 코드는 어떤 곳에서는 약간 혼란 스럽 습니다만, 귀하가 목록보기를 사용하는지 확실하지 않습니다. 그러나 목록보기를 사용하여 onItemClickListener에 바인딩하는 것이 유용 할 것입니다. 아이템을 표시 할 레이아웃에 extends ListActivity을 사용합니다. 목록보기 레이아웃에 listview 요소를 넣어야합니다. 그런 다음 목록보기를 채우기 위해, 당신은 할 수 있습니다 :

 String[] tableColumnNames = new String[] { 
       "TABLE COLUMN NAMES SEPARATED BY COMMAS" }; 
     int[] textViewLabelNames = new int[] { "NAMES OF TEXT VIEW LABELS IN XML ROW LAYOUT" }; 

     SimpleCursorAdapter stuff = new SimpleCursorAdapter(this, 
       R.layout.your_created_xml_row_layout, cursor, tableColumnNames, 
       textViewLabelNames); 
     this.setListAdapter(stuff); 

을 다음 바인딩 당신의 OnClickListener를을 :

listView.setOnItemClickListener(new OnItemClickListener() { 

     //ListView item is clicked 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


    } 
    }); 

다시 코드 참조 :

때 당신은 데이터베이스에서 삭제해야 onListItemClick is triggered onListItemClick 메서드에 전달 된 id에 해당하는 행입니다. cursor.requery()을 사용하여 뷰의 변경 사항을 반영하도록 커서를 업데이트해야합니다.

그래서 당신이 할 수있는 특정 ID로 데이터베이스에서 항목을 삭제하려면 :

ProductDatabaseHelper helper = new ProductDatabaseHelper(context); 
SQLiteDatabase database = helper.getWritableDatabase(); 
database.delete("TABLE NAME HERE", "NAME OF ID COLUMN HERE" + "=" + id, null); 
database.close(); 

또한 메소드 서명의 도움에 대한 설명은 SQLiteDatabase Docs을 볼 수 있습니다.

1

뭔가 작업을해야합니다 :

db.delete("products", "barcode" + "="+ barcodeToDelete, null); 

은 테이블에서 삭제할 바코드의 값으로 barcodeToDelte을 교체합니다. 바코드의 값을 모를 경우 위의 방법에서 "바코드"로 바꿈으로써 데이터베이스의 다른 필드를 기반으로 삭제할 수 있습니다.