在listview中只添加五个元素

时间:2014-01-07 06:22:26

标签: android android-listview

我想在列表视图中只添加五个数字,如果我输入第6个数字则必须显示一个数字    toast message是我的要求。我在listview中使用了arraylist。我写了一些    代码,但它没有工作。请帮助
   我

我的代码是

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

1 个答案:

答案 0 :(得分: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)这将执行大于4的大小。如果你添加6条件将执行它将不会给吐司。使用if(contactNumber.size() <= 5)