我試圖通過使用SQL語句和JTDS驅動程序,通過的AsyncTask顯示在Android上的一個ListView行填充。的ListView在Android上 - 有一個ArrayList
Articles.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
誰能告訴我是怎麼回事?
UPDATE:填充ArrayList中
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);
}
您需要在'Articles'類中重寫'toString()'。 – 2013-04-22 18:54:02
@代碼大師如何? – Brian 2013-04-22 18:54:44
您需要將列表轉換爲字符串數組 – Zabador 2013-04-22 18:55:23