如何在Android的listview中的两个sqlite表行填充listview?

时间:2015-07-30 09:58:54

标签: android android-sqlite

我必须检索JSON字符串并从两个不同的SQLite表中逐个更新,插入和检索记录。但是如何通过连接然后逐个添加来从列表视图中的两个不同表中显示检索到的数据list-view。有人建议我怎么做?

这是我的代码

 private class GetAllActivity extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog

                pDialog = new ProgressDialog(AllPosts_Page.this);
                pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pDialog.setMessage("Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected Void doInBackground(Void... arg0)
            {
                getAllActivityList();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                    pDialog.dismiss();
            }
        }

        @Override
        public void onDestroy(){
            super.onDestroy();
            if ( pDialog!=null && pDialog.isShowing() )
            {
                pDialog.cancel();

                ListView listView = (ListView)findViewById(R.id.listview_AllPost);
                MyListAdapter adapter = new       
                MyListAdapter(this,R.layout.all_post_row,str);
                listView.setAdapter(adapter);
            }
        }


     public void getAllActivityList()
        {
            String strUrl_GetAll_Activity = "http://xxxx/xxx/xxx.svc/xxx/Sync_Time/null/xxx/xxxx-xxxx";
            Log.e("strUrl_GetAll_Activity ", " = " + strUrl_GetAll_Activity);

            InputStream inputStream = null;

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse httpResponse = httpclient.execute(new HttpGet(strUrl_GetAll_Activity));
                inputStream = httpResponse.getEntity().getContent();

                if(inputStream != null)
                    strResult = convertInputStreamToString(inputStream);
                else
                    strResult = "Did not work!";

                String jsonStr = strResult;
                Log.e("Get_Vessels_By_Type  "," = " + jsonStr);

                if(jsonStr != null)
                {

                    try
                    {
                        JSONObject jsonObj = new JSONObject(jsonStr);
                        String jsonResult = jsonObj.toString().trim();
                        Log.e("jsonResult ", " = " + jsonResult);

                        JSONObject getAllActivity_List = jsonObj.getJSONObject("Get_ALL_ActivityListResult");
                        Log.e("getAllActivity_List ", " = " + getAllActivity_List.toString());

                        String strSync = getAllActivity_List.getString("Sync_Time");
                        Log.e("strSync ", " = " + strSync.toString());

                        JSONArray jarr = getAllActivity_List.getJSONArray("ActivityList");
                        Log.e("jarr ", " = " + jarr.toString());

                        for (int j = 0; j < jarr.length(); j++)
                        {
                            JSONObject jobjVessels = jarr.getJSONObject(j);

                            strActivityId = jobjVessels.getString("activityId");
                            strRemark = jobjVessels.getString("remark");
                            strName = jobjVessels.getString("name");
                            strStatus = jobjVessels.getString("status");
                            strType = jobjVessels.getString("type");
                            strUserId = jobjVessels.getString("userId");
                            strObjectId = jobjVessels.getString("objectId");
                            strActiondate = jobjVessels.getString("actionDate");
                            strStarFlag= jobjVessels.getString("StarFlag");




                            if(dbhelper.isAllPostId_Exist(strActivityId))
                            {
                                //Upadte
                                Log.e("*******************", "*******************");
                                dbhelper.update_AllPost(strActivityId, strRemark, strName, strStatus, strType, strUserId, strObjectId, strActiondate, strStarFlag);
                                Log.e("Data updated in ", "Company Table !!");
                                Log.e("*******************", "*******************");
                            }
                            else
                            {

                                Log.e("*******************", "*******************");
                                dbhelper.insert_AllPost(strActivityId, strRemark, strName, strStatus, strType, strUserId, strObjectId, strActiondate, strStarFlag);
                                Log.e("Dats save in ", "AllPost Succesively !!!!");
                                Log.e("*******************", "*******************");

                            }

                            Log.e("*******After Else******", "*******************");
                            getInspectorName(strUserId);

                            Log.e("*******************", "*******************");
                            getVesselName(strObjectId);
                        }

                    }
                    catch(JSONException je)
                    {
                    je.printStackTrace();}
                }

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

此处获取特定行

public String getInspectorName(String strInspectorId)
        {
            Cursor cursor = null;

            SQLiteDatabase mDb = dbhelper.getWritableDatabase();
            try{

                cursor = mDb.rawQuery("select Inspector_name from Inspector where Inspector_Id=?", new String[] {strInspectorId + ""});

                if(cursor.getCount() > 0) {

                    cursor.moveToFirst();
                    inspectorName = cursor.getString(cursor.getColumnIndex("Inspector_name"));

                    Log.e(" inspectorName "," = " + inspectorName);
                }

                return inspectorName;
            }finally {

                cursor.close();
            }

        }


        public String getVesselName(String strVesselIdId)
        {
            Cursor cursor = null;

            SQLiteDatabase mDb = dbhelper.getWritableDatabase();
            try{

                cursor = mDb.rawQuery("select Vessel_Name from VesselList where Vessel_ID=?", new String[] {strVesselIdId + ""});

                if(cursor.getCount() > 0) {

                    cursor.moveToFirst();
                    inVesselName = cursor.getString(cursor.getColumnIndex("Vessel_Name"));
                    Log.e(" inVesselName "," = " + inVesselName);
                }

                return inVesselName;
            }finally {

                cursor.close();
            }

        }

1 个答案:

答案 0 :(得分:0)

您可以使用Join从两个表中获取数据

注意:对于应用加入,您必须为两个表都有一些共同的ID。