2012-08-09 143 views
0

嗨我正在使用菜單來刪除列表視圖項目。
我有兩個菜單,一個是Delete,另一個是Select All。我正在從數據庫中檢索數據,並使用checkboxlistview中顯示它。
如果我選擇了某些列表項並選擇刪除,則必須刪除所選項目。如果我選擇從菜單中選擇全部,則必須選擇所有列表項,並且如果從菜單中選擇刪除,則必須刪除所有數據。
問題是如果我通過Select All菜單選擇所有listview項目,並且我取消選中某些列表項目,剩下的選定項目不會被刪除。
我已使用ResourceCursorAdapter來顯示數據庫中的列表項。使用Android中的菜單刪除列表視圖項目

private void CreateMenu(Menu menu) { 
     menu.setQwertyMode(true); 
     MenuItem mnu1 = menu.add(0, 0, 0, "Delete"); 
     { 
      mnu1.setAlphabeticShortcut('a'); 
     } 
     MenuItem mnu2 = menu.add(1, 1, 1, "Select All"); 
     { 
      mnu2.setAlphabeticShortcut('s'); 
     } 
    } 

    private boolean MenuChoice(MenuItem item) throws Exception { 
     switch (item.getItemId()) { 
     case 0: 
      if (getStrinValue != null) { 
       delhistory(getStrinValue); 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "Please select an Item", Toast.LENGTH_SHORT).show(); 
      } 
     case 1: 
      if (item.getTitle().equals("Select All")) { 
       for (int i = 0; i < lvhistory.getChildCount(); i++) { 
        RelativeLayout itemLayout = (RelativeLayout) lvhistory 
          .getChildAt(i); 
        final CheckBox cb = (CheckBox) itemLayout 
          .findViewById(R.id.check); 
        cb.setChecked(true); 
        if (cb.isChecked() == true) { 
         getStrinValue = getStrinValue + "," 
           + cb.getTag().toString(); 
         System.out.println("CHECKEDIITTEEMSS-->>>" 
           + getStrinValue); 
        } else {       
        }     
       } 
      } 
     } 
     return false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     CreateMenu(menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     try { 
      return MenuChoice(item); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return false; 
    } 

    private class MyAdapter extends ResourceCursorAdapter { 

     public MyAdapter(Context context, Cursor cur) { 
      super(context, R.layout.custom_sent_view, cur); 
     } 

     @Override 
     public View newView(Context context, Cursor cur, ViewGroup parent) { 
      LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      return li.inflate(R.layout.custom_sent_view, parent, false); 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cur) { 
      TextView tvListText = (TextView) view.findViewById(R.id.Mobile); 
      chkBox = (CheckBox) view.findViewById(R.id.check); 
      tvListText.setText(cur.getString(cur 
        .getColumnIndex(MainscreenActivity.COL_Mobile))); 
      chkBox.setTag(cur.getString(cur 
        .getColumnIndex(MainscreenActivity.COL_Sent_id))); 

      chkBox.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        CheckBox cb = (CheckBox) v; 
        if (cb.isChecked() == true) { 
         getStrinValue = getStrinValue + "," 
           + cb.getTag().toString(); 
        } else { 
         getStrinValue = null; 
        } 
       } 
      }); 
     } 

    } 

    public void delhistory(String getStrinValue) { 
     int pos1 = getStrinValue.indexOf(","); 
     if (pos1 > 0) { 
      String rowId = getStrinValue.substring(pos1 + 1); 
      String delimiter = "\\,"; 
      String[] sentID = rowId.split(delimiter); 
      for (int i = 0; i < sentID.length; i++) { 
       String temp0 = sentID[i]; 
       int id = Integer.parseInt(temp0); 
       MainscreenActivity.JEEMAAndroSMSDB 
         .delete(MainscreenActivity.Table_SentHistory, "_id=" 
           + id, null); 
      } 
      Toast.makeText(getApplicationContext(), 
        "History deleted successfully", Toast.LENGTH_SHORT).show(); 
      finish(); 
      Intent intent = new Intent(getApplicationContext(), 
        SentHistoryActivity.class); 
      startActivity(intent); 
     } 
    } 
+0

