2012-08-07 453 views
2

嗨,我想檢查所有項目的listview.For我使用的菜單。
如果選擇全部菜單,則必須選擇所有數據。同樣,如果我從菜單中選擇刪除,則必須刪除列表視圖的所有值。
我正確選擇listview項目。但是,如果我取消選中任何複選框並選擇刪除,則會顯示「請選擇一個項目」。這是一個錯誤。
這意味着如果我選擇所有項目,然後取消選中任何只有一個列表視圖的項目,選中的值必須被刪除。
我不知道該怎麼做。如何只從列表視圖中刪除選定的項目?

如果有人知道,請幫助我。

代碼:

1)定義的ArrayList:

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(); 
        }else{ 
         getStrinValue = getStrinValue + "," 
         + cb.getTag().toString(); 
        }                        
       }    
      } 

     } 
     return true; 
    } 

    @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.dummy, cur); 
     } 

     @Override 
     public View newView(Context context, Cursor cur, ViewGroup parent) { 
      LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      return li.inflate(R.layout.dummy, 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

爲什麼你使用Menu ...這是非常簡單的沒有... – 2012-08-07 12:34:54

+0

我們怎麼能沒有菜單? – Manikandan 2012-08-07 12:35:22

+3

嗨user1498488,小小的評論什麼是不是關於這個問題,但一些提示。 如果你想讓你的評論被NilayOnAndroid注意到,那麼開始你的評論:@NilayOnAndroid。這樣他在收件箱中收到通知。 第二,你的接受比率有點低,人們會幫助你更快,當時更高。 請格式化你的問題一點(更容易閱讀)我現在編輯它。 – Bigflow 2012-08-07 12:48:14

回答

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); 
     } 
    } 

非常感謝。

0

嘗試下面的代碼片段

public static ArrayList<String> item = new ArrayList<String>(); 

2)添加項是由:

item.add("string"); 

3)刪除項目:

item.remove("string"); 

4)對於CheckAll /取消選中Alllogic:

cbk = (CheckBox) findViewById(R.id.selectAllList); 
cbk.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

      if(cbk.isChecked()) 
      { 
       CheckBox cb = (CheckBox) v; 
       int itemCount = getListView().getCount(); 
       for(int i=0 ; i < itemCount ; i++){ 
        getListView().setItemChecked(i, cb.isChecked()); 

       } 

       itemList.clear(); 
       itemList.addAll(item); 


       if(item.size() != itemList.size()) 
       { 
        cbk.setChecked(false); 

       } 


      } 

      else 
      { 
       CheckBox cbk = (CheckBox) v; 
       int itemCount = getListView().getCount(); 
       for(int i=0 ; i < itemCount ; i++){ 
        getListView().setItemChecked(i, cbk.isChecked()); 
       } 
       itemList.clear(); 

      } 
      } 
     }); 
0

這裏是你必須遵循的一般路徑。

  1. 從填充ListViewArrayList中刪除該項目。
  2. 一旦做到這一點,你將不得不調用notifyDataSetChanged()Adapter正在使用的ListView
相關問題