Java Android-ArrayAdapter:将TextView设置为SQLiteDatabase中的值

Java Android-ArrayAdapter:将TextView设置为SQLiteDatabase中的值,java,android,android-listview,android-asynctask,android-sqlite,Java,Android,Android Listview,Android Asynctask,Android Sqlite,我想要实现的目标: public class ServiceAdapter extends ArrayAdapter<Service> { @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position Service service = getItem(positi

我想要实现的目标:

public class ServiceAdapter extends ArrayAdapter<Service> {

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Service service = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.listview_row, parent, false);
        }
        // Lookup view for data population
        TextView quantity = (TextView) convertView
                .findViewById(R.id.QUANTITY_CELL);
        TextView description = (TextView) convertView
                .findViewById(R.id.DESCRIPTION_CELL);
        Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);
        customerView = (TextView) convertView.findViewById(R.id.CUSTOMER_VIEW);
        mListRowPosition = position;
        Log.d("ServiceAdapter", "getView changed mListRowPositon to be "
                + String.valueOf(mListRowPosition));
        new AttemptGetCustomerInfo().execute();

        // Populate the data into the template view using the data object
        quantity.setText(String.valueOf(service.getQuantity()));
        description.setText(service.getDescription());

        // Set up the listener for the delete button.

        final int pos = position;
        delete.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("deleteButton", pos);
            }
        });

        customerView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog("customerView", pos);
            }
        });

        // Return the completed view to render on screen
        return convertView;
    }


class AttemptGetCustomerInfo extends AsyncTask<String, String, String> {
        String shortname = null;

        @Override
        protected String doInBackground(String... params) {

            DatabaseHelper db_helper = new DatabaseHelper(context);
            SQLiteDatabase db = db_helper.getReadableDatabase();

            Log.d("AttemptGetCustomerInfo",
                    "ListRowPos: " + String.valueOf(mListRowPosition));
            Log.d("AttemptGetCustomerInfo", "description of ListRowPos: "
                    + services.get(mListRowPosition).getDescription());
            int customer_id = services.get(mListRowPosition).getCustomerID();
            Log.d("AttemptGetCustomerInfo",
                    "customer id: " + String.valueOf(customer_id));
            Cursor c = db.rawQuery(
                    "SELECT " + CustomerEntry.COLUMN_NAME_SHORTNAME + " FROM "
                            + CustomerEntry.TABLE_NAME + " WHERE "
                            + CustomerEntry.COLUMN_NAME_CUSTOMER_ID + "="
                            + customer_id, null);
            Log.d("AttemptGetCustomerInfo",
                    "available cursor size" + String.valueOf(c.getCount()));
            if (c.getCount() == 0) {
                Log.d("AttemptGetCustomerInfo", "There are no Cursor entries!");
                return null;
            }
            c.moveToFirst();
            shortname = c
                    .getString(c
                            .getColumnIndexOrThrow(CustomerEntry.COLUMN_NAME_SHORTNAME));

            db.close();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if (shortname != null) {
                customerView.setText(shortname);
            }
        }
    }
我希望将ListView的每行一个TextView(其显示文本)动态设置为SQLite数据库中的值

我的尝试:

public class ServiceAdapter extends ArrayAdapter<Service> {

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Service service = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.listview_row, parent, false);
        }
        // Lookup view for data population
        TextView quantity = (TextView) convertView
                .findViewById(R.id.QUANTITY_CELL);
        TextView description = (TextView) convertView
                .findViewById(R.id.DESCRIPTION_CELL);
        Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);
        customerView = (TextView) convertView.findViewById(R.id.CUSTOMER_VIEW);
        mListRowPosition = position;
        Log.d("ServiceAdapter", "getView changed mListRowPositon to be "
                + String.valueOf(mListRowPosition));
        new AttemptGetCustomerInfo().execute();

        // Populate the data into the template view using the data object
        quantity.setText(String.valueOf(service.getQuantity()));
        description.setText(service.getDescription());

        // Set up the listener for the delete button.

        final int pos = position;
        delete.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("deleteButton", pos);
            }
        });

        customerView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog("customerView", pos);
            }
        });

        // Return the completed view to render on screen
        return convertView;
    }


class AttemptGetCustomerInfo extends AsyncTask<String, String, String> {
        String shortname = null;

