2013-05-06 47 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

相關問題