如何将SQLite数据库数据传递给活动并为该数据增加价值



我想向数据库中的数据添加元素。我正在使用可点击的列表视图来使用意图转到另一个活动。我已经可以转到另一个活动,但我无法获取数据,它是空的。我也想为这些数据添加一些价值,但我仍然没有找到做到这一点的方法。

这是我的意图活动的 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ap"
        android:textSize="30dp"
        android:text=""
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/ap_x"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="x (decimal)"
            android:inputType="numberDecimal"
            android:lines="1"
            android:maxLength="10" />
        <EditText
            android:id="@+id/ap_y"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="y (decimal)"
            android:inputType="numberDecimal"
            android:lines="1"
            android:maxLength="10" />
    </LinearLayout>
    <Button
        android:id="@+id/save"
        android:enabled="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Calibrate" />

</LinearLayout>

这是我的列表视图类的一部分

List<ScanResult> results;
    ListView wifisList;
    protected CharSequence[] options;
    protected boolean[] selections;
    ArrayAdapter<Router> arrayAdapter;
    ArrayList<Router> wifis;
    String building;
    Button save;
    public final static String ID="com.example.indoorpositioning._SSID";
    DatabaseHelper db;
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.settings);
        db = new DatabaseHelper(this);
        addWifi = (Button) findViewById(R.id.button_add);
        wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        addWifi.setOnClickListener(new ButtonClickHandler());
        save=(Button) findViewById(R.id.save);
        wifisList = (ListView) findViewById(R.id.friendly_wifis);
        Intent intent=getIntent();
        building = intent.getStringExtra("BUILDING_NAME");
        wifis=db.getFriendlyWifis(building);
        arrayAdapter = new ArrayAdapter<Router>(this,
                android.R.layout.simple_list_item_1, wifis);
        // Set The Adapter
        wifisList.setAdapter(arrayAdapter);
        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(db.addFriendlyWifis(building,wifis))
                {
                    Toast toast = Toast.makeText(getApplicationContext(),"Saved :)", Toast.LENGTH_SHORT);
                    toast.show();
                }


            }
        });
        wifisList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                Intent appInfo = new Intent(FriendlyWifis.this, ApKoor.class);
                appInfo.putExtra("SSID", String.valueOf(id));
                startActivity(appInfo);
            }
        });

这是我的数据库类

private static final String DATABASE_PATH = "/data/data/com.example.indoorpositioning/databases/";
    public static final String DATABASE_NAME = "u236344269_indor.db";
    public static final String AP_TABLE = "access_points";
    public static final String READINGS_TABLE = "readings";
    public static final String AP_CREATE = "CREATE TABLE 'access_points' "
            + "('building_id' TEXT NOT NULL ,'ssid' TEXT NOT NULL,'mac_id' TEXT NOT NULL )";
    public static final String READINGS_CREATE = "CREATE TABLE 'readings' ('building_id' TEXT NOT NULL , "
            + "'position_id' TEXT NOT NULL , 'x' FLOAT NOT NULL, 'y' FLOAT NOT NULL, "
            + " 'ssid' TEXT NOT NULL , 'mac_id' TEXT NOT NULL , 'rssi' INTEGER NOT NULL )";
---------------------------------------------------------------------------
---------------------------------------------------------------------------
    public ArrayList<Router> getFriendlyWifis(String building_id) {
        ArrayList<Router> result = new ArrayList<Router>();
        System.out.println(building_id);
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select ssid,mac_id from " + AP_TABLE
                + " where building_id=?", new String[] { building_id });
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            result.add(new Router(cursor.getString(0), cursor.getString(1)));
            cursor.moveToNext();
        }
        return result;
    }
---------------------------------------------------------------------------
---------------------------------------------------------------------------
    public boolean addFriendlyWifis(String building_id, ArrayList<Router> wifis) {
        deleteFriendlyWifis(building_id);
        SQLiteDatabase db = getWritableDatabase();
        for (int i = 0; i < wifis.size(); i++) {
            ContentValues cv = new ContentValues();
            cv.put("building_id", building_id);
            cv.put("ssid", wifis.get(i).getSSID());
            cv.put("mac_id", wifis.get(i).getBSSID());
            db.insert(AP_TABLE, null, cv);
        }
        System.out.println("Adding done");
        return true;
    }

这是我的意图活动

    String passedVar = null;
    private TextView passedView = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ap_koor);
        passedVar = getIntent().getStringExtra(FriendlyWifis.ID);
        passedView=(TextView)findViewById(R.id.ap);
        passedView.setText("AP="+passedVar);
    }

BUILDING_PATH也说它从未使用过。

wifisList.setOnItemClickListener中,您传递的意图名称SSID

 Intent appInfo = new Intent(FriendlyWifis.this, ApKoor.class);
 appInfo.putExtra("SSID", String.valueOf(id));

但是在接收意图活动中,您的意图使用了错误的名称。

passedVar = getIntent().getStringExtra(FriendlyWifis.ID);

您应该替换此行

public final static String ID="com.example.indoorpositioning._SSID";

SSID.

public final static String ID="SSID";

最新更新