在android中使用光标对象

在android中使用光标对象,android,android-cursoradapter,Android,Android Cursoradapter,SplashActivity.java{Updated} public class SplashActivity extends Activity { /** Called when the activity is first created. */ JSONObject jsonobject; JSONArray jsonarray; ArrayList<HashMap<String, String>> arraylist;

SplashActivity.java{Updated}

public class SplashActivity extends Activity {
    /** Called when the activity is first created. */

    JSONObject jsonobject;  
    JSONArray jsonarray;
    ArrayList<HashMap<String, String>> arraylist;
    private String Content;
    DatabaseAdapter db;
    TextView txtSplashTitle,txtSplashDesc;
    DatabaseAdapter databaseHelper;
    Cursor cursor;


    //@InjectView(R.id.txtSplashDesc) TextView txtSplashDesc=null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        //ButterKnife.inject(this);//using ButterKnife library for viewInjection
        txtSplashDesc=(TextView) findViewById(R.id.txtSplashDesc);

        String serverURL = "";
        db = new DatabaseAdapter(this);
        new LongOperation().execute(serverURL);


        freeMemory();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        //Setting fonts for textviews
        setCustomFontForTextViews();

    }



    private void setCustomFontForTextViews() {
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "royalacid.ttf");
        txtSplashDesc.setTypeface(typeFace);
    }



    // Class with extends AsyncTask class
    private class LongOperation  extends AsyncTask<String, Void, Void> {

        private final HttpClient Client = new DefaultHttpClient();
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(SplashActivity.this);


        protected void onPreExecute() {
            // NOTE: You can call UI Element here.
            Dialog.setMessage("Downloading source..");
            Dialog.show();
        }

        // Call after onPreExecute method
        protected Void doInBackground(String... urls) {
            try {
                // NOTE: Don't call UI Element here.
                HttpGet httpget = new HttpGet("http://10.0.2.2:3009/findmybuffet/?storedproc=get_app_tables&flag=sudhakar");
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                Content = Client.execute(httpget, responseHandler);
                jsonobject = new JSONObject(Content);
                jsonobject = jsonobject.getJSONObject("findmybuffet");
                jsonarray = jsonobject.getJSONArray("buffets");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("buf_off_id", jsonobject.getString("buf_off_id"));
                    map.put("from_time", jsonobject.getString("from_time"));
                    map.put("to_time", jsonobject.getString("to_time"));
                    map.put("online_price", jsonobject.getString("online_price"));
                    map.put("reserved_price", jsonobject.getString("reserved_price"));
                    map.put("buf_image", jsonobject.getString("buf_image"));
                    map.put("res_name", jsonobject.getString("res_name"));
                    map.put("rating", jsonobject.getString("rating"));
                    map.put("latitude", jsonobject.getString("latitude"));
                    map.put("longitude", jsonobject.getString("longitude"));
                    map.put("buf_type_name", jsonobject.getString("buf_type_name"));
                    map.put("from_date", jsonobject.getString("from_date"));
                    map.put("to_date", jsonobject.getString("to_date"));
                    map.put("city_id", jsonobject.getString("city_id"));
                    map.put("city_name", jsonobject.getString("city_name"));
                    map.put("meal_type_id", jsonobject.getString("meal_type_id"));
                    map.put("meal_type_name", jsonobject.getString("meal_type_name"));
                    map.put("buf_desc", jsonobject.getString("buf_desc"));
                    map.put("distance", jsonobject.getString("distance"));

                    Log.d("----$$$----", map.toString());

                    //Calling database 
                    db.addContact(map);

                    try {
                        Cursor cursor = (Cursor) databaseHelper.getAllContacts();
                        cursor.moveToFirst();
                        if(cursor.moveToFirst()){
                            do{
                                String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7)));
                                Log.d("---@*@*@*@*@*@----", refDestLatitude+"");
                            }while(cursor.moveToNext());
                        }

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Log.d("ThrownException", e.toString());
                        e.printStackTrace();
                    }

                    //cursor.close();
                }   
                // Reading all contacts
                Log.d("Reading: ", "Reading all contacts.."); 
            } catch (IOException|JSONException e) {
                Error = e.getMessage();
                cancel(true);
            }
            return null;
        }

        protected void onPostExecute(Void unused) {     
            // Close progress dialog
            Dialog.dismiss();   
            Intent intent=new Intent(SplashActivity.this,MainActivitySherlock.class);
            startActivity(intent);
        }
    }

    private void freeMemory() {
        jsonobject=null;
        jsonarray=null;
        arraylist=null;
        Content=null;
    }

}
光标能够获取值

cursor.getColumnIndex(cursor.getColumnName(7))
cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)));
但异常在

cursor.getColumnIndex(cursor.getColumnName(7))
cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)));
评估

注意::当我在适配器中处理时,该行正在工作。。。。。但它在这里不起作用。我需要投推荐信还是什么

getString(cursor.getColumnIndex(cursor.getColumnName(7))

由于没有第7列,因此出现错误

我不得不问,当你可以从专栏中获取数据时,为什么会有这么多戏剧性的事情

if (getColumnCount() > 11) {  // 4+7 = 11 fail
        cursor.getString(7);
}
试着这样做:

if(c.moveToFirst()){
do{
String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7)));
}while(c.moveToNext())
}

您将获得什么异常..?或使用try catch块绕过该行,然后在调用cursor.getString()owk..之前选中exceptionadd cursor.moveToFirst()。因此..cursor中有一个集合,它不知道从该值中获取该集合的位置。试着这样做:如果(c.moveToFirst()){do{String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7));}而(c.moveToNext())}我正在发布答案。检查that@wqrahd... 请看更新的问题。。。我已经更新了SplashActivity.java&LOgall,似乎没有问题。但问题出在哪里。:p..在getAllContacts()中读取游标之前,请检查您的代码是否正在关闭数据库。我甚至发布了DatabaseAdapter.java的完整代码。。。。。我还没有关闭该类中的DB连接…那么为什么会发生此异常我认为问题在于您正在执行freeMemory()调用asyntask.comment后,立即在onPostExecute中调用或添加它,然后再试一次。该方法将对象设置为null。问题可能在于。太夸张了为什么不从列中获取数据?cursor.getString(columnIndex);