【问题标题】:Retrieve data from sqlite android从 sqlite android 检索数据
【发布时间】:2017-11-10 17:47:17
【问题描述】:

我正在使用以下代码在 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

有什么帮助吗?

【问题讨论】:

    标签: android android-sqlite android-alertdialog android-cursor


    【解决方案1】:

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

    例如:

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

    您也可以在循环中使用moveToNext()

    【讨论】:

    • 是的,光标有SQLiteAdapter.KEY_CONTENT
    • 问题出在 row 索引,而不是 column 索引,抱歉。我改变了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多