索引0请求大小为0 sqlite android

时间:2014-11-29 10:49:50

标签: java android sqlite

我试图在Log.xml的文本视图中可视化数据库中的联系人姓名..但是我收到了大小为0的错误索引0

这是我的代码

Log.java

package it.unimi.di.simplephone;


import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class log extends Activity{

public long rowID;
private TextView nome;
private TextView telefono;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.log);

    nome= (TextView) findViewById(R.id.textView1);
    telefono= (TextView) findViewById(R.id.TxtNum);

    Bundle extras =getIntent().getExtras();
    rowID=extras.getLong("row_id");
}

@Override
protected void onResume()
{
    super.onResume();

    new LoadContactTask().execute(rowID);
}

private class LoadContactTask extends AsyncTask<Long,Object,Cursor>
{
    Database db= new Database(log.this);

    @Override
    protected Cursor doInBackground(Long... params)
    {
        db.open();

        return db.getOneContact(params[0]);

    }

    @Override
    protected void onPostExecute(Cursor result)
    {
        super.onPostExecute(result);
        result.moveToFirst();

            int nomeIndex=result.getColumnIndex("nome");
            //int phoneIndex=result.getColumnIndex("telefono");
            nome.setText(result.getString(nomeIndex));
            result.close();
            db.close();



    }

}
} 

这是MainActivity.class

package it.unimi.di.simplephone;

import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity{

private ListAdapter mAdapter = null;
//database
private Database db;
Intent cont;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //database

    Cursor c = getContentResolver().query(
            CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    String[] columns = new String[] { CommonDataKinds.Phone.DISPLAY_NAME };
    int[] names = new int[] { R.id.contact_name };

    mAdapter = new SimpleCursorAdapter(this, R.layout.row_layout, c,
            columns, names,
            SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    setListAdapter(mAdapter);

    db=new Database(MainActivity.this);

    Button button = (Button) findViewById(R.id.BtnComponi);
    button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            Intent it=new Intent(MainActivity.this,componi.class);
            startActivity(it);

        }
    });



}
@Override
protected void onStart() {
    super .onStart();
    Cursor c = getContentResolver().query(
            CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    String[] columns = new String[] { CommonDataKinds.Phone.DISPLAY_NAME };
    int[] names = new int[] { R.id.contact_name };

    mAdapter = new SimpleCursorAdapter(this, R.layout.row_layout, c,
            columns, names,
            SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    setListAdapter(mAdapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Cursor c = (Cursor) mAdapter.getItem(position);
    String phone = c.getString(c
            .getColumnIndex(CommonDataKinds.Phone.NUMBER));

    //database
    String nome=c.getString(c.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
    db.insertCall("log",phone,nome);
     cont = new Intent(MainActivity.this,log.class);
    cont.putExtra("row_id", id);


    Intent i = new Intent(Intent.ACTION_CALL);
    i.setData(Uri.parse("tel:" + phone));
    startActivity(i);

    Button log = (Button) findViewById(R.id.LastCall);
    log.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            startActivity(cont);

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);



    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}



}

这个id DatabaseOpenHelper类

package it.unimi.di.simplephone;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseOpenHelper extends SQLiteOpenHelper {

public DatabaseOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
    super(context, name, factory, version);

}

@Override
public void onCreate(SQLiteDatabase db) {
    String CreateQuery = "CREATE TABLE log"+
"(_id integer primary key autoincrement,"+
            "nome TEXT, phone TEXT);";
    db.execSQL(CreateQuery);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

这是数据库类

package it.unimi.di.simplephone;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class Database{

private static final String DATA_NAME ="Log";
public SQLiteDatabase db;
private DatabaseOpenHelper dbHelper;

public  Database(Context ctx)
{
    dbHelper= new DatabaseOpenHelper(ctx, DATA_NAME,null,1);

}

public void open() throws SQLException
{
    db = dbHelper.getWritableDatabase();
}

public void close()
{
    db.close();
}

public void insertCall(String string, String phone, String nome) {
    ContentValues cv=new ContentValues();
    cv.put("nome", nome);
    //cv.put("telefono", phone);
    open();
    db.insert("log","nome",cv);
    close();

}

public Cursor getOneContact(Long id) {
    Cursor result=db.query("log", null,"_id="+id, null, null, null, null, null);
    return result;
}



}

我尝试在TextView中可视化我想在数据库中插入的联系人的名称。数据库必须包含应用程序的最后10次调用。但是光标始终为0 ..我不明白为什么..

由于

我试试这个

Cursor result= db.query("log", new String[] {"nome"},"_id="+id, null, null, null, null, null);

但不工作..也许查询创建表...不工作而不创建表?..我不知道..

1 个答案:

答案 0 :(得分:-1)

在getOneContact()中尝试这样,

Cursor cursor = db.rawQuery(&#34; select * from log where _id =&#34; + id,null);