2013-04-22 65 views
0

我試圖通過使用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); 


    } 


     } 

每當我試着運行代碼我得到這樣的輸出:

enter image description here

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);    


      } 
+0

您需要在'Articles'類中重寫'toString()'。 – 2013-04-22 18:54:02

+0

@代碼大師如何? – Brian 2013-04-22 18:54:44

+0

您需要將列表轉換爲字符串數組 – Zabador 2013-04-22 18:55:23

回答

2

添加到您的文章類:你希望你的用戶看到

 @Override 
     public String toString() { 
      return "id: "+article_id + "\n" 
        +"title : " + title 
        +"body: "+body ; 
     } 

或任何其他信息。
這是因爲Android的框架將調用YOUROBJECT.toString();自動渲染列表中的對象。這就是爲什麼你需要重寫這個方法。

UPDATE:

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"); 

這是你popluate你的文章對象的代碼。如果你想在你的項目列表中顯示這個對象的其他屬性。只需在toString()方法中添加該字段即可。 例如:

@Override 
     public String toString() { 
      return "id: "+article_id + "\n" 
        +"title : " + title 
        +"body: "+body+" date"+date +"username: "+username ; 
     } 
+0

謝謝!!工作! – Brian 2013-04-22 19:06:47

+0

以及如何才能從SQL查詢中輸出所有值? – Brian 2013-04-22 19:14:30

+0

@Brian更新。 – wtsang02 2013-04-22 19:30:06

1

我想你使用ArrayAdapter錯了。 您放入的字符串(String)不是您應該放置的類型。

你需要創建一個接受的類型文章和 再做一次新的自定義數組adaper。

類似:

public class ListAdapter extends ArrayAdapter<Articles> { 

public ListAdapter(Context context, int textViewResourceId) { 
    super(context, textViewResourceId); 
    // TODO Auto-generated constructor stub 
} 

private List<Articles> items; 

public ListAdapter(Context context, int resource, List<Articles> items) { 

    super(context, resource, items); 

    this.items = items; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View v = convertView; 

    if (v == null) { 

     LayoutInflater vi; 
     vi = LayoutInflater.from(getContext()); 
     v = vi.inflate(R.layout.itemlistrow, null); 

    } 

    Item p = items.get(position); 

    if (p != null) { 

     TextView tt = (TextView) v.findViewById(R.id.id); 
     TextView tt1 = (TextView) v.findViewById(R.id.categoryId); 
     TextView tt3 = (TextView) v.findViewById(R.id.description); 

     if (tt != null) { 
      tt.setText(p.userName()); 
     } 
     if (tt1 != null) { 

      tt1.setText(p.getDate); 
     } 
     //and so on... 
    } 

    return v; 

} 

,你應該用你的文章對象連接您的看法。視圖屬性。 多數民衆贊成什麼適配器代表。而字符串適配器僅用於字符串列表。

在這之後,你應該做的:

ListAdapter<Articles> arrayAdapter = new ListAdapter<Articles>(this,android.R.layout.simple_list_item_1, postsList); 
    lv.setAdapter(arrayAdapter); 

希望它幫助。

+0

+1對於建議ArrayAdapter的替代使用。 – 2013-04-22 19:01:45

1

看起來Articles.execute().get()方法返回對象的List對象。 ArrayListAdapter調用這些對象的方法toString()。如果你不提供自己的toString()方法,只需繼承Object默認的打印出對象的類名和哈希值。這就是輸出顯示的原因。要解決這個問題

一種方法是重寫toString()方法在Articles類返回要出現在ListView數據的String表示。