0

下面的代码当前从 mysql 数据库中提取数据并将其显示在 ListView 中。我要做的是找到一种方法让应用程序每分钟左右检查一次 mysql 数据库以检查是否有任何新条目,如果它发现一个不在当前 ListView 中的新条目 - 它会在新条目之前项到列表顶部。我已经阅读了关于 notifyDataSetChanged() 的一些内容,但我想我无法掌握它的实际工作原理或如何实现它。任何帮助都是适用的。谢谢!

public class Data extends ListActivity {
    private ArrayList<Feed> posts = new ArrayList<Feed>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new FeedTask().execute();
    }
    private class FeedTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressDialog;
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(Data.this,"", "Loading. Please wait...", true);
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://xxxx/livefeed/getdata.php");
                HttpResponse httpResponse = httpClient.execute(httpPost);
                String result = EntityUtils.toString(httpResponse.getEntity());
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    Feed feed = new Feed();
                    feed.content = json_data.getString("post");
                    feed.time = json_data.getString("post_time");
                    posts.add(feed);
                }
            } 
            catch (Exception e){
                Log.e("ERROR", "Error loading JSON", e);
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            setListAdapter(new FeedListAdaptor(Data.this, R.layout.feed, posts));
        }
    }
    private class FeedListAdaptor extends ArrayAdapter<Feed> {
        private ArrayList<Feed> posts;
        public FeedListAdaptor(Context context,
        int textViewResourceId,
        ArrayList<Feed> items) {
            super(context, textViewResourceId, items);
            this.posts = items;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.feed, null);
            }
            Feed o = posts.get(position);
            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView bt = (TextView) v.findViewById(R.id.bottomtext);
            tt.setText(o.content);
            bt.setText(o.time);
            return v;
        }
    }
    public class Feed {
        String content;
        String time;
    }
}
4

1 回答 1

0

我建议在解析 XML 对象后将所有内容存储在本地 sql 数据库中。这样你就可以在内容提供者上使用LoaderManager,当新的东西被添加到数据库时它会异步更新。

它可能会对您当前的代码进行一些重构,但它值得 IMO,因为它几乎是您正在寻找的

于 2012-05-12T05:08:17.870 回答