        @Override
        protected String doInBackground(String... params) {

            DatabaseHelper db_helper = new DatabaseHelper(context);
            SQLiteDatabase db = db_helper.getReadableDatabase();

            Log.d("AttemptGetCustomerInfo",
                    "ListRowPos: " + String.valueOf(mListRowPosition));
            Log.d("AttemptGetCustomerInfo", "description of ListRowPos: "
                    + services.get(mListRowPosition).getDescription());
            int customer_id = services.get(mListRowPosition).getCustomerID();
            Log.d("AttemptGetCustomerInfo",
                    "customer id: " + String.valueOf(customer_id));
            Cursor c = db.rawQuery(
                    "SELECT " + CustomerEntry.COLUMN_NAME_SHORTNAME + " FROM "
                            + CustomerEntry.TABLE_NAME + " WHERE "
                            + CustomerEntry.COLUMN_NAME_CUSTOMER_ID + "="
                            + customer_id, null);
            Log.d("AttemptGetCustomerInfo",
                    "available cursor size" + String.valueOf(c.getCount()));
            if (c.getCount() == 0) {
                Log.d("AttemptGetCustomerInfo", "There are no Cursor entries!");
                return null;
            }
            c.moveToFirst();
            shortname = c
                    .getString(c
                            .getColumnIndexOrThrow(CustomerEntry.COLUMN_NAME_SHORTNAME));

            db.close();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if (shortname != null) {
                customerView.setText(shortname);
            }
        }
    }
getView()
中,我通过
findViewById()
将所述文本视图分配给一个全局变量。然后我将值
position
(来自
getView()
的参数)分配给全局变量
mListRowPosition
。在此之后,我通过
new AttemptGetCustomerInfo().execute()
执行AsyncTask子类

My AsyncTask子类从My
DatabaseHelper
获取
SQLiteDatabase
。使用
mListRowPosition
它从
ArrayList
数据集中服务对象的
getCustomerID()
方法接收
customer\u id

它与
customer\u id
一起构建一个SQL查询,以获取客户的短名称。查询后,它从游标获取字符串
shortname
。然后,我从前面(在
getView()
中)开始
setText(shortname)
全局文本视图

这方面的问题:

public class ServiceAdapter extends ArrayAdapter<Service> {

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Service service = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.listview_row, parent, false);
        }
        // Lookup view for data population
        TextView quantity = (TextView) convertView
                .findViewById(R.id.QUANTITY_CELL);
        TextView description = (TextView) convertView
                .findViewById(R.id.DESCRIPTION_CELL);
        Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);
        customerView = (TextView) convertView.findViewById(R.id.CUSTOMER_VIEW);
        mListRowPosition = position;
        Log.d("ServiceAdapter", "getView changed mListRowPositon to be "
                + String.valueOf(mListRowPosition));
        new AttemptGetCustomerInfo().execute();

        // Populate the data into the template view using the data object
        quantity.setText(String.valueOf(service.getQuantity()));
        description.setText(service.getDescription());

        // Set up the listener for the delete button.

        final int pos = position;
        delete.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("deleteButton", pos);
            }
        });

        customerView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog("customerView", pos);
            }
        });

        // Return the completed view to render on screen
        return convertView;
    }


class AttemptGetCustomerInfo extends AsyncTask<String, String, String> {
        String shortname = null;

        @Override
        protected String doInBackground(String... params) {

            DatabaseHelper db_helper = new DatabaseHelper(context);
            SQLiteDatabase db = db_helper.getReadableDatabase();

            Log.d("AttemptGetCustomerInfo",
                    "ListRowPos: " + String.valueOf(mListRowPosition));
            Log.d("AttemptGetCustomerInfo", "description of ListRowPos: "
                    + services.get(mListRowPosition).getDescription());
            int customer_id = services.get(mListRowPosition).getCustomerID();
            Log.d("AttemptGetCustomerInfo",
                    "customer id: " + String.valueOf(customer_id));
            Cursor c = db.rawQuery(
                    "SELECT " + CustomerEntry.COLUMN_NAME_SHORTNAME + " FROM "
                            + CustomerEntry.TABLE_NAME + " WHERE "
                            + CustomerEntry.COLUMN_NAME_CUSTOMER_ID + "="
                            + customer_id, null);
            Log.d("AttemptGetCustomerInfo",
                    "available cursor size" + String.valueOf(c.getCount()));
            if (c.getCount() == 0) {
                Log.d("AttemptGetCustomerInfo", "There are no Cursor entries!");
                return null;
            }
            c.moveToFirst();
            shortname = c
                    .getString(c
                            .getColumnIndexOrThrow(CustomerEntry.COLUMN_NAME_SHORTNAME));

            db.close();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if (shortname != null) {
                customerView.setText(shortname);
            }
        }
    }
