Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取listview的项索引,并在listview和我的数据库中删除它_Android - Fatal编程技术网

Android 获取listview的项索引,并在listview和我的数据库中删除它

Android 获取listview的项索引,并在listview和我的数据库中删除它,android,Android,我现在正在进行一个项目,该项目允许用户查看某个表的列表,并具有编辑或删除该表的权限。没有错,我只是不知道该怎么办。也许任何人都可以帮我,哪怕只是删除功能?给出一个示例代码会非常有帮助。这是我的密码: SQLiteDatabase myDataBase; String DB_PATH = null; private ProgressDialog m_ProgressDialog = null; private ArrayList<Order> m_orders = null; pri

我现在正在进行一个项目,该项目允许用户查看某个表的列表,并具有编辑或删除该表的权限。没有错,我只是不知道该怎么办。也许任何人都可以帮我,哪怕只是删除功能?给出一个示例代码会非常有帮助。这是我的密码:

SQLiteDatabase myDataBase;
String DB_PATH = null;

private ProgressDialog m_ProgressDialog = null;
private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter;
private Runnable viewOrders;
int totalId, totalProdId, totalQty, totalBigQty, totalUnitQty;

Cursor cursorProduct;

String id = "";
String productid = "";
String qty = "";
String bigqty = "";
String unitqty = "";

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

    copyDB();

    myDataBase = SQLiteDatabase.openOrCreateDatabase(DB_PATH,null);

    m_orders = new ArrayList<Order>();
    this.m_adapter = new OrderAdapter(this, R.layout.dc_inventory_viewlist, m_orders);
    setListAdapter(this.m_adapter);

    viewOrders = new Runnable(){
        @Override
        public void run() {
            getOrders();
        }
    };
    Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();
    m_ProgressDialog = ProgressDialog.show(DC_invviewlist.this,
            "Please wait...", "Retrieving data ...", true);

    registerForContextMenu(getListView());
}

private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
        if(m_orders != null && m_orders.size() > 0){
            m_adapter.notifyDataSetChanged();
            for(int i=0;i<m_orders.size();i++)
                m_adapter.add(m_orders.get(i));
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();
    }
};

private void getOrders(){
    try{
        String i = getPIViewListId();
        String pi = getPIViewListProdId();
        String q = getPIViewListQty();
        String bq = getPIViewListBigQty();
        String uq = getPIViewListUnitQty();

        String[] iValue = i.split("~");
        String[] piValue = pi.split("~");
        String[] qValue = q.split("~");
        String[] bqValue = bq.split("~");
        String[] uqValue = uq.split("~");

        totalId = iValue.length;
        totalProdId = piValue.length;
        totalQty = qValue.length;
        totalBigQty = bqValue.length;
        totalUnitQty = uqValue.length;

        m_orders = new ArrayList<Order>();

        for (int j = 0; j < totalId; j++) {
            Order o = new Order();
            o.setOrderId(iValue[j]);
            o.setOrderProdId(piValue[j]);
            o.setOrderQty(qValue[j]);
            o.setOrderBigQty(bqValue[j]);
            o.setOrderUnitQty(uqValue[j]);
            m_orders.add(o);
        }

        Thread.sleep(2000);
        Log.i("ARRAY", ""+ m_orders.size());
    } catch (Exception e) {
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
    runOnUiThread(returnRes);
}


private class OrderAdapter extends ArrayAdapter<Order> {

    private ArrayList<Order> items;

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.dc_inventory_viewlist, null);
        }
        Order o = items.get(position);
        if (o != null) {
            TextView id = (TextView) v.findViewById(R.id.txtInvViewListId);
            TextView prodid = (TextView) v.findViewById(R.id.txtInvViewListProductId);
            TextView qty = (TextView) v.findViewById(R.id.txtInvViewListQty);
            TextView bigqty = (TextView) v.findViewById(R.id.txtInvViewListBigQty);
            TextView unitqty = (TextView) v.findViewById(R.id.txtInvViewListUnitQty);

            if (id != null) {
                id.setText(o.getOrderId());                            }
            if(prodid != null){
                prodid.setText(o.getOrderProdId());
            }
            if(qty != null){
                qty.setText(o.getOrderQty());
            }
            if(bigqty != null){
                bigqty.setText(o.getOrderBigQty());
            }
            if(unitqty != null){
                unitqty.setText(o.getOrderUnitQty());
            }
        }
        return v;
    }
}

