这个代码对我有用。.
package com.Itrack.Mobile;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class listV extends ListActivity {
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check if the database exist //
db = openOrCreateDatabase(
"itrackmobile.sqlite"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
try
{
Cursor c = db.query("basico", new String[]
{"_id","codigo","cantidad","fecha"},null,null,null,null,null);
// rutina de prueba //
ArrayList<String> mArrayList = new ArrayList<String>();
c.moveToFirst();
while(!c.isAfterLast()) {
mArrayList.add("ID: " +c.getString(c.getColumnIndex("_id")) +
"\nCodigo : " + c.getString(c.getColumnIndex("codigo")) + "\nCantidad : "
+ c.getString(c.getColumnIndex("cantidad")) + "\nFecha : " +
c.getString(c.getColumnIndex("fecha")) );
c.moveToNext();
}
setListAdapter( new ArrayAdapter<String>
(this,R.layout.single_item,mArrayList));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), ((TextView)
arg1).getText(), Toast.LENGTH_SHORT).show();
}
});
}
catch (RuntimeException e)
{
Log.e("basico", e.toString(), e);
}
}
}