ListView中未显示Android JSON数据(使用Realm)

时间:2017-07-04 07:09:32

标签: android json listview parsing realm

我试图从服务器获取数据通知JSON并将数据存储到我的离线数据库(在本例中为Realm),每当我尝试检索数据时,listview中都不会显示任何内容。

    public class MainActivity extends AppCompatActivity {


    public static ListView myList;
    public static ListAdapter myAdapter;
    public static Realm realm;


    @Override
    protected  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Realm.init(this);
    realm = Realm.getDefaultInstance();
            realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Recipe> dRecipies = realm.where(Recipe.class).findAll();
        if(dRecipies!= null)dRecipies.deleteAllFromRealm();

    }
});
        DownloadTask newTask = new DownloadTask();
        newTask.execute("hi");
        setContentView(R.layout.activity_main);
        myList = (ListView) findViewById(R.id.Recipe_list);

       // getData();

        setDisplay();

        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String p = String.valueOf(position);
                Intent in = new Intent(MainActivity.this, SecondScreenDetails.class);
                in.putExtra("Position", p);
                startActivity(in);
            }
        });

    }

    public void setDisplay(){

        ArrayList<Recipe> finalRecipies = new ArrayList<>();
        RealmResults<Recipe> rrRecipies = realm.where(Recipe.class).findAll();

        for(Recipe r: rrRecipies){
            finalRecipies.add(r);
            Toast.makeText(this, r.getName(), Toast.LENGTH_SHORT).show();
        }
        myAdapter = new ListViewAdapter(this, finalRecipies);
        myList.setAdapter(myAdapter);

    }


    @Override
    protected void onDestroy() {
        realm.close();
        super.onDestroy();
    }
}

我这样做是因为,如果我不这样做,数据会不断重复存储,导致重复。

        realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Recipe> dRecipies = realm.where(Recipe.class).findAll();
        if(dRecipies!= null)dRecipies.deleteAllFromRealm();

    }
});

当我尝试没有实际删除数据,然后在Toast中 setDisplay()方法正常工作并且正在显示数据。(第二次打开应用程序时,重复播放Toast,它会获得两次......等等) 当我插入它时,即使是吐司也不会出现。

我的下载活动

    public class DownloadTask extends AsyncTask<String,Void,String> {


    private RealmList<Recipe> realmRecipe = new RealmList<>();
    String result;

    @Override
    protected String doInBackground(String... params) {
        result = "";
        Realm realm = null;
            realm = Realm.getDefaultInstance();
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url("https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json").build();
                    try {
                        result = client.newCall(request).execute().body().string();
                        Log.i("RESULT", result);
                        JSONArray rootArray = new JSONArray(result);
                        for (int i = 0; i < rootArray.length(); i++) {
                            JSONObject tempObject = rootArray.getJSONObject(i);
                            JSONArray jIngredients = tempObject.getJSONArray("ingredients");
                            JSONArray jSteps = tempObject.getJSONArray("steps");

                            // Get the ingredients
                            List<Ingredients> ingredients = new ArrayList<>();
                            for (int j = 0; j < jIngredients.length(); j++) {
                                JSONObject tempIngredient = jIngredients.getJSONObject(j);
                                Ingredients nIngredient = realm.createObject(Ingredients.class);
                                nIngredient.setIngredient(tempIngredient.getString("ingredient"));
                                nIngredient.setMeasure(tempIngredient.getString("measure"));
                                nIngredient.setQuantity(tempIngredient.getString("quantity"));
//                                Ingredients newIngredient = new Ingredients(tempIngredient.getString("quantity"),
//                                        tempIngredient.getString("measure"),
//                                        tempIngredient.getString("ingredient"));
//                                ingredients.add(newIngredient);
                                ingredients.add(nIngredient);
                            }

                            // Get the steps
                            List<Steps> steps = new ArrayList<>();
                            for (int j = 0; j < jSteps.length(); j++) {
                                JSONObject tempStep = jSteps.getJSONObject(j);
                                Steps nStep = realm.createObject(Steps.class);
                                nStep.setDescription(tempStep.getString("description"));
                                nStep.setId(tempStep.getString("id"));
                                nStep.setShortDescription(tempStep.getString("shortDescription"));
                                nStep.setVideoURL(tempStep.getString("videoURL"));
                                steps.add(nStep);
//                                Steps newStep = new Steps(tempStep.getString("id"), tempStep.getString("shortDescription"),
//                                        tempStep.getString("description"), tempStep.getString("videoURL"));
//                                steps.add(newStep);
                            }

                            // Create the recipe

                            Recipe nRecipe = realm.createObject(Recipe.class);
                            nRecipe.setId(tempObject.getString("id"));
                            nRecipe.setName(tempObject.getString("name"));
                            nRecipe.setServings(tempObject.getString("servings"));
                            nRecipe.setIngredients(ingredients);
                            nRecipe.setSteps(steps);
                            realmRecipe.add(nRecipe);
//                            Recipe newRecipe = new Recipe(tempObject.getString("id"), tempObject.getString("name"), tempObject.getString("servings"), ingredients, steps);
//                            MainActivity.mRecipies.add(newRecipe);

                        }
                    }catch (Exception e){
                        Log.i("Error Message", e.getMessage());
                    }

                }
            });


        return null;
    }

    @Override
    protected void onPostExecute(String s) {

            super.onPostExecute(s);
        }
    }

0 个答案:

没有答案