0

我正在尝试通过 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);             


            }
4

4 回答 4

2

将此添加到您的文章类:

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

或您希望用户看到的任何其他信息。
这是因为 Android 框架会调用 YOUROBJECT.toString(); 自动渲染列表中的对象。这就是为什么你需要重写这个方法。

更新:

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

这是您填充文章对象的代码。如果您想在您的项目列表中显示此对象的其他属性。只需在 toString() 方法中添加字段。前任:

  @Override
        public String toString() {
            return "id: "+article_id + "\n"
                   +"title : " + title 
                   +"body: "+body+" date"+date +"username: "+username ;
        }
于 2013-04-22T19:02:42.313 回答
1

我认为您使用 ArrayAdapter 错误。您输入的类型 (String) 不是您应该输入的类型。

您需要创建接受类型 Articles 的新自定义数组适配器并再次执行此操作。

就像是:

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

希望能帮助到你。

于 2013-04-22T19:00:41.380 回答
1

Articles.execute().get()方法似乎返回了一个List对象ArticlesArrayListAdapter调用toString()这些对象的方法。如果您不提供自己的toString()方法,则只需继承默认方法,Object从中打印出对象的类名和哈希值。这就是输出如您所见的原因。

解决此问题的一种方法是覆盖类中的toString()方法Articles以返回String您希望出现在ListView.

于 2013-04-22T19:01:06.723 回答
0

按照此说明获取自定义适配器的示例。

于 2013-04-22T19:03:13.753 回答