0

我正在使用以下代码在 listView 中显示来自 sqlite 的数据

public class AndroidSQLite extends Activity {

ListView listContent;
private SQLiteAdapter mySQLiteAdapter;
Button addZekr;
EditText zekrTxtEditor;

String[] items;
 boolean[] itemsChecked ;

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


    listContent = (ListView)findViewById(R.id.contentlist);

    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToWrite();
    mySQLiteAdapter.deleteAll();

    mySQLiteAdapter.insert(getResources().getString(R.string.zekr1));
    mySQLiteAdapter.insert(getResources().getString(R.string.zekr2));
    mySQLiteAdapter.insert(getResources().getString(R.string.zekr3));
    mySQLiteAdapter.insert(getResources().getString(R.string.zekr4));
    mySQLiteAdapter.insert(getResources().getString(R.string.zekr5));

    // Open the same SQLite database and read all it's content.
    mySQLiteAdapter = new SQLiteAdapter(this);
    setDataInList();

    Button addZekr = (Button) findViewById(R.id.addZekrBtn);
    addZekr.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            zekrTxtEditor = (EditText)findViewById(R.id.zekr_text);
            String newZekr = zekrTxtEditor.getText().toString();
            System.out.println("newZekr: "+newZekr);
             mySQLiteAdapter.openToWrite();
             mySQLiteAdapter.insert(newZekr); 


             setDataInList();

            }
        });

    mySQLiteAdapter.close();


    listContent.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parentView, View childView,
                int position, long id) {
            // TODO Auto-generated method stub

            String itemPostion = listContent.getItemAtPosition(position).toString();
            System.out.println("in Long Press itemPostion: "+itemPostion);

            return false;
        }

    });

    listContent.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parentView, View childView,
                int position, long id) {
            // TODO Auto-generated method stub
            String itemPostion = listContent.getItemAtPosition(position).toString();
            System.out.println("in Normal Press itemPostion: "+itemPostion);
        }
    });

}

 public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.delete: {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Dialog with simple text");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                    for (int i = 0; i < items.length; i++) {
                    if (itemsChecked[i]) {
                        Toast.makeText(getBaseContext(), items[i] + " checked!", Toast.LENGTH_LONG).show();
                    }
                }
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_LONG).show();
                }
            });
            builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Toast.makeText(getBaseContext(), items[which] + (isChecked ? "checked!" : "unchecked!"), Toast.LENGTH_SHORT).show();
                }
            });
            builder.create();
            builder.show();
        }

        break;
        }
        return true;
    }


public void setDataInList(){

        mySQLiteAdapter.openToRead();

        Cursor cursor = mySQLiteAdapter.queueAll();
        startManagingCursor(cursor);

        items = new String[]{SQLiteAdapter.KEY_CONTENT};
        int[] to = new int[]{R.id.text};

        String myContent = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT)).toString().trim();

       // long id = cursor.getLong(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));

        System.out.println("itemssss: "+myContent);

        itemsChecked = new boolean[items.length];

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.row, cursor, items, to);

        listContent.setAdapter(cursorAdapter);

        mySQLiteAdapter.close();
}

}

我想要的是获取 KEY_CONTENT 列的数据并将它们放在一个数组中以在 AlertDialog 中显示它们

所以我把这条线

String myContent = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT)).toString().trim();

但它给了我这个例外:

CursorIndexOutOfBoundsException:请求索引 -1,大小为 5

有什么帮助吗?

4

1 回答 1

0

异常意味着光标位于第一行之前,就像它创建时的情况一样。您应该使用moveToFirst()将其移动到第一行

例如 :

if (moveToFirst()) {
    String myContent = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT)).toString().trim();
    ...
} else {
   // no data
}

或者,您可以使用moveToNext(),例如在循环中。

于 2013-01-30T16:07:26.453 回答