2016-04-22 18 views
1

我的問題是如何使用另一個putExtra方法將Rating bar值傳遞給另一個活動?任何人都可以解決此問題? 這是我的第一項活動使用數據庫傳遞意圖時的問題

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) { 

    } 
} 
+0

請寫下您的結論。如果你有錯,然後給錯誤日誌。 –

+1

當前代碼有什麼問題 –

+0

我目前的代碼沒問題,但我想用另一個putExtra方法傳遞float值。 – AnilkumarJohn

回答

0

你可以簡單地用keyName再添putExtra

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

而且從項目活動

Intent i = getIntent(); 
    String name; 
    String rating; 

    Bundle b = getIntent().getExtras(); 

    name= b.getString("name"); 
    rating= b.getString("rating"); 

    textView.setText(name); 
    textView1.setText(rating); 

    textView.setTextSize(20); 

希望這是對你有幫助搞定。

+0

但它不工作我已經嘗試此代碼early.but此評級是浮動值 – AnilkumarJohn

+0

//使用此代碼 意圖我= getIntent(); 字符串名稱; 浮動評級; Bundle b = getIntent()。getExtras(); name = b.getString(「name」); rating = b.getFloatExtra(「rating」,0); textView.setText(name); textView1.setText(將String.valueOf(等級); textView.setTextSize(20); –

+0

然後在主要活動中哪些是主要活動浮法 – AnilkumarJohn