【发布时间】:2013-12-17 20:29:22
【问题描述】:
我有一个 android 应用程序,它从我的网络服务器获取数据,然后将它们显示在我的列表视图上并保存到我的本地数据库 (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();
无济于事。它不会更新列表。如果我关闭应用程序并再次打开它,它只是在更新。任何想法如何做到这一点?非常感谢您的帮助。谢谢。
【问题讨论】:
-
你需要再次调用
DownloadJSONAsyncTask。 -
什么时候获取数据?一种简单的方法是使用后台任务定期获取数据并在有新数据时更新列表..
-
@AmulyaKhare 哦,谢谢,我从来没想过,但我的问题是如何实现定期提取?
-
@Tamilan 我应该把 DownloadJSON asynctask 放在哪里?
-
@Dunkey On button 单击或在哪里使用刷新适配器时必须调用它。
标签: android mysql facebook sqlite listview