【发布时间】:2014-07-30 17:04:09
【问题描述】:
我有一个编写 SQLite 数据库的程序和一个读取数据库的 VIEW 按钮。每当我单击 VIEW 按钮时,它什么都不做,我什至没有收到任何错误或警告。我不知道我错过了什么。谁能帮帮我。我确定我错过了一些非常小的东西,但我不知道是什么。
package com.newboston.thefirst;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SQLiteExample extends Activity implements OnClickListener{
Button sqlUpdate, sqlView;
EditText sqlName, sqlFriend;
boolean worked;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqliteexample);
sqlUpdate = (Button) findViewById(R.id.bsqlUpdate);
sqlView = (Button) findViewById(R.id.bsqlview);
sqlName = (EditText) findViewById (R.id.etsqlName);
sqlFriend = (EditText) findViewById(R.id.etsqlfrnd);
sqlUpdate.setOnClickListener(this);
sqlName.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bsqlUpdate:
try {
String name = sqlName.getText().toString();
String frnd = sqlFriend.getText().toString();
Friendliness entry = new Friendliness (SQLiteExample.this);
entry.open();
entry.createEntry(name, frnd);
entry.Close();
worked = true;
} catch (SQLiteException e) {
worked= false;
} finally {
if (worked){
Dialog d = new Dialog(this);
d.setTitle("success!");
TextView tv = new TextView(this);
tv.setText("done!");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.bsqlview:
sqlView.setText("done");
Intent i = new Intent ("com.newboston.thefirst.SQLView");
startActivity(i);
break;
}
}
}
我的 xml 代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/etsqlName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friendliness" />
<EditText
android:id="@+id/etsqlfrnd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/bsqlUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update SQLite Database" />
<Button
android:id="@+id/bsqlview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View" />
</LinearLayout>
而我的 SQLView 代码是:
package com.newboston.thefirst;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SQLView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
Friendliness info = new Friendliness(this);
info.open();
String data = info.getData();
info.Close();
tv.setText(data);
}
}
如果您需要任何其他代码,请告诉我。
【问题讨论】:
-
有没有可以发布的崩溃日志?
-
它不会崩溃,也不会给我错误。我只是错过了正确变量上的 onClickListener。感谢回复。现在可以使用了
标签: java android eclipse sqlite