I am Writing my first code to add and fetch some data from a database .:
以下是我在 Mainactivity.java 文件中编写的代码及其给出的一些错误,例如令牌“Private”上的语法错误,“Public”也应为私有;我也附上了屏幕截图,有人可以帮忙解决这个问题。因为我是 java 和 android 代码的新手
包 com.example.ghrixdatabse;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.TableLayout;
public class MainActivity extends Activity {
private static final EditText FindViewById = null;
String fname,lname,email;
SQLiteDatabase db;
@Override
protected 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 VARCHAR,lname VARCHAR,email VARCHAR);" );
}
Private 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+"');");
}
Private void Showdata(View view)
{
Cursor c = db.rawQuery(SELECT * FROM from",null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext () );
tableLayout.setVerticalScrollBarEnabled(true);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("Firstname");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("LastName");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableRow = new TableRow(getApplicationContext());
textView = new TextView(getApplicationContext());
textView.setText("Email");
textView.setColor(Color.Red);
textView.setTypeface(null,Typeface.Bold);
textView.setPadding(20,20,20,20);
tableRow.addView(textView);
tableLayout.addView(tableRow);
for (Integer j=0;j < count;j++)
{
tableRow =new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColoumIndex('fname')));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColoumIndex('lanme')));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColoumIndex('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);
}
}
这是 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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="49dp"
android:text="FirstName" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:text="Lastname" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_centerVertical="true"
android:text="E-mail" />
<EditText
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/lastname"
android:ems="10" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/email"
android:layout_marginBottom="24dp"
android:onClick="showdata"
android:text="Show Table" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignRight="@+id/button3"
android:layout_marginBottom="26dp"
android:layout_marginRight="17dp"
android:onClick="close"
android:text="Close" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/textView3"
android:onClick="Adddata"
android:text="Create" />
</RelativeLayout>