我是android数据库的新手。我正在创建一个关于数据库http://www.youtube.com/watch?v=TApjQ-LFNpI的youtube教程的应用程序。
我遵循了所有内容,当我运行应用程序时,没有错误,但在点击时没有对创建按钮的响应。
这是我的代码:
MainActivity.java
package com.example.studentdatabase;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity {
String fname,lname,email;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db= openOrCreateDatabase("MyDB1",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname TEXT,lname TEXT,email TEXT)");
}
public void Adddata(View view){
EditText editText1=(EditText) findViewById(R.id.firstname);
EditText editText2=(EditText) findViewById(R.id.lastname);
EditText editText3=(EditText) findViewById(R.id.email);
fname=editText1.getText().toString();
lname=editText2.getText().toString();
email=editText3.getText().toString();
db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"')");
}
public void showdata(View view){
Cursor c=db.rawQuery("SELECT * from Student", null);
int count=c.getCount();
c.moveToFirst();
TableLayout tableLayout=new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;
tableRow=new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Firstname");
textView.setTextColor(Color.RED);
textView.setTypeface(null,Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
textView4.setText("LastName");
textView4.setTextColor(Color.RED);
textView4.setTypeface(null,Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
textView5=new TextView(getApplicationContext());
textView5.setText("LastName");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null,Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(tableRow);
for(Integer j=0; j < count; j++){
tableRow=new TableRow(getApplicationContext());
textView1=new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("fname")));
textView2=new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("lname")));
textView3=new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("email")));
textView1.setPadding(20, 20,20, 20);
textView2.setPadding(20, 20,20, 20);
textView3.setPadding(20, 20,20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext();
}
setContentView(tableLayout);
db.close();
}
public void close(View view){
System.exit(0);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="25dp"
android:text="last Name" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp"
android:text="Email" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="27dp"
android:text="First Name" />
<EditText
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/firstname"
android:ems="10" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/textView1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/email"
android:layout_marginTop="28dp"
android:onClick="Adddata"
android:text="create" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/email"
android:layout_marginRight="42dp"
android:onClick="showdata"
android:text="Show Table" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="33dp"
android:onClick="close"
android:text="Close" />
</RelativeLayout>
答案 0 :(得分:-1)
我的代码中没有看到任何Button或onClick事件?!您确定要为按钮设置活动吗?