public void copyDB(){
    if(android.os.Build.VERSION.SDK_INT >= 17){
        DB_PATH = getApplicationContext().getApplicationInfo().dataDir + "/databases/";
    }
    else{
        DB_PATH = "/data/data/" + getApplicationContext().getPackageName() + "/databases/";
    }
    File dbdir = new File(DB_PATH);
    if(!dbdir.exists()){
        dbdir.mkdirs();
    }
    File file = new File(DB_PATH+"DC");

    if(!file.exists()){
        try{
            InputStream is = getApplicationContext().getAssets().open("DC");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(buffer);
            fos.flush();
            fos.close();
        }catch(Exception e){ throw new RuntimeException(e);};
    }

    if(android.os.Build.VERSION.SDK_INT >= 17){
        DB_PATH = getApplicationInfo().dataDir + "/databases/DC";
    }else{
        DB_PATH = "/data/data/" + getPackageName() + "/databases/DC";
    }
}

@Override
protected void onResume() {
    super.onResume();
    myDataBase = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null);
}

@Override
protected void onPause() {
    super.onPause();
    myDataBase.close();
}

public String getPIViewListId(){
    cursorProduct = myDataBase.rawQuery("Select * from tblProductInvAdjust", null);
    if (cursorProduct.moveToFirst()) {
        do {
            id += ""+cursorProduct.getString(0)+"~";
        } while (cursorProduct.moveToNext());
    }

    if (cursorProduct != null && !cursorProduct.isClosed()) {
        cursorProduct.close();
    }
    return id;
}

public String getPIViewListProdId(){
    cursorProduct = myDataBase.rawQuery("Select * from tblProductInvAdjust", null);
    if (cursorProduct.moveToFirst()) {
        do {
            productid += ""+cursorProduct.getString(1)+"~";
        } while (cursorProduct.moveToNext());
    }

    if (cursorProduct != null && !cursorProduct.isClosed()) {
        cursorProduct.close();
    }
    return productid;
}

public String getPIViewListQty(){
    cursorProduct = myDataBase.rawQuery("Select * from tblProductInvAdjust", null);
    if (cursorProduct.moveToFirst()) {
        do {
            qty += ""+cursorProduct.getString(2)+"~";
        } while (cursorProduct.moveToNext());
    }

    if (cursorProduct != null && !cursorProduct.isClosed()) {
        cursorProduct.close();
    }
    return qty;
}

public String getPIViewListBigQty(){
    cursorProduct = myDataBase.rawQuery("Select * from tblProductInvAdjust", null);
    if (cursorProduct.moveToFirst()) {
        do {
            bigqty += ""+cursorProduct.getString(3)+"~";
        } while (cursorProduct.moveToNext());
    }

    if (cursorProduct != null && !cursorProduct.isClosed()) {
        cursorProduct.close();
    }
    return bigqty;
}

public String getPIViewListUnitQty(){
    cursorProduct = myDataBase.rawQuery("Select * from tblProductInvAdjust", null);
    if (cursorProduct.moveToFirst()) {
        do {
            unitqty += ""+cursorProduct.getString(4)+"~";
        } while (cursorProduct.moveToNext());
    }

    if (cursorProduct != null && !cursorProduct.isClosed()) {
        cursorProduct.close();
    }
    return unitqty;
}

