如何从数据库中获取listview值?

时间:2012-06-18 02:57:16

标签: android database sqlite proxy android-listview

首先,我为可能的语法错误道歉,我是巴西人 我正在尝试在变量中获取listview的值,但是当我点击 _id>时,我会收到错误的吐司。 1 {/ 1> listview

但是当我点击第一个_id 时没有任何反应!

IMG:

Image


proxylist.java

public class ProxyListView extends Activity {

    private SQLiteAdapter mySQLiteAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.proxylist);

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

        /*
         *  Create/Open a SQLite database
         *  and fill with dummy content
         *  and close it
         */
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();
        mySQLiteAdapter.insert("111.168.1.1","8080","http");
        mySQLiteAdapter.insert("222.168.2.1","8080","http");
        mySQLiteAdapter.insert("333.168.3.1","8080","http");
        mySQLiteAdapter.insert("444.168.4.1","8080","http");
        mySQLiteAdapter.insert("555.168.5.1","8080","http");
        mySQLiteAdapter.insert("666.168.6.1","8080","http");
        mySQLiteAdapter.insert("777.168.7.1","8080","http");
        mySQLiteAdapter.insert("888.168.8.1","8080","http");
        mySQLiteAdapter.insert("999.168.9.1","8080","http");
        mySQLiteAdapter.insert("1010.168.10.1","8080","http");
        mySQLiteAdapter.insert("1111.168.11.1","8080","http");

        mySQLiteAdapter.close();

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

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

        String[] from = new String[]{SQLiteAdapter.KEY_ID,SQLiteAdapter.KEY_IP,SQLiteAdapter.KEY_PORT, SQLiteAdapter.KEY_TYPE};
        int[] to = new int[]{R.id.text,R.id.text2,R.id.text3,R.id.text4};

        SimpleCursorAdapter cursorAdapter =
            new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

        listContent.setAdapter(cursorAdapter);

        listContent.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                selectData(position);
            }

            public void selectData(int position) {
                try{
                    mySQLiteAdapter.openToRead();
                    Cursor c = mySQLiteAdapter.sqLiteDatabase.rawQuery("SELECT * FROM Proxy WHERE _id = '"+position+"'", null);
                    while(c.moveToNext()){

                        String id = c.getString(c.getColumnIndex("_id"));
                        String proxy = c.getString(c.getColumnIndex("proxy"));
                        String port = c.getString(c.getColumnIndex("port"));
                        String type = c.getString(c.getColumnIndex("type"));

                        Context context = getApplicationContext();
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context,id+proxy+port+type, duration);
                        toast.show();
                    }
                    }catch(Exception erro){
                    //
                        Context context = getApplicationContext();
                        CharSequence text = "Erro";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }finally{
                        mySQLiteAdapter.close();    
                    //
                    }
            }
        });
       }  
    }

日志:

E/CursorWindow(585): Bad request for field slot 0,-1. numRows = 1, numColumns = 4

2 个答案:

答案 0 :(得分:0)

在开始操作光标之前,需要调用cursor.moveToFirst()。如果调用返回false,则光标为空。

答案 1 :(得分:0)

是的,您需要更换

while(c.moveToNext()){
  ....
  ....
}

用这个

if (cur.moveToFirst()) {
                         do {
                              ....
                              ....
                             } while(c.moveToNext())
                       }

希望它有所帮助!!