Android应用程序:如何在列表视图显示结果集? [英] Android App: how to display resultset in Listview?

查看:88
本文介绍了Android应用程序:如何在列表视图显示结果集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来这个....
我有MySQL数据库和使用JDBC连接器的连接。
运行查询并显示到TextView的没有问题,
但我怎么在ListView中显示所有提取的记录。
我通过在这里和那里发现了教程,但他们介绍如何使用光标做,但我有ResultSet作为我的查询结果。
请帮忙谢谢

I am new to this.... I have a connection to MySQL database and using jdbc connector. Running queries and displaying to textview no problem, but how do I display all fetched records in ListView. I went through tutorials found here and there but they describe how to do it with cursor but I have Resultset as a result of my query. Please help and Thank you

推荐答案

一旦你运行你的查询,你有你的ResultSet就像这个例子:

Once you've run your query you have your ResultSet like in this example:

sqlConnection myConn = new sqlConnection();
Statement stmt = myConn.getConnection().createStatement();
ResultSet resSet = stmt.executeQuery(myQuery);

您现在可以读取数据一步一步用了一段时间,填写你的LinkedList:

You can now read the data step-by-step with a while and fill your LinkedList:

List<Example> mExampleList = new LinkedList<Example>();

while (resSet.next()) {
    Example mExample = new Example ();      
    mExample.ID = resSet.getInt("ExampleID");
    mExample.name = resSet.getString("ExampleName");
    [..]
    mExampleList.add(mExample);
}

请注意:记得要关闭连接

Note: remember to close the connection!

该列表mExa​​mpleList现在可以成为你的适配器的内容:

The list "mExampleList" can now become the content of your adapter:

CustomAdapterOptimized mAdapter = new CustomAdapterOptimized(mContext, R.layout.example_item, mExampleList);
mListView.setAdapter(mAdapter);

这就是它。

在这个例子中我应该有一个名为Example类重新presenting单项;包含项目的列表命名ExampleList显示;上下文mContext布局example_item重presenting单项的视图列表中。

In this example I supposed you have a class named Example representing the single item; a list named ExampleList containing the items to display; a Context "mContext"; a layout "example_item" representing the view of the single item inside your list.

我使用的是CustomAdapterOptimized像这样的:

I am using a CustomAdapterOptimized like this one:

private class CustomAdapterOptimized extends ArrayAdapter<Example> {

public CustomAdapterOptimized(Context context, int textViewResourceId, List<Example> objects) {
    super(context, textViewResourceId, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getViewOptimize(position, convertView, parent);
}

public View getViewOptimize(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
            getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.example_item, null);
    viewHolder = new ViewHolder();
    viewHolder.exampleID= (TextView) convertView.findViewById(R.id.exampleID);
    viewHolder.exampleName= (TextView) convertView.findViewById(R.id.exampleName);
    convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
        Example example = (Example)getItem(position);
        viewHolder.exampleID.setText(example.ID);
        viewHolder.exampleName.setText(example.name);
        return convertView;
}

private class ViewHolder {
    public TextView exampleID;
    public TextView exampleName;
}
}

这篇关于Android应用程序:如何在列表视图显示结果集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