我有一个Android应用程序,它从我的Web服务器获取数据,然后在我的listview上显示它们并保存到我的本地数据库(sqlite)。如果我将另一条记录添加到我的网络服务器,应用程序应刷新/更新我的列表视图,就像Facebook应用程序,如果有新帖子,然后显示这些帖子。
这是我到目前为止所做的:
ListViewAdapter adapter;
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Downloading Records");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given website URL in JSONfunctions.class
String result = JSONFunctions.getJSONfromURL(URL);
try {
JSONArray jr = new JSONArray(result);
for(int i=0;i<jr.length();i++)
{
HashMap<String, String> map = new HashMap<String, String>();
jb = (JSONObject)jr.get(i);
map.put(TAG_ID, jb.getString(TAG_ID));
map.put(TAG_NAME, jb.getString(TAG_NAME));
arraylist.add(map);
String strID = jb.getString(TAG_NAME);
dbHelper.createEntry(strID);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listView1);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
//TextView tv = (TextView) findViewById (R.id.textView1);
//tv.setText("Successfully processed");
// Close the progressdialog
mProgressDialog.dismiss();
}
}
我的 listviewadapter.class 的片段:
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView id;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_item, parent, false);
// Get the position from the results
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = data.get(position);
// Locate the TextViews in list_item.xml
id = (TextView) itemView.findViewById(R.id.listitem_id);
// Capture position and set results to the TextViews
id.setText(resultp.get(MainActivity.TAG_NAME));
// Capture position and set results to the ImageView
// Passes movie images URL into ImageLoader.class to download and cache
// images
// Capture button clicks on ListView items
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get the position from the results
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = data.get(position);
// Send single item click data to SingleMenuItemView Class
Intent intent = new Intent(context, SingleMenuItemActivity.class);
// Pass data
intent.putExtra("id", resultp.get(MainActivity.TAG_ID));
intent.putExtra("name", resultp.get(MainActivity.TAG_NAME));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
我也添加了这个:
adapter.notifyDataSetChanged();
无济于事。它没有更新列表。如果我关闭应用程序并再次打开它,它只是更新。任何想法如何做到这一点?非常感谢帮助。感谢。
答案 0 :(得分:1)
在这种情况下,您可以使用Google Cloud Messaging(GCM)服务。只要您的Web服务器发生任何更改,只需此服务器发出的推送通知即可。抓住它并相应地在您的应用程序中使用它。你可以看看here
答案 1 :(得分:1)
您应该安排DownloadJSON
任务定期运行,如下所示:
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
DownloadJSON performBackgroundTask = new DownloadJSON();
performBackgroundTask.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000); //execute in every 10 sec
}
这应该可行但不是最有效的方式,因为您一次又一次地请求整个数据。 (下载的数据很多,不需要)。
相反,如果存在任何新更新,则应定期运行检查服务器的任务。如果服务器说是..那么它将获取完整列表..否则它会等待一段时间并再次请求新的更新。但为此,您需要修改服务器API(以便将来考虑这一点)
答案 2 :(得分:0)
您只需要将新适配器设置为listView ..
newsAdapter = new NewsListViewAdapter(getActivity(),
R.layout.custome_listview_news_item, newsItems);
listViewNews.setAdapter(newsAdapter);
我假设你有一个片段,你想要更新片段中的listview .. 所以只需在Fragment中创建一个方法来更新listview .. 当你有新的项目时..只需要在你的数据中请求这个方法.. 注意:您必须设置新适配器..