【问题标题】:How to automatically refresh a Listview? [duplicate]如何自动刷新列表视图? [复制]
【发布时间】:2018-04-23 10:58:51
【问题描述】:

我有一个 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);

谢谢。

【问题讨论】:

  • 这对我不起作用,我不知道如何插入此代码

标签: java android json push-notification


【解决方案1】:

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

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

                (OR)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多