2014-12-19 128 views
1

我在我的應用程序中有一個自定義列表視圖。它的所有項目都存儲在數據庫中。每行有一個刪除按鈕。單擊刪除按鈕,我想刪除相應的行,從數據庫和Listview.I都成功地執行代碼從數據庫中刪除,但這種變化並沒有反映在我的listview中。我不明白我的datalist(datalist的名字)是什麼。Android:從按鈕點擊自定義列表視圖中刪除行點擊

add2cart.java

public class add2cart extends Activity{ 
ListView adlstvw; 
Button btn,remove_btn; 
SQLiteDatabase mydb; 
public String sme; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add2crt); 
    adlstvw=(ListView)findViewById(R.id.lstvw_add2crt); 
    btn=(Button)findViewById(R.id.place_order); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
    sme= extras.getString("dtls");  
    } 


    mydb=add2cart.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null); 
    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=new CartAdapter(this,pname,price); 
    adlstvw.setAdapter(cart); 

    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent in=new Intent(add2cart.this,buy_ltr.class); 
      Bundle bndl = new Bundle(); 
      bndl.putString("som",sme); 
      in.putExtras(bndl); 
      startActivity(in); 

     } 
    }); 

} 

} 

CartAdapter.java

public class CartAdapter extends BaseAdapter{ 

public static ListView adlstvw; 
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+"'"); 


    ////////////ADDED CODE///////////////////// 

    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

而不是使用pname.length,使用crtlist,現在當您調用notifydatasetchanged時,從該列表中刪除項目將反映回來。 @Override public int getCount(){ // TODO自動生成方法存根 return crtlist.size; } – androidStud 2014-12-19 05:35:53

回答

1

您正在使用數組,你是填充那些像婁代碼。

mydb=add2cart.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null); 
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")); 
    this.pname[i] = name; 
    this.price[i] = prprice; 
    i++; 
} 

您從數據庫中刪除項目後所做的同樣的事情。之後,再次創建適配器並將其設置爲列表視圖(您可以通過在活動中使其爲公共靜態來訪問適配器的列表視圖)。或者在填充數組後嘗試notifyDataSetChanged()。

+0

使ListView對象在活動中處於公共靜態,從適配器訪問該對象,並在按鈕內部執行listview.setAdapter()單擊偵聽器。 – 2015-01-08 09:29:44

+0

檢查上面編輯的答案 – 2015-01-08 13:08:12

+0

你是否嘗試過notifyDataSetChanged並再次設置適配器 – 2015-01-09 07:15:01

0

嘗試從適配器刪除視圖項目,而不是從列表中刪除。

listAdapter.remove(viewToRemove); listAdapter.notifyDataSetChanged();

+0

CartAdapter.remove(getItem(position)); CartAdapter.notifyDataSetChanged();你有沒有這樣做.. – arjun 2014-12-19 08:24:07

+0

是的..但它沒有奏效。 – droid 2015-01-12 08:51:27

1

直接調用notifyDataSetChanged(),沒有使用ClassName.this.notifyDataSetChanged(),因爲您已經在適配器中了。

  crtlist.remove(position); //removing from your List 
     notifyDataSetChanged(); //notify your adapter about the change. 

Reference here

+0

代碼中提到的單詞「datalist」在哪裏? – Darpan 2014-12-19 11:07:56

+0

這裏的列表將是您用來在適配器中顯示數據的列表,然後刪除其中的一行。 – Darpan 2014-12-19 11:31:03

+1

您可能希望將陣列'pname'和'price'更改爲'ArrayList',因爲它更好地處理數據。然後,調用pname.remove(pname.indexOf(pnam)); price.remove(pname.indexOf(pnam)); notifyDataSetChanged(); – Darpan 2014-12-22 11:35:49

相關問題