Android - 自动向ListView添加项目

时间:2012-05-12 03:02:55

标签: android android-listview

下面的代码当前从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;
    }
}

1 个答案:

答案 0 :(得分:0)

我建议在解析XML对象后将所有内容存储在本地sql数据库中。这样,您可以在内容提供程序上使用LoaderManager,并在将新内容添加到数据库时异步更新。

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