我正在尝试通过 AsyncTask 使用 SQL 语句和 jTDS 驱动程序在 Android 上的 ListView 中显示行。
文章.java:
package com.example.projectmanager;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import android.os.AsyncTask;
import android.util.Log;
public class Articles extends AsyncTask <List<Articles>, Void, List> {
int article_id;
String title;
String body ;
Date date;
String username;
List<Articles> posts = new ArrayList<Articles>();
protected List<Articles> doInBackground(List... params) {
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
String connString = "jdbc:jtds:sqlserver://10.0.2.2/master_db;";
String sqlusername = "admin";
String sqlpassword = "root";
conn = DriverManager.getConnection(connString, sqlusername, sqlpassword);
Log.w("Connection","open");
String articleQuery = "SELECT TOP 5 E.article_id,E.article_title,E.article_description,E.article_date,u.username FROM articles AS E INNER JOIN user_articles as A ON A.article_id = E.article_id INNER JOIN users as u ON A.user_id = u.user_id WHERE E.article_status = 1;";
PreparedStatement stmt = conn.prepareStatement(articleQuery);
ResultSet rs;
rs = stmt.executeQuery();
while (rs.next()) {
Articles article = new Articles();
article.article_id = rs.getInt("article_id");
article.username = rs.getString("username");
article.date = rs.getDate("article_date");
article.title = rs.getString("article_title");
article.body = rs.getString("article_description");
posts.add(article);
}
conn.close();
} catch (Exception e)
{
Log.w(e.getMessage(), e);
}
return posts;
}
protected void onPostExecute(List posts) {
// Result is here now, may be 6 different List type.
this.posts = posts;
}
}
HomeActivity.java:
package com.example.projectmanager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class HomeActivity extends Activity {
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Articles a = new Articles();
ListView lv = (ListView) findViewById(R.id.lvPosts);
List postsList = null;
try {
postsList = new Articles().execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, postsList);
lv.setAdapter(arrayAdapter);
}
}
每当我尝试运行代码时,我都会得到以下输出:
LogCat: http: //pastebin.com/cjm1BNeZ
谁能告诉我发生了什么?
更新:填充数组列表
String articleQuery = "SELECT TOP 5 E.article_id,E.article_title,E.article_description,E.article_date,u.username FROM articles AS E INNER JOIN user_articles as A ON A.article_id = E.article_id INNER JOIN users as u ON A.user_id = u.user_id WHERE E.article_status = 1;";
PreparedStatement stmt = conn.prepareStatement(articleQuery);
ResultSet rs;
rs = stmt.executeQuery();
while (rs.next()) {
Articles article = new Articles();
article.article_id = rs.getInt("article_id");
article.username = rs.getString("username");
article.date = rs.getDate("article_date");
article.title = rs.getString("article_title");
article.body = rs.getString("article_description");
posts.add(article);
}