这种“类型的作品”在某种程度上是可行的,但它似乎太慢了,以至于只有最后一个(几乎每次)的文本设置了一个值。有时它也会出错

在调试日志记录之后,我看到调用
getView()
的速度比异步任务完成的速度快得多(这是有道理的,但它破坏了我的问题解决方案)

同样有趣的是:我的调试日志告诉我,
getView()
被调用的频率比ArrayList中的数据项要高。如果有5个条目,它将调用
getView()
大约15到20次。为什么?

背后的代码:

public class ServiceAdapter extends ArrayAdapter<Service> {

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Service service = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.listview_row, parent, false);
        }
        // Lookup view for data population
        TextView quantity = (TextView) convertView
                .findViewById(R.id.QUANTITY_CELL);
        TextView description = (TextView) convertView
                .findViewById(R.id.DESCRIPTION_CELL);
        Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);
        customerView = (TextView) convertView.findViewById(R.id.CUSTOMER_VIEW);
        mListRowPosition = position;
        Log.d("ServiceAdapter", "getView changed mListRowPositon to be "
                + String.valueOf(mListRowPosition));
        new AttemptGetCustomerInfo().execute();

        // Populate the data into the template view using the data object
        quantity.setText(String.valueOf(service.getQuantity()));
        description.setText(service.getDescription());

        // Set up the listener for the delete button.

        final int pos = position;
        delete.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("deleteButton", pos);
            }
        });

        customerView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog("customerView", pos);
            }
        });

        // Return the completed view to render on screen
        return convertView;
    }


class AttemptGetCustomerInfo extends AsyncTask<String, String, String> {
        String shortname = null;

        @Override
        protected String doInBackground(String... params) {

            DatabaseHelper db_helper = new DatabaseHelper(context);
            SQLiteDatabase db = db_helper.getReadableDatabase();

            Log.d("AttemptGetCustomerInfo",
                    "ListRowPos: " + String.valueOf(mListRowPosition));
            Log.d("AttemptGetCustomerInfo", "description of ListRowPos: "
                    + services.get(mListRowPosition).getDescription());
            int customer_id = services.get(mListRowPosition).getCustomerID();
            Log.d("AttemptGetCustomerInfo",
                    "customer id: " + String.valueOf(customer_id));
            Cursor c = db.rawQuery(
                    "SELECT " + CustomerEntry.COLUMN_NAME_SHORTNAME + " FROM "
                            + CustomerEntry.TABLE_NAME + " WHERE "
                            + CustomerEntry.COLUMN_NAME_CUSTOMER_ID + "="
                            + customer_id, null);
            Log.d("AttemptGetCustomerInfo",
                    "available cursor size" + String.valueOf(c.getCount()));
            if (c.getCount() == 0) {
                Log.d("AttemptGetCustomerInfo", "There are no Cursor entries!");
                return null;
            }
            c.moveToFirst();
            shortname = c
                    .getString(c
                            .getColumnIndexOrThrow(CustomerEntry.COLUMN_NAME_SHORTNAME));

            db.close();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if (shortname != null) {
                customerView.setText(shortname);
            }
        }
    }
