在这里,我尝试按如下方式输入详细信息,然后需要对其进行检索。
package com.example.orderform;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class NewActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
public void onClick(View V) {
Cursor c1=null;
final SQLiteDatabase db;
db=openOrCreateDatabase("Order", MODE_PRIVATE, null);
db.execSQL("Create Table if not exists codeo(name Varchar,Address Varchar,code int(10),password int(10),phno int(10),email Varchar,ccode int(10));");
final String name;
final String Address;
final String code;
final String ccode;
final String password;
final String phno;
final String email;
final EditText n=(EditText) findViewById(R.id.editText1);
final EditText c2=(EditText) findViewById(R.id.editText2);
final EditText a=(EditText) findViewById(R.id.editText3);
final EditText em=(EditText) findViewById(R.id.editText4);
final EditText ph=(EditText) findViewById(R.id.editText5);
final EditText cc=(EditText) findViewById(R.id.editText6);
final EditText pass=(EditText) findViewById(R.id.editText7);
name=n.getText().toString();
code=c2.getText().toString();
Address=a.getText().toString();
phno=ph.getText().toString();
email=em.getText().toString();
ccode=cc.getText().toString();
password=pass.getText().toString();
if(name.equalsIgnoreCase("")||code.equalsIgnoreCase("")||ccode.equalsIgnoreCase("")||password.equalsIgnoreCase("")||Address.equalsIgnoreCase("")||phno.equalsIgnoreCase("")||email.equalsIgnoreCase(""))
{
Toast.makeText(getApplicationContext(), "No Field Can be Left Blank", Toast.LENGTH_LONG).show();
}
else
{
if(code.equals(ccode))
{
try
{
c1=db.rawQuery("Select * from codeo where code='"+code+"'", null);
c1.moveToFirst();
if(c1.getCount()<=0)
{
try
{
AlertDialog.Builder alrt=new AlertDialog.Builder(NewActivity.this);
alrt.setTitle("CONFIRMATION");
alrt.setCancelable(false);
alrt.setMessage("Are You Sure that no more Changes on the above details");
alrt.setPositiveButton("YES",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
try
{
db.execSQL("Insert into codeo values('"+name+"','"+code+"','"+ccode+"','"+Address+"','"+phno+"','"+email+"','"+password+"');");
n.setText("");
c2.setText("");
pass.setText("");
a.setText("");
ph.setText("");
em.setText("");
cc.setText("");
SharedPreferences sp= getSharedPreferences("My code",0);
SharedPreferences.Editor e=sp.edit();
e.putString("code1",code);
/*e.putString("Address",Address);
e.putString("name",name);
e.putString("email", email);
e.putString("ccode",ccode);
e.putString("phno",phno);
e.putString("password",password);
e.putString("code",code);*/
e.commit();
Intent j=new Intent(getBaseContext(), New1Activity.class);
startActivity(j);
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_SHORT).show();
}
}
});
alrt.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog al= alrt.create();
al.show();
}
catch(Exception er)
{
Toast.makeText(getApplicationContext(), "Exception in getcount", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Code taken", Toast.LENGTH_SHORT).show();
}
}
catch(Exception er)
{
Toast.makeText(getApplicationContext(), "Exception in isfirst", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Code and Confirm Code doesn't Match", Toast.LENGTH_LONG).show();
c2.setText("");
cc.setText("");
}
}
}
}
现在我正在尝试检索数据。但是我正在请求 CursorIndexOutOfBoundsException 索引 0,大小为 0。
package com.example.orderform;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class New1Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new1);
String name,Address,code,phno,email,ccode,password;
Cursor c;
SQLiteDatabase db;
SharedPreferences sp=getSharedPreferences("NewActivity", 0);
name=sp.getString("name", "");
code=sp.getString("code", "");
Address=sp.getString("Address", "");
phno=sp.getString("phno", "");
email=sp.getString("email","");
ccode=sp.getString("ccode", "");
password=sp.getString("password", "");
db=openOrCreateDatabase("Order", MODE_MULTI_PROCESS, null);
db.execSQL("Create Table if not exists codeo(name Varchar,Address Varchar,code
int(10),password int(10),phno int(10),email Varchar,ccode int(10));");
c=db.rawQuery("Select * from codeo where code='"+code+"'", null);
c.moveToFirst();
name=c.getString(c.getColumnIndex("name"));
code=c.getString(c.getColumnIndex("code"));
Address=c.getString(c.getColumnIndex("Address"));
phno=c.getString(c.getColumnIndex("phno"));
email=c.getString(c.getColumnIndex("email"));
ccode=c.getString(c.getColumnIndex("ccode"));
password=c.getString(c.getColumnIndex("password"));
TextView n=(TextView) findViewById(R.id.textView6);
n.setText(name);
TextView c2=(TextView) findViewById(R.id.textView7);
c2.setText(code);
TextView a=(TextView) findViewById(R.id.textView8);
a.setText(Address);
TextView em=(TextView) findViewById(R.id.textView9);
em.setText(email);
TextView ph=(TextView) findViewById(R.id.textView10);
ph.setText(phno);
TextView cc=(TextView) findViewById(R.id.textView12);
cc.setText(ccode);
TextView pass=(TextView) findViewById(R.id.textView14);
pass.setText(password);
}
public void onClick(View view)
{
startActivity(new Intent(this,CodeActivity.class));
}
}