Intent传递使用数据库的问题

时间:2016-04-22 12:40:27

标签: java android android-intent

我的问题是如何使用另一个putExtra方法将评级栏值传递给另一个活动?任何一个解决这个问题? 这是我的第一个活动

public void giveRating(View v)


    {
            String Name =  spinner.getSelectedItem().toString();
            Float Rating = ratingBar.getRating();
        String query = "insert into Rating values('"+Name+"',"+Rating+")";
        try
        {
            sqLiteDatabase.execSQL(query);
            AlertDialog.Builder ab = new AlertDialog.Builder(this);
            ab.setMessage("DATA SAVED");
            ab.show();
        }catch (Exception e)
        {
            Log.e("InsertionException",""+e);
        }
    }
    public void viewRating(View v)
    {
        try {
            StringBuffer sb = new StringBuffer();
            String query = "select * from Rating";
            Cursor cursor = sqLiteDatabase.rawQuery(query, null);
            boolean res = cursor.moveToFirst();
            if (res) {
                do {
                    String Name = cursor.getString(0);
                    String Rating = cursor.getString(1);

                    sb.append(Name + ":" + Rating+"\n");
                } while (cursor.moveToNext());
            } else {

                AlertDialog.Builder ab = new AlertDialog.Builder(this);
                ab.setMessage("No data To read");
                ab.show();
            }

            String value = sb.toString();
            Intent i = new Intent(MainActivity.this, Items.class);
            i.putExtra("k1", value);
            startActivity(i);


        }
        catch (Exception e)
        {
            Log.e("ReadDataEXception",""+e);
        }

第二项活动

TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.items_layout);
        textView = (TextView)findViewById(R.id.tv3);
        textView1 = (TextView)findViewById(R.id.tv4);
        Intent i = getIntent();
        String name;
        Bundle b = getIntent().getExtras();
        name= b.getString("k1");
        textView.setText(name);
        textView.setTextSize(20);

数据库活动

 public Database(Context c)


    {
        super(c,DBNAME,null,VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try {
            String query = "create table Rating(Name TEXT,Rating REAL)";
            db.execSQL(query);
        }catch (Exception e)
        {
            Log.e("TableCreationException",""+e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

1 个答案:

答案 0 :(得分:0)

您只需添加另一个OnPaintputExtra类似

keyName

从项目活动中获取

    Intent i = new Intent(MainActivity.this, Items.class);
    i.putExtra("name", name);
    i.putExtra("rating",rating);
    startActivity(i);

希望它对你有所帮助。