我試圖使用swipeRefreshLayout更新數據,但每次向下滑動時刷新它都只是盤旋而不是刷新。使用swipeRefreshLayout更新列表視圖中的數據
你能幫我解決這個問題嗎?
package com.reader.ashishyadav271.hackernewsreader;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity {
SwipeRefreshLayout mSwipeRefreshLayout;
Map<Integer, String> articleURLs = new HashMap<>();
Map<Integer, String> articleTitles = new HashMap<>();
ArrayList<Integer> articleIds = new ArrayList<>();
SQLiteDatabase articlesDB;
ArrayList<String> titles = new ArrayList<>();
ArrayAdapter arrayAdapter;
ArrayList<String> urls = new ArrayList<>();
ArrayList<String> content = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefreshLayout.setColorSchemeResources(R.color.red, R.color.green, R.color.blue, R.color.yellow);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshContent();
}
});
ListView listView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, titles);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), Main2Activity.class);
i.putExtra("articleUrl", urls.get(position));
i.putExtra("content", content.get(position));
startActivity(i);
}
});
articlesDB = this.openOrCreateDatabase("Articles", MODE_PRIVATE, null);
articlesDB.execSQL("CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY, articleId INTEGER, url VARCHAR, title VARCHAR, content VARCHAR)");
updateListView();
DownloadTask task = new DownloadTask();
try {
task.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");
} catch (Exception e) {
e.printStackTrace();
}
}
private void refreshContent() {
arrayAdapter.notifyDataSetChanged();
titles.clear();
updateListView();
mSwipeRefreshLayout.setRefreshing(false);
}
public void updateListView() {
try {
Log.i("UI UPDATED", "DONE");
Cursor c = articlesDB.rawQuery("SELECT * FROM articles", null);
int contentIndex = c.getColumnIndex("content");
int urlIndex = c.getColumnIndex("url");
int titleIndex = c.getColumnIndex("title");
c.moveToFirst();
titles.clear();
urls.clear();
while (c != null) {
titles.add(c.getString(titleIndex));
urls.add(c.getString(urlIndex));
content.add(c.getString(contentIndex));
c.moveToNext();
}
arrayAdapter.notifyDataSetChanged();
}catch (Exception e) {
e.printStackTrace();
}
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
JSONArray jsonArray = new JSONArray(result);
articlesDB.execSQL("DELETE FROM articles");
for (int i = 0; i < 20; i++) {
String articleId = jsonArray.getString(i);
url = new URL("https://hacker-news.firebaseio.com/v0/item/" + articleId + ".json?print=pretty");
urlConnection = (HttpURLConnection) url.openConnection();
in = urlConnection.getInputStream();
reader = new InputStreamReader(in);
data = reader.read();
String articleInfo = "";
while (data != -1) {
char current = (char) data;
articleInfo += current;
data = reader.read();
}
JSONObject jsonObject = new JSONObject(articleInfo);
String articleTitle = jsonObject.getString("title");
String articleURL = jsonObject.getString("url");
String articleContent = "";
/*
url = new URL(articleURL);
urlConnection = (HttpURLConnection) url.openConnection();
in = urlConnection.getInputStream();
reader = new InputStreamReader(in);
data = reader.read();
while (data != -1) {
char current = (char) data;
articleInfo += current;
data = reader.read();
}
*/
articleIds.add(Integer.valueOf(articleId));
articleTitles.put(Integer.valueOf(articleId), articleTitle);
articleURLs.put(Integer.valueOf(articleId), articleURL);
String sql = "INSERT INTO articles (articleId, url, title, content) VALUES (? , ? , ? , ?)";
SQLiteStatement statement = articlesDB.compileStatement(sql);
statement.bindString(1, articleId);
statement.bindString(2, articleURL);
statement.bindString(3, articleTitle);
statement.bindString(4, articleContent);
statement.execute();
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
refreshContent();
}
}
}
您不關閉在烏拉圭回合編碼即最大的錯誤之一光標連接,嘗試調試表是否真的有在數據庫中的數據的代碼。打印日誌並檢查是否存在數據 –
數據來自哪裏? –
來自黑客新聞json api ... –