在 asynctask 类中设置为 listView

Posted

技术标签:

【中文标题】在 asynctask 类中设置为 listView【英文标题】:set to listView in asynctask class 【发布时间】:2018-01-26 21:42:03 【问题描述】:

我是 android 新手,我想这样做,但我有问题。 我有许多可以加载到任何 ListView 的适配器,我只想在 AsyncTask 中执行此操作。我的每个适配器都是这样的:

 package com.example.mohammad.workclock;

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.ListView;
 import android.widget.TextView;

 import com.example.mohammad.workclock.Tbls.Projects;

 import java.util.ArrayList;
 import java.util.List;


public class projectAdapter extends BaseAdapter 

private Context context;

private LayoutInflater layoutInflater;

private List<Projects> listProject = new ArrayList<>();

private ListView listView;
public projectAdapter(Context context) 
    this.context = context;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



@Override
public int getCount() 
    return listProject.size();


@Override
public Object getItem(int i) 
    return listProject.get(i);


@Override
public long getItemId(int i) 
    return i;


@Override
public View getView(int position, View view, ViewGroup parent) 
    View Row = layoutInflater.inflate(R.layout.itemsproject, parent, false);

    listView = (ListView) parent;


    TextView Txt_Title = (TextView) Row.findViewById(R.id.txt_Title);
    Projects P = (Projects) getItem(position);
    Txt_Title.setText(P.getTitle());

    return Row;



public void upDateInputData(List<Projects> listProject) 
    this.listProject = listProject;
    notifyDataSetChanged();

为了将数据加载到 ListView,我有一个在 AsyncTask 中执行此操作的类,这是该类:

       class loadAsyncTaskToListView  extends AsyncTask<Void, Void, ArrayList<T>> 
        private SQLiteDatabase db;
        private Context context;
        private T adapter;
        private ProgressDialog progressDialog;
        private String tableName;
        private N object_;

        public loadAsyncTaskToListView(Context context, T adapter, N object, String tableName)
            this.context = context;
            db = getReadableDatabase();
            this.adapter = adapter;
            this.tableName = tableName;
            this.object_ = object;
        


        @Override
        protected void onPreExecute() 
            progressDialog = ProgressDialog.show(this.context, "","در حال بارگذاری");
        


        @Override
        protected ArrayList doInBackground(Void... voids) 
            String Query = " SELECT * FROM " + tableName;
            List<N> ListTobject = new ArrayList<>();
            Cursor cursor = db.rawQuery(Query, null);

            if (cursor.moveToFirst())
                do 
                    N object = this.object_;
                    for (Field field : listField) 

                        Annotation AnnotationF = field.getAnnotation(AnnotationPartialClass.field.class);
                        AnnotationPartialClass.field fieldAnnotation = (AnnotationPartialClass.field) AnnotationF;

                        field.setAccessible(true);

                        setvalueToobject(object, field, cursor.getString(cursor.getColumnIndex(fieldAnnotation.NameField())));



                    

                
                while (cursor.moveToNext());
            return (ArrayList<T>) ListTobject;
        

        @Override
        protected void onPostExecute(ArrayList<T> inputObjects) 
            ((projectAdapter)adapter).upDateInputData((List<Projects>) inputObjects);
            progressDialog.dismiss();
        

        public <N>void setvalueToobject(N object, Field field, Object fieldValue)
                field.setAccessible(true);
            try 
                field.set(object, fieldValue);
             catch (IllegalAccessException e) 
                e.printStackTrace();
            

        
    

知道什么时候我想使用这个方法在我的活动中写下这段代码:

projectAdapter dd = new projectAdapter(this);
LV.setAdapter(dd);
// LV is my ListView
LoadToListView(LV,dd, new Projects(),"");

当我运行代码时,我有错误列表:

08-18 13:18:28.999 26047-26163/com.example.mohammad.workclock 
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

Process: com.example.mohammad.workclock, PID: 26047
                                                                            java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                at java.lang.Thread.run(Thread.java:761)
                                                                             Caused by: java.lang.IllegalArgumentException: field com.example.mohammad.workclock.Tbls.Projects.ID has type int, got java.lang.String
                                                                                at java.lang.reflect.Field.set(Native Method)
                                                                                at com.example.mohammad.workclock.AsyncTask.WholeWorWithDb$1loadAsyncTaskToListView.setvalueToobject(WholeWorWithDb.java:383)
                                                                                at com.example.mohammad.workclock.AsyncTask.WholeWorWithDb$1loadAsyncTaskToListView.doInBackground(WholeWorWithDb.java:357)
                                                                                at com.example.mohammad.workclock.AsyncTask.WholeWorWithDb$1loadAsyncTaskToListView.doInBackground(WholeWorWithDb.java:306)
                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                at java.lang.Thread.run(Thread.java:761) 

我该如何解决?

【问题讨论】:

我的错误是在这个方法 setvalueToobject 在这一行 field.set(object, fieldValue); 【参考方案1】:

如您的 logcat 中所列,问题出在:字段 com.example.mohammad.workclock.Tbls.Projects.ID 的类型为 int,得到 java.lang.String 所以你期待 int,但有 String

附:这似乎是一个大奖: ListView + AssyncTask + 反射) 以后尽量避免所有这些事情

【讨论】:

【参考方案2】:

这行有问题

setvalueToobject(object,field,cursor.getString(cursor.getColumnIndex(fieldAnnotation.NameField())));

在您的“项目”类中,可能有许多具有不同数据类型的字段。 但是您仅将字段值作为字符串发送。(即您的 Int 类型的字段并且您试图在其中存储一个字符串值。) 所以如果有任何不同数据类型的字段。然后你会得到这个异常。

所以你应该首先通过 field.getType() 方法检查你的字段类型,然后将你的字段值发送给“setvalueToobject”方法。

我希望它会起作用。

【讨论】:

以上是关于在 asynctask 类中设置为 listView的主要内容,如果未能解决你的问题,请参考以下文章

在java中设置为构造函数参数[重复]。

如何在 Android Studio 中使用 AsyncTask 从可绘制对象中设置图像

无法使用 AsyncTask 在 TextView 中设置文本

Android - 在 AsyncTask 中设置 WebViewClient 会导致 NullPointerException

确定根记录器是不是在 Python 中设置为 DEBUG 级别?

iOS 核心数据抽象类和子类