从联机json文件接收列表视图中的新数据后发送通知?

ddhy6vgd  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(224)

我的应用程序中有一个列表视图。我通过http请求从json文件中获取数据。我想通过json文件在列表视图中添加新数据时显示一个通知
这是我的密码。在这段代码中,我只从json获取数据并显示在列表视图中
一个类似的问题已经发布了链接,但没有人给出满意的答案我知道关于android通知,但我想知道如何显示通知时,新的数据添加到列表视图后的json文件更新
我想要的解决方案是:当新数据到达时,我的应用程序会发出推送通知,然后用户单击通知并读取新内容

public class MainActivity extends AppCompatActivity {
   ListView textViewResult;
   private JsonPlaceHolder jsonPlaceHolder;
   List<String> Title = new ArrayList<String>();
   List<String> pageID = new ArrayList<String>();
   ArrayAdapter<String> arrayAdapter;
   ProgressDialog progressDialog;
   public   int parentid;
   private String menu;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textViewResult = findViewById(R.id.mylist);
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("please wait");

  parentid = getIntent().getIntExtra("parentId",1);
  menu = getIntent().getStringExtra("menu");

    int cacheSize = 10 * 1024 * 1024; // 10 MB
    Cache cache = new Cache(getCacheDir(), cacheSize);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .cache(cache)
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://www.codeelife.com/wp-json/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    jsonPlaceHolder = retrofit.create(JsonPlaceHolder.class);

     getMenuItems();

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            Title );

    textViewResult.setAdapter(arrayAdapter);

}

private void getPage(int id) {

        Call<Page> call = jsonPlaceHolder.getComments(id);
        call.enqueue(new Callback<Page>()
        {
            @Override
            public void onResponse(Call<Page> call, Response<Page> response) {

                if (!response.isSuccessful()) {
                    Toast.makeText(MainActivity.this, "code: "+response.code(), Toast.LENGTH_SHORT).show();
                    return;
                }

                Page page = response.body();
                String content = page.getContent().getRendered();

                Intent intent = new Intent(getApplicationContext(), Web.class);
                intent.putExtra("webcontent", content);
                startActivity(intent);
                progressDialog.dismiss();
            }

            @Override
            public void onFailure(Call<Page> call, Throwable t) {
                // textViewResult.setText(t.getMessage());

            }
        });

}

private void getMenuItems() {
    Call<Model> call = jsonPlaceHolder.getItem(menu);
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            if (!response.isSuccessful()){
               // textViewResult.setText("code: "+response.code());

                return;
            }

            Model models = response.body();

            for (Item item: models.getItems()) {
                pageID.add(item.getObjectId());
            }
            for (Item item: models.getItems()) {
                Title.add(item.getTitle());
            }

            list((ArrayList<String>) Title);

        }

        @Override
        public void onFailure(Call<Model> call, Throwable t) {
           // textViewResult.setText(t.getMessage());

        }
    });

}

private void list( ArrayList<String>  ss) {
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            ss );

    textViewResult.setAdapter(arrayAdapter);
    textViewResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            progressDialog.show();
            String value=pageID.get(position);
            int pageid=Integer.parseInt(value);
            getPage(pageid);
        }
    });
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题