Autore Topic: Listview e CustomCursorAdapters  (Letto 556 volte)

Offline Maschiaccio

  • Nuovo arrivato
  • *
  • Post: 7
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy S2 - Sony Tablet S
  • Sistema operativo:
    Wndows7 - Netbeans
Listview e CustomCursorAdapters
« il: 05 Dicembre 2012, 23:08:41 CET »
0
Il problema è il seguente... da una Activity, premendo un pulsante devo lanciare una listview popolata con elementi di un DB SQLite...

questo è il sorgente dell'evento click del pulsante:

    public void OnSearchTroups(View v) {
        Intent myIntent = new Intent(this, ActivitySearch.class);
        startActivityForResult(myIntent, 0);
    }

questo è quello dell'activity chiamata (ActivitySearch)

public class ActivitySearch extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor myCursor = Main.myDb.rawQuery("Select Name, Speed FROM TGSpeed order by itemID");
        if (myCursor.getCount() > 0) {
            startManagingCursor(myCursor);
            String[] DbColumns = new String[]{"Name"};
            int[] Views = new int[]{android.R.id.text1};
            setListAdapter(new DbToListViewAdapter(this, myCursor, android.R.layout.simple_list_item_1, DbColumns, Views, 1));
        }
        //        getListView().setTextFilterEnabled(true);
    }

ed infine... l'adapter: (DbToListViewAdapter)

public class DbToListViewAdapter extends CursorAdapter {

    private String mVisibleDBColumns[];
    private int mOutViews[];
    private int mNumColumns;
    private int mLayoutToUse;

    public DbToListViewAdapter(Context context, Cursor c, int ListViewLayoutToUse, String VisibleDbColumns[], int OutViews[], int numColumns) {
        super(context, c);
        mVisibleDBColumns = VisibleDbColumns;
        mOutViews = OutViews;
        mNumColumns = numColumns;
        mLayoutToUse = ListViewLayoutToUse;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(mLayoutToUse, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        int ciclo;
        for (ciclo = 0; ciclo < mNumColumns; ciclo++) {
            ((TextView) view.findViewById(mOutViews[ciclo])).setText(cursor.getString(cursor.getColumnIndex(mVisibleDBColumns[ciclo])));
        }
    }
}


in pratica... alla riga     super(context, c);  l'app si chiude con exception...

logcat dell'errore:

21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime   FATAL EXCEPTION: main
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime   java.lang.RuntimeException: Unable to start activity ComponentInfo{Calculator.com/Calculator.com.ActivitySearch}: java.lang.IllegalArgumentException: column '_id' does not exist
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.ActivityThread.access$2300(ActivityThread.java:125)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.os.Handler.dispatchMessage(Handler.java:99)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.os.Looper.loop(Looper.java:123)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.ActivityThread.main(ActivityThread.java:4627)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at java.lang.reflect.Method.invokeNative(Native Method)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at java.lang.reflect.Method.invoke(Method.java:521)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at dalvik.system.NativeStart.main(Native Method)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime   Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:122)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at Tools.com.DbToListViewAdapter.<init>(DbToListViewAdapter.java:27)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at Calculator.com.ActivitySearchTroups.onCreate(ActivitySearch.java:30)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
21:41:15.698   850   Calculator.com   ERROR   AndroidRuntime       ... 11 more

cosa sarebbe column _id ????  cosa sto sbagliando?

sono novello con Java ed android...  scusate l'ignoranza :D

grazie a quelli che vorranno aiutarmi
« Ultima modifica: 05 Dicembre 2012, 23:39:59 CET da Maschiaccio »