Android : 使用自定义适配器异步加载列表视图并在 OnCreate 中加载时显示进度条

Android : load listview with custom adapter async and display progress bar while loading in OnCreate

我在 activity 上有列表视图和文本视图,我想从 Web 服务调用异步加载列表视图并在调用后在文本视图中显示记录数,并在异步服务调用时显示进度条。

我试过 AyncTask,但它在显示进度条时抛出错误 "can't create handler inside thread that has not called looper.prepare()"

我也试过 runOnUiThread 但它抛出错误 "Only the original thread that created a view hierarchy can touch its views."

@Override
protected void onCreate(Bundle savedInstanceState) {
     try {

new AsyncTask<Void, Void, Boolean>() {
                protected Boolean doInBackground(Void... params) {

GetData();
  return null;
                }

                protected void onPostExecute(Boolean result) {
                    hide();
                }
            }.execute();
 } catch (Exception e) {
            e.printStackTrace();
        }
    }

 private void GetData() {
        try {

            show();//display progress bar

           ServiceCall();//web service call

           hide()//hide progress bar

           textView.setText(" total records : 5");

  }catch (Exception e) {
            e.printStackTrace();
        }

}

移动textView.setText(" total records : 5");

runOnUiThread(new Runnable() {
     @Override
     public void run() {

 textView.setText(" total records : 5");

    }
});

创建内部 class 并从 oncreate() 调用它,如 new SubCategory().execute();

  public class SubCategory extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SubCategories.this);
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.setMessage("Please wait...");
            pDialog.show();
        }

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

            //  Log.e("email password", email + "    " + password);
//call you service and perform parsing

            return null;
        }


        protected void onPostExecute(String result) {

                lvsubcategorylist.setAdapter(new AdapterSubCategory(SubCategories.this, categoryname));                

            }
            //Error status is true
            else {
                Toast.makeText(getApplicationContext(), "Error occured in invoking webservice.", Toast.LENGTH_LONG).show();
            }
            //Re-initialize Error Status to False
            errored = false;
        }
    }

适配器

public class AdapterSubCategory extends BaseAdapter {
    String[] categoryname;

    Context context;
    private static LayoutInflater inflater=null;
    public AdapterSubCategory(Context context, String[] categoryname) {
        this.context=context;
        this.categoryname=categoryname;

        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }



    @Override
    public int getCount() {
        return categoryname.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        if(currntview==null)
        {
            currntview=inflater.inflate(R.layout.subcategorylist,parent,false);
        }
        TextView tvcategoryname=(TextView)currntview.findViewById(R.id.tv_category);
        tvcategoryname.setText(categoryname[position]);



        return currntview;
    }
}

subcategorylist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:id="@+id/imageView"
                android:src="@drawable/right"
                android:scaleType="centerInside"
                android:layout_marginLeft="5dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/tv_category"
                android:minHeight="50dp"
                android:textStyle="bold"
                android:typeface="serif"
                android:gravity="center_vertical"
                android:paddingStart="10dp"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>