如何在数据库中存储屏幕的坐标以及如何在单击按钮时检索所有屏幕的坐标

时间:2016-07-22 18:58:29

标签: java android

我想创建一个存储屏幕坐标的应用程序并将其存储在数据库中,并希望通过点击按钮检索它们

这是我的数据库代码

package com.example.android.touchview;

/**
 * Created by Vikas on 7/20/2016.
 */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.sql.SQLException;
import java.util.ArrayList;


    public class DataBase
    {
        public static final String KEY_ROWID = "_id";
        public static final String KEY_X = "X";
        public static final String KEY_Y = "Y";

        private static final String DATABASE_NAME = "Details";
        private static final String DATABASE_TABLE = "DetailsTable";
        private static final int DATABASE_VERSION = 1;

        private DbHelper ourHelper;
        private final Context ourContext;
        private SQLiteDatabase ourDatabase;

        private static class DbHelper extends SQLiteOpenHelper {

            public DbHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
                // TODO Auto-generated constructor stub
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
                db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_X + " TEXT, " + KEY_Y + " TEXT);");
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
                onCreate(db);
            }
        }

        public DataBase(Context c) {
            ourContext = c;
        }

        public DataBase open() throws SQLException {
            ourHelper = new DbHelper(ourContext);
            ourDatabase = ourHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            ourHelper.close();
        }

        public long createEntry(String x, String y) {
            ContentValues cv = new ContentValues();
            cv.put(KEY_X, x);
            cv.put(KEY_Y, y);
            return ourDatabase.insert(DATABASE_TABLE, null, cv);
        }
  public ArrayList<Float> getData() {
    ourDatabase = ourHelper.getReadableDatabase();
    Point p = new Point();
    ArrayList<Float> dataPoints = new ArrayList<>();
    String[] col = new String[]{KEY_ROWID, KEY_X, KEY_Y};
    Cursor c = ourDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
    float[] array = new float[c.getCount()];
    float[] array1 = new float[c.getCount()];
    int i = 0;
    if (c.moveToFirst())
    {
        do {
            float x = c.getFloat(c.getColumnIndex("X"));
            p.setX(x);
            dataPoints.add(p.getX());

            float y = c.getFloat(c.getColumnIndex("Y"));
            p.setY(y);
            dataPoints.add(p.getY());
            array[i] = x;
            array1[i] = y;
            i++;
        } while (c.moveToNext());
    }


    Log.d("hi", "getData() called");
    return dataPoints;
}

这是Point.java

package com.example.android.touchview;

/**
 * Created by Vikas on 7/20/2016.
 */
public class Point
{

        public float x;
        public float y;
        int id;

        public Point() {}

        public Point(int id, float x, float y) {
            this.id = id;
            this.x = x;
            this.y = y;
        }
        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }
        public int getId(){
            return this.id;
        }
        public void setId(int id){
            this.id= id;
        }
        public float getX(){
            return this.x;
        }
        public  void setX(float x){
            this.x=x;
        }
        public float getY(){
            return this.y;
        }
        public void setY(float y){
            this.y=y;
        }
    }

这是我的主要活动

package com.example.android.touchview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {




        float x = 0, y = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

           addTouchListener();

          //  final CanvasView canvas = (CanvasView) findViewById(R.id.paint_view);



        }
    private  void addTouchListener()
    {
       // TextView tx = (TextView)findViewById(R.id.SQLinfo);
        RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.activityView);
        relativeLayout.setOnTouchListener(new OnTouchListener()
        {
            public boolean onTouch(View v, MotionEvent event)
            {

                 x=event.getX();
                 y=event.getY();

                String text = "You click at x = " + event.getX() + " and y = " + event.getY();
                Toast.makeText(getApplicationContext(),text, Toast.LENGTH_LONG).show();
                DataBase entry = new DataBase(MainActivity.this);
                boolean flag = true;
                try {
                    entry.open();
                    entry.createEntry(Float.toString(x), Float.toString(y));
                    entry.close();
                } catch (Exception e) {
                    flag = false;
                    Toast.makeText(getApplicationContext(), "Not Working", Toast.LENGTH_LONG).show();
                } finally {
                    if (flag)
                        Toast.makeText(getApplicationContext(), "Working", Toast.LENGTH_SHORT).show();
                }
              return false;
        }
    });

        //Setting touch event listener for the PaintView
           Button data = (Button) findViewById(R.id.db);
            data.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(MainActivity.this, DbActivity.class);
                    startActivity(i);
                }
            });




    }

}

这是我的Dbactivity.java

package com.example.android.touchview;

/**
 * Created by Vikas on 7/20/2016.
 */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.sql.SQLException;
import java.util.ArrayList;

public class DbActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);
        TextView tv = (TextView) findViewById(R.id.SQLinfo);
        DataBase db = new DataBase(this);
        try {
            db.open();
            ArrayList<Float> data = db.getData();
            db.close();


            data.toString();
            //Log.d("THE DATA IS ", "+ data.get(i)");

           /* for(int i=0;i<data.size();i++)
            {
                tv.setText(data.get(i).toString());
                Log.d("hi","data[i]");
            }*/
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

请帮我修改此代码,以便我可以从数据库中获取检索到的值

0 个答案:

没有答案