2014-01-07 24 views
0

我想在列表視圖中只添加五個數字,如果我輸入第六個數字,它必須顯示一個
Toast消息,這是我的要求。我在列表視圖中使用arraylist .i寫道一些
代碼,但它不work.please幫助
在列表視圖中只添加五個元素

我的代碼是

private EditText nuberText; 
private Button addBtn; 
private ListView listShowContacts; 

private Builder dialog; 

//ReceivingNumbers numbers = new ReceivingNumbers(); 
private ArrayList<String> contactNumber = new ArrayList<String>(); 


ArrayAdapter<String> adapter ; 
int clickCounter=0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.addordelnumbers); 
    listShowContacts = (ListView)findViewById(R.id.showlist); 
    contactNumber =ReceivingNumbers.getNumbers(); 
    Log.e("ContactNumbers",contactNumber.get(0)); 

    setUpView(); 


    listShowContacts.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
       final int arg2, long arg3) { 
      // TODO Auto-generated method stub 

      dialog = new Builder(AddOrDelNumbers.this); 

      dialog.setTitle("ALERT"); 
      dialog.setMessage(contactNumber.get(arg2)+" Do You Want to Delete This Number?"); 

      dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        contactNumber.remove(arg2); 
         adapter.notifyDataSetChanged(); 

       } 


      }); 
      dialog.show(); 


      return false; 
     } 

    }); 


// adapter= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,contactNumber); 
// listShowContacts.setAdapter(adapter); 
} 
private void setUpView(){ 

    addBtn = (Button)findViewById(R.id.addBtn); 
    nuberText = (EditText)findViewById(R.id.enterNumber); 

    if(contactNumber.size()>4){ 
    adapter= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,contactNumber); 
    listShowContacts.setAdapter(adapter); 
    } 
    else{ 
     Toast.makeText(getApplicationContext(), "enter only five numbers",100).show(); 
    } 



    addBtn.setOnClickListener(new OnClickListener() { 

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

      addItemList(); 

     } 
    }); 

} 

private void addItemList(){ 
    if(isInputValid(nuberText)){ 

     contactNumber.add(nuberText.getText().toString()); 
     adapter.notifyDataSetChanged(); 

    } 
} 
private boolean isInputValid(EditText ed){ 
    if(nuberText.getText().toString().trim().length()<1){ 
     nuberText.setError("please enter number"); 
     return false; 

    }else{ 
     return true; 
    } 
} 


i wrote like this,but it not working 



    if(contactNumber.size()>4){ 
    adapter= new ArrayAdapter<String>  
    (getApplicationContext(),android.R.layout.simple_list_item_1,contactNumber); 
    listShowContacts.setAdapter(adapter); 
    } 
    else{ 
     Toast.makeText(getApplicationContext(), "enter only five 
    numbers",100).show(); 
    } 
+0

在arraylist大小上放置一個標誌,並相應地顯示一個烤麪包。 – Skynet

+0

請根據這個發送一些代碼 – Durga

回答

0

試試這個..

如果您使用adapter.notifyDataSetChanged();添加項目再次調用該方法它只刷新列表視圖它不會檢查條件。

private void addItemList(){ 
    if(isInputValid(nuberText)){ 

     contactNumber.add(nuberText.getText().toString()); 
     setUpView(); 

    } 
} 

private void setUpView(){ 

    addBtn = (Button)findViewById(R.id.addBtn); 
    nuberText = (EditText)findViewById(R.id.enterNumber); 

    if(contactNumber.size() <= 5){ 
    adapter= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,contactNumber); 
    listShowContacts.setAdapter(adapter); 
    } 
    else{ 
     Toast.makeText(getApplicationContext(), "enter only five numbers",100).show(); 
    } 



    addBtn.setOnClickListener(new OnClickListener() { 

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

      addItemList(); 

     } 
    }); 

} 

那麼你的條件是if(contactNumber.size() > 4)這個,如果你加6這個條件將執行它會不會給吐司將執行尺寸大於4。使用if(contactNumber.size() <= 5)