2013-05-06 50 views
0

这里我试图输入详细信息如下,然后它需要检索。错误:CursorIndexOutOfBoundsException索引0请求,大小为0

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(""); 
     } 
    } 
    } 
} 

现在我试图找回data.But我得到CursorIndexOutOfBoundsException指数0要求,尺寸为0.1

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)); 
    } 


} 
+0

在这里,将其粘贴之前格式化你的代码。我尽可能多地完成了我的工作,但仍然很差。 – Hauleth 2013-05-06 06:48:13

回答

0
PROBLEM :- your cursor array do not contain any data in this case, in other words its `empty` 

着请求index o如果size

cursor array is 0 

,如果你还记得

indexes start from zero in array.. 

所以当实际you request 0 index它实际上means you are requesting first element of the cursor array

其中further means that cursor array contains at least one elementindex zero

其中further derives that size of cursor array is not zero在这种情况下。

所以你需要时,您所请求的指针数组if the size is 0那么should skip这一条件

请参阅该图像

中添加一些条件时,你会在这里request index 0在这个形象是will be 23

if the size of the cursor array will be zero那么你cant request the 0 index ..

这里size of array is 6

和,但indexes of array are in the range of 0-5

Array indexing

相关问题