samedi 29 novembre 2014

index 0 requested with a size of 0 sqlite android


Vote count:

0




I've tried to visualize contact name from database in text view in Log.xml.. but I receive error index 0 requested with a size of 0


this is my code


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



}

}
}


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



}


this id DatabaseOpenHelper class



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

}

}


this is database class



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



}


I try to visualize in TextView the name of the contact that i suppose insert in database. Database must contain the last 10 call out with the application..But the cursor is always 0.. i not understand why..


Thanks



asked 2 mins ago







index 0 requested with a size of 0 sqlite android

Aucun commentaire:

Enregistrer un commentaire