2017-10-13 117 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

相关问题