公共类ServiceAdapter扩展了ArrayAdapter{
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取此职位的数据项
服务=获取项目(位置);
//检查是否正在重用现有视图,否则会膨胀视图
if(convertView==null){
convertView=LayoutFlater.from(getContext())。充气(
R.layout.listview_行,父级,false);
}
//数据填充的查找视图
TextView数量=(TextView)convertView
.findviewbyd(R.id.QUANTITY\u CELL);
TextView说明=(TextView)convertView
.findviewbyd(R.id.DESCRIPTION_CELL);
按钮删除=(按钮)convertView.findViewById(R.id.Button\u delete);
customerView=(TextView)convertView.findViewById(R.id.CUSTOMER\u视图);
mListRowPosition=位置;
Log.d(“ServiceAdapter”,“getView将MLIStrowPosition更改为”
+字符串.valueOf(mListRowPosition));
新的AttemptGetCustomerInfo().execute();
//使用数据对象将数据填充到模板视图中
quantity.setText(String.valueOf(service.getQuantity());
description.setText(service.getDescription());
//设置删除按钮的侦听器。
最终int pos=位置;
delete.setOnClickListener(新建按钮.OnClickListener(){
@凌驾
公共void onClick(视图v){
显示对话框(“删除按钮”,pos);
}
});
customerView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
showDialog(“customerView”,pos);
}
});
//返回要在屏幕上渲染的已完成视图
返回视图;
}
类AttemptGetCustomerInfo扩展了AsyncTask{
字符串shortname=null;
@凌驾
受保护的字符串doInBackground(字符串…参数){
DatabaseHelper db_helper=新的DatabaseHelper(上下文);
SQLiteDatabase db=db_helper.getReadableDatabase();
Log.d(“AttemptGetCustomerInfo”,
ListRowPos:“+String.valueOf(mListRowPosition));
Log.d(“AttemptGetCustomerInfo”,“ListRowPos的说明:”
+getservices.get(mListRowPosition.getDescription());
int customer_id=services.get(mListRowPosition.getCustomerID();
Log.d(“AttemptGetCustomerInfo”,
“客户id:+String.valueOf(客户id));
游标c=db.rawQuery(
从中选择“+CustomerEntry.COLUMN\u NAME\u SHORTNAME+”
+CustomerEntry.TABLE_NAME+“其中”
+CustomerEntry.COLUMN\u NAME\u CUSTOMER\u ID+“=”
+客户标识,空);
Log.d(“AttemptGetCustomerInfo”,
“可用光标大小”+String.valueOf(c.getCount());
如果(c.getCount()==0){
Log.d(“AttemptGetCustomerInfo”,“没有光标条目!”);
返回null;
}
c、 moveToFirst();
shortname=c
.getString(c)
.getColumnIndexOrThrow(CustomerEntry.COLUMN_NAME_SHORTNAME));
db.close();
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
if(shortname!=null){
customerView.setText(短名称);
}
}
}
其他信息:

public class ServiceAdapter extends ArrayAdapter<Service> {

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Service service = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.listview_row, parent, false);
        }
        // Lookup view for data population
        TextView quantity = (TextView) convertView
                .findViewById(R.id.QUANTITY_CELL);
        TextView description = (TextView) convertView
                .findViewById(R.id.DESCRIPTION_CELL);
        Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);
        customerView = (TextView) convertView.findViewById(R.id.CUSTOMER_VIEW);
        mListRowPosition = position;
        Log.d("ServiceAdapter", "getView changed mListRowPositon to be "
                + String.valueOf(mListRowPosition));
        new AttemptGetCustomerInfo().execute();

        // Populate the data into the template view using the data object
        quantity.setText(String.valueOf(service.getQuantity()));
        description.setText(service.getDescription());

        // Set up the listener for the delete button.

        final int pos = position;
        delete.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("deleteButton", pos);
            }
        });

        customerView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog("customerView", pos);
            }
        });

        // Return the completed view to render on screen
        return convertView;
    }


class AttemptGetCustomerInfo extends AsyncTask<String, String, String> {
        String shortname = null;

        @Override
        protected String doInBackground(String... params) {

            DatabaseHelper db_helper = new DatabaseHelper(context);
            SQLiteDatabase db = db_helper.getReadableDatabase();

            Log.d("AttemptGetCustomerInfo",
                    "ListRowPos: " + String.valueOf(mListRowPosition));
            Log.d("AttemptGetCustomerInfo", "description of ListRowPos: "
                    + services.get(mListRowPosition).getDescription());
            int customer_id = services.get(mListRowPosition).getCustomerID();
            Log.d("AttemptGetCustomerInfo",
                    "customer id: " + String.valueOf(customer_id));
            Cursor c = db.rawQuery(
                    "SELECT " + CustomerEntry.COLUMN_NAME_SHORTNAME + " FROM "
                            + CustomerEntry.TABLE_NAME + " WHERE "
                            + CustomerEntry.COLUMN_NAME_CUSTOMER_ID + "="
                            + customer_id, null);
            Log.d("AttemptGetCustomerInfo",
                    "available cursor size" + String.valueOf(c.getCount()));
            if (c.getCount() == 0) {
                Log.d("AttemptGetCustomerInfo", "There are no Cursor entries!");
                return null;
            }
            c.moveToFirst();
            shortname = c
                    .getString(c
                            .getColumnIndexOrThrow(CustomerEntry.COLUMN_NAME_SHORTNAME));

            db.close();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            if (shortname != null) {
                customerView.setText(shortname);
            }
        }
    }
我没有粘贴所有的代码,在上面的代码中发生了很多代码引用,但也没有代码。我希望没有显示的方法的功能可以通过方法名称理解。

那么,让我们开始吧

如果您想在
列表视图中显示数据库中的信息
我强烈建议您使用
光标或适配器