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

时间:2016-07-13 12:31:35

标签: android listview asynchronous progress-bar

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

我已经尝试过AyncTask,但它在显示进度条时会抛出错误"无法在未调用looper.prepare()"

的线程内创建处理程序

我也试过runOnUiThread,但它会抛出错误"只有创建视图层次结构的原始线程才能触及它的视图。"

@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();
        }

}

2 个答案:

答案 0 :(得分:0)

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

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

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

    }
});

答案 1 :(得分:0)

创建内部类并从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>