2014-12-08 125 views
2

我有一個自定義列表視圖中刪除按鈕在每一行。列表視圖中的項目從數據庫中顯示。單擊刪除按鈕,數據庫中的項目被刪除。但它不是反映在列表視圖中。我需要刪除該項目的相應行。從列表視圖中刪除項目,按鈕點擊

CartAdapter.java

public class CartAdapter extends BaseAdapter 
{ 

private final String[] pname; 
private final String[] price; 
private Context cntxt; 
public CartAdapter(Context c,String [] pname,String [] price) 
{ 

    cntxt=c; 
    this.pname=pname; 
    this.price=price; 
} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return pname.length; 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View List; 
    LayoutInflater mLayoutInflater=(LayoutInflater)cntxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (convertView==null) { 
List=new View(cntxt); 
List=mLayoutInflater.inflate(R.layout.add2crt_sub,parent, false); 

} 
else { 
List=(View)convertView; 
} 

Button button=(Button)List.findViewById(R.id.remove); 
button.setTag(position); 
button.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 


     int position = (Integer)v.getTag(); 
     SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null); 
     String pnam = pname[position]; 

     mydb.execSQL("DELETE FROM add2cart WHERE pnme ='"+pnam+"'"); 

     Cursor cr = mydb.rawQuery("SELECT * FROM add2cart", null); 
     String [] pname = new String[cr.getCount()]; 
     String [] price = new String[cr.getCount()]; 

     int i = 0; 
     while(cr.moveToNext()) 
     { 
      String name = cr.getString(cr.getColumnIndex("pnme")); 
      String prprice = cr.getString(cr.getColumnIndex("prate")); 
      pname[i] = name; 
      price[i] = prprice; 
      i++; 
     } 

     CartAdapter cart=CartAdapter.this; 
     add2cart.adlstvw.setAdapter(cart); 
     notifyDataSetChanged(); 
    } 
    }); 
TextView nametxt=(TextView)List.findViewById(R.id.prdt_nme); 
final TextView pricetxt=(TextView)List.findViewById(R.id.prdt_rate); 
final TextView totltxt=(TextView)List.findViewById(R.id.prdt_totl); 
final EditText edittext=(EditText)List.findViewById(R.id.prdt_qnty); 
edittext.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     // TODO Auto-generated method stub 

     String s1=pricetxt.getText().toString(); 
     String s2=edittext.getText().toString(); 
     int i1=Integer.parseInt(s1); 
     int i2=Integer.parseInt(s2); 
     int res=i1*i2; 
     totltxt.setText(Integer.toString(res)); 
     if (s2.matches("")) { 

      edittext.setText(""); 

     } 


    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
     // TODO Auto-generated method stub 




    } 



    @Override 
    public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 

    } 
}); 

nametxt.setText(pname[position].toString()); 
pricetxt.setText(price[position]); 

    return List; 
} 


} 
+0

錯誤是什麼? – 2014-12-08 06:06:59

+0

發佈錯誤或崩潰 – 2014-12-08 06:07:04

+0

請您分享完整的錯誤或異常。 – 2014-12-08 06:45:52

回答

2

首先你需要Context類的引用來打開數據庫

SQLiteDatabase mydb=CartAdapter.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null); 

上面的代碼是錯誤的

你必須通過數據庫類的上下文,我不能在上面的行中看到...所以糾正我,如果我錯了

從上面的線
android.database.sqlite.SQLiteException: near "(": syntax error: DELETE FROM add2cart WHERE pname=button.setTag(position 

安靜乾淨存在下面的代碼語法錯誤

mMydb.execSQL("DELETE FROM add2cart WHERE pname=button.setTag(position)"); 

我想正確的代碼可能是

public void onClick(View v) { 
    // TODO Auto-generated method stub 

    int position = (Integer)v.getTag(); 
//this will give you the position of the data been clicked 
// how extract the data item from the list at the position 
// for eg that data is in pname vriable 
    SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null); 
    String pnam = pname[position]; 
    mydb.execSQL("DELETE FROM add2cart WHERE pnme = '"+pnam+"'"); 
    notifyDataSetChanged(); 


} 
}); 

你也必須從你刪除一個項目list String [] pname來反映列表視圖中的變化,您應該更好地使用列表pname作爲刪除在列表數據結構中更好

+0

你確定從數據庫中刪除了特定的列嗎? – 2015-01-07 10:16:08

+0

數據庫中的更改沒有反映在listview中。從我的列表中刪除我的代碼中是否有任何錯誤。 – droid 2015-01-12 08:42:17

0

Try repl使用您在適配器類的構造函數中傳遞的活動上下文來使用CartAdapter.this。

更換

SQLiteDatabase mydb=CartAdapter.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null); 

SQLiteDatabase mydb=cntxt.openOrCreateDatabase("addcart", MODE_PRIVATE, null); 
+0

什麼是錯誤? – TechHelper 2014-12-08 07:51:33

+0

你的查詢是錯誤的。我猜應該有'pname [position]'而不是'button.setTag(position)' – TechHelper 2014-12-08 09:09:54

0

的OnClickListener的整個設計是錯誤的。每次用戶單擊按鈕時打開數據庫都沒有任何意義。

您應該複製以下行粘貼到該適配器的構造:

mydb=cntxt.openOrCreateDatabase("addcart",Context.MODE_PRIVATE, null); 

保持mydb的一個局部變量,你可以稱之爲mMydb爲正確的代碼約定。

然後,在onclick()剛剛離開下面一行:

mMydb.execSQL("DELETE FROM add2cart WHERE pname=button.setTag(position)"); 
     notifyDataSetChanged(); 

此外,作爲@dragon出生提示 - 你應該使用封裝的活動範圍內,從而只需將其添加到適配器的情況下,並用它。

相關問題