在一台手机上修改数据库中的数据并在另一台手机上检索修改后的数据

Modifying the data in a database in one mobile and retrieving the modified data in other mobile

我创建了 3 个活动,其中一个包含下面的查询

        spsrchcity.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int itemcity, long id) {
            // TODO Auto-generated method stub
            selsrchcty=spsrchcity.getSelectedItem().toString();


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

    spsrcharea.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int itemarea, long id) {
            // TODO Auto-generated method stub
            selsrcharea=spsrcharea.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

    spsrchhstlfr.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int itemhstlfr, long id) {
            // TODO Auto-generated method stub
            selsrchfr=spsrchhstlfr.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

    spsrchhstltyp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int itemtype, long id) {
            // TODO Auto-generated method stub
            selsrchtyp=spsrchhstltyp.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });


    btnsrchrm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Cursor c;
            //sdb=openOrCreateDatabase("Hostels", MODE_PRIVATE, null);
            //c=sdb.rawQuery("select Hostel Name, City, Area, Type, For, Rating , Phone No:  from Hostels where City ='"+selcty+"' and Area='"+selarea+"' and For='"+selfr+"' and Type='"+seltyp+"' "  ,null);

            Intent isrch=new Intent(SearchRoom.this,SearchResult.class);

            Bundle bndlsrch=new Bundle();

            bndlsrch.putString("keycity", selsrchcty);
            bndlsrch.putString("keyarea", selsrcharea);
            bndlsrch.putString("keyfor", selsrchfr);
            bndlsrch.putString("keytype", selsrchtyp);
            isrch.putExtras(bndlsrch);
            startActivity(isrch);

        }
    });

另一个用于显示上述查询的结果

    srchrslt=(ExpandableListView)findViewById(R.id.expandableListView1SrchResult);
    dbsrch=openOrCreateDatabase("Hostels", MODE_PRIVATE, null);

    String scty=getIntent().getStringExtra("keycity");
    String sarea=getIntent().getStringExtra("keyarea");
    String sfor=getIntent().getStringExtra("keyfor");
    String styp=getIntent().getStringExtra("keytype");

    dbsrch=openOrCreateDatabase("Hostels", MODE_PRIVATE, null);

    crsrsrch=dbsrch.rawQuery("select Hostel Name ,  City, Area, Type, For, Rating , Phone No:  from Hostels where City ='"+scty+"' and Area='"+sarea+"' and For='"+sfor+"' and Type='"+styp+"' and Flag='1' "  ,null);


    ArrayAdapter<String> adsrch=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1);
    srchrslt.setAdapter(adsrch);


    while (crsrsrch.moveToNext()) {

    String HostelName=crsrsrch.getString(0);
    String City=crsrsrch.getString(1);
    String Area=crsrsrch.getString(2);
    String HostelType=crsrsrch.getString(3);
    String HostelFor=crsrsrch.getString(4);
    String Rating=crsrsrch.getString(5);
    String PhoneNum=crsrsrch.getString(6);

    adsrch.add("Hostel Name:"+HostelName+"\nCity:"+City+"\nArea:"+Area+"\nHostel Type:"+HostelType+"\nHostel For:"+HostelFor+"\nRating for the Hostel:"+Rating+"\nPhone Number:"+PhoneNum+"\n\n");

    }

    crsrsrch.close();
    dbsrch.close();
  }

另一个用于将值添加到数据库中,如下所示:

     name=(EditText)findViewById(R.id.editText1addname);

    phone=(EditText)findViewById(R.id.editText1addphonenum);

    addhostel=(Button)findViewById(R.id.button1addHostel);

    addhostelname=name.getText().toString();
    addhostelphone=phone.getText().toString();

    spaddcity=(Spinner)findViewById(R.id.spinner1addCity);
    ArrayAdapter<String> adaddcity=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,addcity);
    spaddcity.setAdapter(adaddcity);

    spaddarea=(Spinner)findViewById(R.id.spinner2addArea);
    ArrayAdapter<String> adaddarea=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,addarea);
    spaddarea.setAdapter(adaddcity);

    spaddhostelfor=(Spinner)findViewById(R.id.spinner3addHostelFor);
    ArrayAdapter<String> adaddhostelfor=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,addhostelfor);
    spaddhostelfor.setAdapter(adaddhostelfor);


    spaddhosteltype=(Spinner)findViewById(R.id.spinner4addHostelType);
    ArrayAdapter<String> adaddhosteltype=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,addhosteltype);
    spaddhosteltype.setAdapter(adaddhosteltype);


    spaddcity.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            seladdcty=spaddcity.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

    spaddarea.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            seladdarea=spaddarea.getSelectedItem().toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

    spaddhostelfor.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            seladdhostelfor=spaddhostelfor.getSelectedItem().toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub


        }
    });

    spaddhosteltype.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            seladdhosteltype=spaddhosteltype.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });


    addhostel.setOnClickListener(new OnClickListener() {

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


            dbadd=openOrCreateDatabase("Hostels", MODE_PRIVATE, null);

            dbadd.execSQL("insert into Hostels values('"+addhostelname+"','"+seladdcty+"','"+seladdarea+"','"+seladdhostelfor+"','"+seladdhosteltype+"','0','"+addhostelphone+"','0');");

        }
    });

现在我的问题是,如果我将它安装在 2 部手机上,如果我在一部手机上添加了一些数据,如何在另一部手机上检索该数据?

请帮帮我..

提前致谢..

赛·斯里尼瓦斯

为此,需要有一台服务器,这样对数据库的每一次更改都会发送到服务器,然后更改会通过更新反映到具有该应用程序的手机上...