0

我的应用程序一次只能定位一个人,但是如果我希望我的应用程序一次定位多个人怎么办..我尝试使用 for 循环来做到这一点,但它不起作用..我的应用程序中有一个编辑文本和一个名为 Add 的按钮,在编辑文本上写任何数字并单击 Add ,一个数字将添加到列表中然后我有一个名为 Locate 的按钮,通过单击该定位按钮,我希望我的应用程序找到那些添加到某些清单。谁能告诉我该怎么做?这是我的代码

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fl_number_provider);
    // Show the Up button in the action bar.
    setupActionBar();

    contacts = new ArrayList<String>();
    btnAdd = (Button)findViewById(R.id.btnflAddNum);
    btnAdd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        number = (EditText)findViewById(R.id.flPhoneNumber);
        strNumber = number.getText().toString();

        if (strNumber == "")
        {
            Toast.makeText(getApplicationContext(), "Required Field is empty", Toast.LENGTH_LONG).show();

        }else{
        contacts.add(strNumber);
        //x++;
        listview = (ListView) findViewById(R.id.listView1);
        listview.setAdapter(new BaseAdapter() {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.list_row, null);
                TextView textView = (TextView) view.findViewById(R.id.TextView01);
                textView.setText(contacts.get(position));

                return view;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return contacts.get(position);
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub

                return contacts.size();
            }
        });


            number.setText("");
            }

        }
        });



    //number = (EditText) this.findViewById(R.id.flPhoneNumber);
    //listview = (ListView)findViewById(R.id.listView1);
    LocationHead = (TextView) this.findViewById(R.id.flLastKnownLocationHead);
    LastLocation = (TextView) this.findViewById(R.id.flLastLocation);
    getLocation = (Button) this.findViewById(R.id.btnflgetLocation);

    getLocation.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View arg0) {
            //String num = number.getText().toString();
            //int x = contacts.size();
            try{
            if(contacts == null)
            {

                Toast.makeText(getApplicationContext(), "0", Toast.LENGTH_SHORT).show();
            }
            else{

            //  Toast.makeText(getApplicationContext(), x+" - check", Toast.LENGTH_SHORT).show();

            for(int i = 0; i <= contacts.size(); i++)
            {
            //String x = contacts.get(i).toString();
                    sendMsg(contacts.get(i), contacts.size()+ "message family location");

            //LocationHead.setText(contacts.get(i).toString());
            //LastLocation.setText("waiting....");
            }
            }
            }catch(Exception e){

                Toast.makeText(getApplicationContext(), e.toString()+ "Field Empty", Toast.LENGTH_SHORT).show();
            }
        }
    });
4

2 回答 2

0

首先,我是中国人,所以请原谅我糟糕的英语......

你的代码不好。

首先,您将 listView.setAdapter 放在 btnAdd.setOnClickListener 中,因此,每次单击 btnAdd,listView setAdapter 一次。

另一个问题是for(int i = 0; i <= contacts.size(); i++),它会抛出一个 IndexOutOfBound 异常。这是一个致命错误,当您单击 getLocation 按钮时,应用程序将崩溃。

于 2013-11-05T17:48:00.840 回答
0

利用:

if (contacts.size > 0)
  for(int i = 0; i < contacts.size(); i++) {
     ...
于 2013-11-05T18:01:49.693 回答