0

首先,我为可能的语法错误道歉,我是巴西人。
我正在尝试获取listview变量中的值,但是当我单击_id > 1 on时出现错误提示listview

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

图片:

图片


代理列表.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
4

2 回答 2

0

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

于 2012-06-18T03:00:10.027 回答
0

是的,您需要更换

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

有了这个

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

希望能帮助到你 !!

于 2012-06-18T06:13:25.367 回答