在android中使用游标对象

时间:2014-04-22 04:58:04

标签: 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;
    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;
    }

}

当我调试应用程序时,我发现如下 我遇到问题 ::

String refDestLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(7)));

光标可以获取值

cursor.getColumnIndex(cursor.getColumnName(7))

时会弹出异常
cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4)));
评估

注意 ::当我在适配器中处理时这行正常工作.....但它在这里不起作用。我需要投射参考文件吗?

2 个答案:

答案 0 :(得分:1)

  

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

您收到错误,因为没有第7列。

当你可以从列中获取数据时,我不得不问为什么所有的戏剧?

if (getColumnCount() > 11) {  // 4+7 = 11 fail
        cursor.getString(7);
}

答案 1 :(得分:1)

尝试这样:

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