java - 如何自动刷新Listview?

标签 java android json push-notification

我有一个 android ListView ,但每次我想刷新列表时都必须重新调整应用程序。我有两个问题请教:

1)如何自动刷新?

2)如何在将项目添加到数据库时收到通知?

所以它只是测试是否添加了一个项目,我收到一条通知,当我单击它时,我将能够看到项目列表。

这是我的代码:

public class MainActivity extends Activity {

    ListView SubjectFullFormListView;
    ProgressBar progressBar;
    String HttpURL = "http://254.221.325.11/test/Subject.php";
    ListAdapter adapter ;
    List<Subject> SubjectFullFormList;
    EditText editText ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        SubjectFullFormListView = (ListView) findViewById(R.id.SubjectFullFormListView);

        editText = (EditText)findViewById(R.id.edittext1);

        progressBar = (ProgressBar) findViewById(R.id.ProgressBar1);

        new ParseJSonDataClass(this).execute();


    }

    private class ParseJSonDataClass extends AsyncTask<Void, Void, Void> {
        public Context context;
        String FinalJSonResult;

        public ParseJSonDataClass(Context context) {

            this.context = context;
        }

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }



        @Override
        protected Void doInBackground(Void... arg0) {

            HttpServiceClass httpServiceClass = new HttpServiceClass(HttpURL);

            try {
                httpServiceClass.ExecutePostRequest();

                if (httpServiceClass.getResponseCode() == 200) {

                    FinalJSonResult = httpServiceClass.getResponse();

                    if (FinalJSonResult != null) {

                        JSONArray jsonArray = null;
                        try {

                            jsonArray = new JSONArray(FinalJSonResult);
                            JSONObject jsonObject;
                            Subject subject;

                            SubjectFullFormList = new ArrayList<Subject>();

                            for (int i = 0; i < jsonArray.length(); i++) {

                                subject = new Subject();

                                jsonObject = jsonArray.getJSONObject(i);

                                subject.Subject_Name = jsonObject.getString("SubjectName");

                                subject.Subject_Full_Form = jsonObject.getString("SubjectFullForm");

                                SubjectFullFormList.add(subject);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                } else {

                    Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result)

        {
            progressBar.setVisibility(View.GONE);

            SubjectFullFormListView.setVisibility(View.VISIBLE);

                adapter = new ListAdapter(SubjectFullFormList, context);

                SubjectFullFormListView.setAdapter(adapter);

        }
    }

}

我找到了解决方案,但我不知道如何以及在哪里插入它:

final Handler handler = new Handler();

Runnable refresh = new Runnable() {
        @Override
        public void run() {
            new JSONParse().execute();  
            handler.postDelayed(this, 60 * 1000);
        }
    };

handler.postDelayed(refresh, 60 * 1000);

谢谢。

最佳答案

-> 编写一个可运行线程,在一段时间后定期调用列表的 API,并根据其响应更改适配器中的列表并调用 notifydatasetchanged()。

-> 通过使用 FCM,您可以获得问题 2 的解决方案。当在数据库中添加项目时,从服务器端生成通知并将其发送到客户端,通过使用挂起 Intent ,您可以在用户点击通知时显示列表。

                (OR)

-> 您可以使用实时数据库(例如 Firebase DB 或 Realm 等)来实现此方法。当项目添加到列表中时,此数据库会通知您,并且您不需要线程来刷新列表。

关于java - 如何自动刷新Listview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49979449/

相关文章:

java - 有没有办法更改上下文操作栏的背景颜色?

java - 如何在Java中使用DriverManager实现连接

android - 我不明白为什么我的按钮没有出现在我的 Android VM 上?

android - Intent.ACTION_REBOOT 和 Intent.ACTION_SHUTDOWN 之间的区别

json - 使用 ARM 模板为容器部署 Web 应用程序

java - 使用字节流写入文件

java - 使用 Spring @SubscribeMapping 获取当前用户

android - 将 5mb 数据文件复制到数据库后,如何清除 android 中的堆内存?

javascript - 如何返回自定义表单字段的值

MySQL 未返回有效的 Json