如何仅将项目详细信息提供给下一个要编辑的活动

时间:2014-03-24 10:04:35

标签: android

在蓝牙设备列表中项目名称被重命名并存储在数据库中。当应用程序启动它时应该检查数据库,如果uuids相同,那么它应该显示带有这些名称的项目列表。当我点击项目然后去到下一个活动,我点击设置选项来编辑该项目的名称,然后返回到第一个activty.it更改名称,但列表视图中的所有名称都更改为已编辑的名称,所有名称都相同。

    protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null) return;
    Log.v("_____________LOG", "localdevicename : "+mBluetoothAdapter.getName()+" localdeviceAddress : "+mBluetoothAdapter.getAddress());


    Log.v("___________LOG", "localdevicename : "+mBluetoothAdapter.getName()+" localdeviceAddress : "+mBluetoothAdapter.getAddress());
    UUID deviceUuid = new UUID(device.hashCode(), ((long)device.hashCode() << 32)  );
    Log.v("__________DEVICE UUID________","__________________"+deviceUuid);
    String deviceUUID = deviceUuid.toString();
    Log.v("_______________________","___________________"+deviceUUID);

     Intent intent = new Intent(this, DeviceControlActivity.class);

     intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, sname);
     intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());//}
     intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_UUID, deviceUUID);//}
         if (mScanning) {
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        mScanning = false;
    }
    startActivity(intent);
}

    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        String deviceName = device.getName();
        UUID deviceUuid = new UUID(device.hashCode(), ((long)device.hashCode() << 32)  );
        Log.v("__________DEVICE UUID________","__________________"+deviceUuid);
        String deviceId = deviceUuid.toString();
        Log.v("_______________________","___________________"+deviceId);

        if (deviceName != null && deviceName.length() > 0){
             db.open();
        if(db!=null)
        {
             Cursor c=db.getData();
             String selectQuery = "SELECT * FROM DeviceDetails";
                c = db.select(selectQuery);
                while (c != null && c.moveToNext())
                {
          if (c.moveToFirst()) {
                do {
         if(deviceId.equalsIgnoreCase(c.getString(c.getColumnIndex("uuid"))))
                    {
                          viewHolder.deviceName.setText(c.getString(c.getColumnIndex("devicename")));
sname= c.getString(c.getColumnIndex("devicename"));
                    }
             else
                     {
                          mBluetoothAdapter.setName("NONAME");           
                     }

                } while (c.moveToNext());
            }      
        }
        }
        db.close();
        }
        else
            viewHolder.deviceName.setText(R.string.unknown_device);
        viewHolder.deviceAddress.setText(device.getAddress());

        return view;
    }
}

* 从这个类中调用下面的类,并在此我添加了设置选项。从上面的代码中它应该得到项目选择的名称和设备的uuid。 *在这里我应该只得到我选择的项目详情。

             mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
    mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
    mDeviceUUID=intent.getStringExtra(EXTRAS_DEVICE_UUID);
    Log.v("!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "__________________________________________"+mDeviceUUID);    

案例R.id.menu_settings:

    Log.v("_______mdevicename______", ""+mDeviceName);
    Log.v("_______device uuid______", ""+mDeviceUUID);

            Intent intent = new Intent(this, EditPreview.class);
         intent.putExtra("Dname", mDeviceName);
            intent.putExtra("Daddress", mDeviceUUID);
            startActivityForResult(intent,0);

            return true;

以下是我的编辑类,它添加到数据库中,当我点击edittext时,它给出的设备名称为fistrow但不是项目选择的名称。在movig回到拳头活动后,列表中的所有名称都显示相同的名称

              protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_layout);
    edittext=(EditText)findViewById(R.id.device_text);
     db = new DataBaseAdapter(this);

     Intent i = getIntent();


       if(i.hasExtra("Dname"))
             val = i.getStringExtra("Dname");

    edittext.setText(val);
    if(i.hasExtra("Daddress"))
        pos=i.getStringExtra("Daddress");
  db.open();

 db.insertData(pos,val,"off","off");

     c = db.getData();
        if (c.moveToFirst()) {
            do {
            } while (c.moveToNext());
        }
    db.close();
    }

    @Override
    public void onBackPressed(){

        db.open();
        c=db.getData();

        if (c.moveToFirst()) {
            do {

                    String strSQL = "UPDATE DeviceDetails SET devicename ='"+ edittext.getText().toString() +"'  WHERE uuid = '"+c.getString(c.getColumnIndex("uuid"))+"'" ;
                    db.select(strSQL);
                db.updateData(pos, edittext.getText().toString());

                Intent intent=new Intent();
                intent.putExtra("Dname", edittext.getText().toString());
                intent.putExtra("Daddress",pos);
                setResult(RESULT_OK, intent);
                finish();

            } while (c.moveToNext());
        }

    db.close();

    super.onBackPressed();
    }   

0 个答案:

没有答案