【发布时间】:2019-12-07 20:40:51
【问题描述】:
我正在尝试按照 Udemy“完整的 Android N 开发人员课程”实施新闻阅读器应用程序。使用了列表视图。
按照我正确遵循的说明,但在执行以下主要活动时,虽然需要更新带有标题的列表项,但在列表视图中没有显示任何内容。即使在 Android 监视器中也没有错误。
请提供任何有关查找问题的建议。
谢谢!
public class MainActivity extends AppCompatActivity {
ArrayList<String > titles = new ArrayList<>();
ArrayList<String> content = new ArrayList<>();
ArrayAdapter arrayAdapter;
SQLiteDatabase articleDB ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView );
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,titles);
listView.setAdapter(arrayAdapter);
articleDB = this.openOrCreateDatabase("articles",MODE_PRIVATE,null);
articleDB.execSQL("CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY, articleID INTEGER,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();
}
}
//update table
public void updateListView(){
Cursor c = articleDB.rawQuery("SELECT * FROM articles", null);
int contentIndex = c.getColumnIndex("content");
int titleIndex = c.getColumnIndex("title");
if(c.moveToFirst()){
titles.clear();
content.clear();
do{
titles.add(c.getString(titleIndex));
content.add(c.getString(contentIndex));
}while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
}
}
public class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
String result = "";
URL url;
HttpsURLConnection urlConnection = null;
try {
url = new URL (strings[0]);
urlConnection = (HttpsURLConnection) 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();
}
//Log.i("URLContent",result);
JSONArray jsonArray = new JSONArray(result);
int numberOfItems = 20;
if(jsonArray.length() <20){
numberOfItems = jsonArray.length();
}
//to clear the table before add data
articleDB.execSQL("DELETE FROM articles"); //will clear everything and add a new data
for (int i=0;i<numberOfItems;i++ ){
//Log.i("JSONItem",jsonArray.getString(i));
String articleId = jsonArray.getString(i);
url = new URL("https://hacker-news.firebaseio.com/v0/item/"+articleId+".json?print=pretty");
urlConnection = (HttpsURLConnection) 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();
}
//Log.i("ArticleInfo",articleInfo);
//separate title and URL
JSONObject jsonObject = new JSONObject(articleInfo);
if (!jsonObject.isNull("title") && !jsonObject.isNull("url")){
String articleTitle = jsonObject.getString("title");
String articleURL = jsonObject.getString("url");
//Log.i("info",articleTitle + articleURL);
url = new URL(articleURL);
urlConnection = (HttpsURLConnection) url.openConnection();
in = urlConnection.getInputStream();
reader = new InputStreamReader(in);
data = reader.read();
String articleContent = "";
while (data!= -1){
char current = (char) data;
articleContent += current;
data = reader.read();
}
//Log.i("articleContent",articleContent);
String sql = "INSERT INTO articles(articleID,title,content) VALUES(? , ? , ?)";
SQLiteStatement statement = articleDB.compileStatement(sql);
statement.bindString(1,articleId);
statement.bindString(2,articleTitle);
statement.bindString(3,articleContent);
statement.execute();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//run when the download task is completed
updateListView();
}
}
}
【问题讨论】:
-
尝试使用 Log 来测试连接,然后你的数据来确保它首先工作。
-
这里的逻辑是首先确保您收到正确的数据。第二确保数据与模型兼容并且您可以解析它。第三,它在列表中正确膨胀。所以记录所有这些步骤。但是有两个注意事项。制作两个字符串列表是不好的做法,最好制作一个对象 NEWS,例如其中包含字符串标题和字符串描述。其次是获取 JSON 的逻辑不好。改造后它可以短得多。如果您需要帮助重构,请告诉我
-
如果该课程学习了这种开发方式 - 将其扔进垃圾箱。尝试改造 + 回收器 + 常规型号 class= 更清洁/更快
-
@BorisRuzanov 感谢您的建议。我是新手,我会尝试。很抱歉我没有得到关于改造的信息:(也许我应该谷歌它。
标签: java android sqlite android-studio listview