嘗試通過將getStrinValue初始化爲null,只要您將數據分配給它。 – user1203673 2012-08-09 06:15:15

+0

''Log.info()''你的'getStrinValue'在'delHistory'中,看看哪些ID傳遞到刪除。 – 2012-08-09 06:17:53

+0

@ user1203673我試過了,但它不工作。請檢查代碼嗎? – Manikandan 2012-08-09 06:29:00

回答

0

好吧,最後我自己找到了答案。我將setonclicklistener添加到複選框中以刪除未選中的值。答案如下:

private boolean MenuChoice(MenuItem item) throws Exception { 
     switch (item.getItemId()) { 
     case 0: 
      if (getStrinValue != null) { 
       delhistory(getStrinValue); 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "Please select an Item", Toast.LENGTH_SHORT).show(); 
      } 
     case 1: 
      if (item.getTitle().equals("Select All")) { 
       for (int i = 0; i < lvhistory.getChildCount(); i++) { 
        RelativeLayout itemLayout = (RelativeLayout) lvhistory 
          .getChildAt(i); 
        final CheckBox cb = (CheckBox) itemLayout 
          .findViewById(R.id.check); 
        cb.setChecked(true); 
        if (cb.isChecked() == true) { 
         getStrinValue = getStrinValue + "," 
           + cb.getTag().toString(); 
         System.out.println("CHECKEDIITTEEMSS-->>>" 
           + getStrinValue); 
        } 
        cb.setOnClickListener(new OnClickListener() { 
         @Override 
         public void onClick(View arg0) { 
          // TODO Auto-generated method stub 
          if (cb.isChecked() == false) {        
           getUnselected = cb.getTag().toString(); 
           getStrinValue = getStrinValue.replace("," 
             + getUnselected, ""); 
           System.out.println("UNCHECKED--->>>" 
             + getStrinValue); 
          } else if (cb.isChecked() == true) { 
           getStrinValue = getStrinValue + "," 
             + cb.getTag().toString(); 
           System.out.println("CHECKEDIITTEEMSS-->>>" 
             + getStrinValue); 
          } 
         } 
        }); 
       } 
      } 
     } 
     return false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     CreateMenu(menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     try { 
      return MenuChoice(item); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return false; 
    } 

    private class MyAdapter extends ResourceCursorAdapter { 

     public MyAdapter(Context context, Cursor cur) { 
      super(context, R.layout.custom_sent_view, cur); 
     } 

     @Override 
     public View newView(Context context, Cursor cur, ViewGroup parent) { 
      LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      return li.inflate(R.layout.custom_sent_view, parent, false); 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cur) { 
      TextView tvListText = (TextView) view.findViewById(R.id.Mobile); 
      chkBox = (CheckBox) view.findViewById(R.id.check); 
      tvListText.setText(cur.getString(cur 
        .getColumnIndex(MainscreenActivity.COL_Mobile))); 
      chkBox.setTag(cur.getString(cur 
        .getColumnIndex(MainscreenActivity.COL_Sent_id))); 

      chkBox.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        CheckBox cb = (CheckBox) v; 
        if (cb.isChecked() == true) { 
         getStrinValue = getStrinValue + "," 
           + cb.getTag().toString(); 
        } else { 
         getStrinValue = null; 
        } 
       } 
      }); 
     } 

    } 

    public void delhistory(String getStrinValue) { 
     int pos1 = getStrinValue.indexOf(","); 
     if (pos1 > 0) { 
      String rowId = getStrinValue.substring(pos1 + 1); 
      String delimiter = "\\,"; 
      String[] sentID = rowId.split(delimiter); 
      for (int i = 0; i < sentID.length; i++) { 
       String temp0 = sentID[i]; 
       int id = Integer.parseInt(temp0); 
       MainscreenActivity.JEEMAAndroSMSDB 
         .delete(MainscreenActivity.Table_SentHistory, "_id=" 
           + id, null); 
      } 
      Toast.makeText(getApplicationContext(), 
        "History deleted successfully", Toast.LENGTH_SHORT).show(); 
      finish(); 
      Intent intent = new Intent(getApplicationContext(), 
        SentHistoryActivity.class); 
      startActivity(intent); 
     } 
    } 

非常感謝。

相關問題