获得多人的位置

时间:2013-11-05 17:21:49

标签: android

我的应用程序一次只能找到一个人,但如果我希望我的应用程序一次找到多个人,该怎么办? 我尝试通过使用for循环来实现这一点但它不起作用..我在我的应用程序中有一个编辑文本和一个名为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();
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

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

你的代码不好。

首先,将listView.setAdapter放在btnAdd.setOnClickListener中,这样,每次点击btnAdd,listView setAdapter一次。

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

答案 1 :(得分:0)

使用:

if (contacts.size > 0)
  for(int i = 0; i < contacts.size(); i++) {
     ...