final int CONTEXT_MENU_EDIT_ITEM =1;
final int CONTEXT_MENU_DELETE =2;
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    menu.add(Menu.NONE, CONTEXT_MENU_EDIT_ITEM, Menu.NONE, "Edit");
    menu.add(Menu.NONE, CONTEXT_MENU_DELETE, Menu.NONE, "Delete");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    Long id = getListAdapter().getItemId(info.position);
    switch (item.getItemId()) {
        case CONTEXT_MENU_EDIT_ITEM:
            //do smth
            return(true);
        case CONTEXT_MENU_DELETE:
            myDataBase.execSQL("DELETE FROM tblProductInvAdjust");
            return(true);
    }
    return(super.onOptionsItemSelected(item));
}
SQLiteDatabase-myDataBase;
字符串DB_PATH=null;
private ProgressDialog m_ProgressDialog=null;
private ArrayList m_orders=null;
专用OrderAdapter m_adapter;
私人可运行视图命令;
int totalId、totalProdId、totalQty、totalBigQty、totalUnitQty;
游标游标产品;
字符串id=“”;
字符串productid=“”;
字符串数量=”;
字符串bigqty=“”;
字符串unitqty=“”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dc_invviewslist);
copyDB();
myDataBase=SQLiteDatabase.openOrCreateDatabase(DB_路径,null);
m_orders=new ArrayList();
this.m_adapter=新订单适配器(this,R.layout.dc_inventory_viewlist,m_orders);
setListAdapter(此.m_适配器);
viewOrders=new Runnable(){
@凌驾
公开募捐{
getOrders();
}
};
线程线程=新线程(null,viewOrders,“MagentoBackground”);
thread.start();
m_ProgressDialog=ProgressDialog.show(DC_invviewlist.this,
“请稍候…”,“正在检索数据…”,为true);
registerForContextMenu(getListView());
}
private Runnable returnRes=new Runnable(){
@凌驾
公开募捐{
if(m_orders!=null&&m_orders.size()>0){
m_adapter.notifyDataSetChanged();
对于(int i=0;i=17){
DB_PATH=getApplicationContext().getApplicationInfo().dataDir+“/databases/”;
}
否则{
DB_PATH=“/data/data/”+getApplicationContext().getPackageName()+”/databases/”;
}
File dbdir=新文件(DB_路径);
如果(!dbdir.exists()){
dbdir.mkdirs();
}
文件文件=新文件(DB_路径+“DC”);
如果(!file.exists()){
试一试{
InputStream=getApplicationContext().getAssets().open(“DC”);
int size=is.available();
字节[]缓冲区=新字节[大小];
is.read(缓冲区);
is.close();
FileOutputStream fos=新的FileOutputStream(文件);
写入(缓冲区);
fos.flush();
fos.close();
}catch(异常e){抛出新的运行时异常(e);};
}
如果(android.os.Build.VERSION.SDK_INT>=17){
DB_PATH=getApplicationInfo().dataDir+“/databases/DC”;
}否则{
DB_PATH=“/data/data/”+getPackageName()+“/databases/DC”;
}
}
@凌驾
受保护的void onResume(){
super.onResume();
myDataBase=SQLiteDatabase.openOrCreateDatabase(DB_路径,null);
}
@凌驾
受保护的void onPause(){
super.onPause();
myDataBase.close();
}
公共字符串getPIViewListId(){
cursorProduct=myDataBase.rawQuery(“从tblProductInvAdjust中选择*”,null);
if(cursorProduct.moveToFirst()){
做{
id+=“”+cursorProduct.getString(0)+“~”;
}while(cursorProduct.moveToNext());
}
if(cursorProduct!=null&&!cursorProduct.isClosed()){
cursorProduct.close();
}
返回id;
}
公共字符串getPIViewListProdId(){
cursorProduct=myDataBase.rawQuery(“从tblProductInvAdjust中选择*”,null);
if(cursorProduct.moveToFirst()){
做{
productid+=“”+cursorProduct.getString(1)+“~”;
}while(cursorProduct.moveToNext());
}
if(cursorProduct!=null&&!cursorProduct.isClosed()){
cursorProduct.close();
}
返回productid;
}
公共字符串getPIViewListQty(){
cursorProduct=myDataBase.rawQuery(“从tblProductInvAdjust中选择*”,null);
if(cursorProduct.moveToFirst()){
做{
数量+=“”+cursorProduct.getString(2)+“~”;
}while(cursorProduct.moveToNext());
}
if(cursorProduct!=null&&!cursorProduct.isClosed()){
cursorProduct.close();
}
退货数量;
}
公共字符串getPIViewListBigQty(){
cursorProduct=myDataBase.rawQuery(“从tblProductInvAdjust中选择*”,null);
if(cursorProduct.moveToFirst()){
做{
BigQuantity+=“”+cursorProduct.getString(3)+“~”;
}while(cursorProduct.moveToNext());
}
if(cursorProduct!=null&&!cursorProduct.isClosed()){
cursorProduct.close();
}
返回大数量;
}
公共字符串getPIViewListUnitQty(){
cursorProduct=myDataBase.rawQuery(“从tblProductInvAdjust中选择*”,null);
if(cursorProduct.moveToFirst()){
做{
unitqty+=“”+cursorProduct.getString(4)+“~”;
}while(cursorProduct.moveToNext());
}
if(cursorProduct!=null&&!cursorProduct.isClosed()){
cursorProduct.close();
}
退货单位数量;
}
最终整数上下文菜单编辑项=1;
最终整数上下文菜单删除=2;
@凌驾
public void onCreateContextMenu(ContextMenu菜单,视图v,ContextMenu.ContextMenuInfo菜单信息){
添加(menu.NONE,上下文菜单编辑项,menu.NONE,“编辑”);
menu.add(menu.NONE,CONTEXT_menu_DELETE,menu.NONE,“DELETE”);
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)项。getMenuInfo();
Long id=getListAdapter().getItemId(info.position);
开关(item.getItemId()){
案例上下文菜单编辑项:
//做smth
返回(真);
案例上下文菜单删除:
execSQL(“从tblProductInvAdjust中删除”);
返回(真);
}
返回(super.onOptionsItemSelected(item));
}
我希望任何人都能帮我。我现在真的需要帮助。谢谢!:)

你可以用这个

   sql_lite_db_obj.delete(tablename, whereClause, whereArgs)
为了方便起见,我编辑了我的答案

public void clickHandler(View v) {
if (v.getId() == R.id.deleteOrder) {

int _id = 0;          
try {               
    _id = (Integer) v.getTag(R.id.orderTitle);       
} catch (Exception e) {       
    e.printStackTrace();                
}    


dh.delete(DatabaseHelpere.TableNAME, "_id=?",new String[] { String.valueOf(_id) });
}
}
如何合作