2017-10-13 118 views
-1

我們的應用程序應該連接到一個SQL數據庫。它在我們的網絡中。該應用程序應該編輯數據庫中的數據。我們已經建立了連接,並且想要將一個onclicklistener設置爲Button,這會導致連接代碼連接。如何添加一個OnClicklistener到這種類型的代碼

這是我們已經得到了代碼:

public class Werte_aendern extends AppCompatActivity { 

TextView tvIP; 

String Textauslesen = tvIP.getText().toString(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    tvIP = (TextView) findViewById(R.id.tvIP); 
    setContentView(R.layout.activity_werte_aendern); 
} 




    Connection con = null; 
    //private static String dbHost = "192.168.40.148"; // Hostname 
    String dbPort = "3306";  // Port -- Standard: 3306 
    String dbName = "wasserwerte"; // Datenbankname 
    String dbUser = "App";  // Datenbankuser 
    String dbPass = "fruitcake";  // Datenbankpasswort 

    private Werte_aendern(){ 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden. 

      // Verbindung zur JDBC-Datenbank herstellen. 
      con = DriverManager.getConnection("jdbc:mysql://"+Textauslesen+":"+ dbPort+"/"+dbName+"?"+"user="+dbUser+"&"+"password="+dbPass); 
      // Statement createStatement(); 
      // SQLiteDatabase wasserwerte = 

     } catch (ClassNotFoundException e) { 
      Toast.makeText(getApplicationContext(), "Treiber nicht gefunden", Toast.LENGTH_SHORT).show(); 
     } catch (SQLException e) { 
      Toast.makeText(getApplicationContext(), "Verbindung nicht möglich", Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getApplicationContext(), "SQLException: " + e.getMessage(), Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getApplicationContext(), "SQLState: " + e.getSQLState(), Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getApplicationContext(), "VendorError: " + e.getErrorCode(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

我們是菜鳥,但我們必須爲schoolproject做到這一點。
你能幫助我們嗎?

+0

您的代碼中沒有按鈕,因此您將無法在其中添加偵聽器......您知道如何創建偵聽器嗎? 請向我們展示您聲明按鈕的位置(java代碼或xml) – deHaar

+0

@deHaar這不是必需的。在佈局中,您可以引用一個單擊事件處理程序。但是這在所示的代碼中也不存在。 –

+0

好吧,但問題是明確的OnClickListener,所以我認爲應該有一個代碼;-)我知道你可以只寫一個方法,並把它放在xml onClick。 – deHaar

回答

1

我已更新您的代碼。活動應始終命名爲[無論]活動。如果因爲我不會說德語(我認爲它是德語),所以「WerteAndern」是一個正確的名字。

public class WerteAendernActivity extends AppCompatActivity { 

    TextView tvIP; 

    // You should get the text from the View AFTER inflating the layout and find it with 
    // findViewById. Otherwise it's gonna crash. 
    String textauslesen; 
    private Button connectBtn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // First you inflate the layout 
     setContentView(R.layout.activity_werte_aendern); 
     // Then you get the views 
     tvIP = (TextView) findViewById(R.id.tvIP); 
     // Probably should be somewhere else as there is no interesting text to retrieve from the 
     // view at the moment 
     textauslesen = tvIP.getText().toString(); 

     connectBtn = (Button) findViewById(R.id.connection_button); // ! You need to add a Button in your layout 
     connectBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // Add some test, ... if you're already connected 
       connectToDataBase(); 
      } 
     }); 
    } 

    Connection con = null; 
    //private static String dbHost = "192.168.40.148"; // Hostname 
    String dbPort = "3306";  // Port -- Standard: 3306 
    String dbName = "wasserwerte"; // Datenbankname 
    String dbUser = "App";  // Datenbankuser 
    String dbPass = "fruitcake";  // Datenbankpasswort 


    // Method to connect to the database. 
    // !!! You're not supposed to override the constructor of an Activity! 
    private void connectToDataBase() { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden. 

      // Verbindung zur JDBC-Datenbank herstellen. 
      con = DriverManager.getConnection("jdbc:mysql://" + textauslesen + ":" + dbPort + "/" + dbName + "?" + "user=" + dbUser + "&" + "password=" + dbPass); 
      // Statement createStatement(); 
      // SQLiteDatabase wasserwerte = 

     } catch (ClassNotFoundException e) { 
      Toast.makeText(getApplicationContext(), "Treiber nicht gefunden", Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      Toast.makeText(getApplicationContext(), "Error! See Exception logs", Toast.LENGTH_SHORT).show(); 
      // The logs will be displayed in the Logcat window in Android Studio 
      e.printStackTrace(); 
     } 
    } 
} 
+1

你似乎確實是Android的初學者。我建議在開始使用數據庫之前,先遵循一些教程來完成非常基本的應用程序。開始一個Activity,膨脹一個佈局,獲取Views並與它們交互應該是第一步。在學會走路之前,你不能跑步! – Eselfar

+0

非常感謝。你幫了我們很多。但是,當我們點擊主活動中的按鈕時,我們的應用會一直崩潰,該活動與您看到的代碼(WerteAendernActivity)的活動相關。這個問題可能是什麼? –

+0

你是什麼意思「降級到上述活動的主要活動」?如果您的按鈕位於上述活動的佈局中,並且您嘗試從另一個佈局訪問該按鈕,則會崩潰。但至少要把錯誤日誌寫出來讓我們瞭解問題所在。 – Eselfar

相關問題