0

我通过在后台运行任务在 Listview 中一一加载图像。我正在运行一个进度条,直到它完全加载。当图像开始加载时,进度的位置没有改变。我也想要进度条的位置加载图像时进行更改。怎么做?我究竟做错了什么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relaGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/Master"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/activity_master_page" />

    <ListView
        android:id="@+id/ItemView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/Master" >
    </ListView>

<ProgressBar
        android:id="@+id/ItemsProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Master"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

添加了加载图像的代码

私有类 testAynscTask 扩展 AsyncTask {

    @Override
    protected void onPreExecute() {
        TheProgressBarG = (ProgressBar) findViewById(R.id.ItemsProgressBar);
        TheProgressBarG.setVisibility(View.VISIBLE);
    }

    protected Void doInBackground(Void... ParamsP) {

        try {
            POSManager aPOSManagerL = new POSManager();
            ListCriteria aListCriteriaL = new ListCriteria();
            ObjectInformation zItemInfoL = new ObjectInformation();
            CategoryItemListG = new ObjectList();
            //Get CategoryId
            String zCategoryIdL = GetCategoryId();

            aListCriteriaL.SearchConditionsM.AddSearchConditionWithValue("Item_Category.Id", zCategoryIdL);
            Gson gsonBuilderL = new GsonBuilder().serializeNulls().create();
            String zListCriteriaL = gsonBuilderL.toJson(aListCriteriaL);

            //Get Category Items List
            aPOSManagerL.GetCategoryItems(HttpUtil.SessionKeyM,zListCriteriaL);

            DisplayMetrics zDisplayMetricsL = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(zDisplayMetricsL);
            int nPreviewSizeL = zDisplayMetricsL.widthPixels / 3;

            ObjectList zItemListL = HttpUtil.CategoryItemListM;

            for (int countL = 0; countL < zItemListL.GetLength(); countL++) {

                zItemInfoL = (ObjectInformation) zItemListL.GetObject(countL);
                String zItemIdL = (String) zItemInfoL.GetValue("ID");
                //Get Item Image
                aPOSManagerL.GetCategoryItemImage(HttpUtil.SessionKeyM,zItemIdL, nPreviewSizeL);
                zItemInfoL.SetValue("aPictureByteArrayP",HttpUtil.CategoryItemImageBytesM);
                CategoryItemListG.Add(zItemInfoL);
                publishProgress(countL);
            }

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

    protected void onProgressUpdate(Integer... PositionP) {

        ListView TheItemListViewL=(ListView) findViewById(R.id.ItemView);
        TheItemListViewL.setAdapter(anImageAdapterG);
        super.onProgressUpdate(PositionP);
    }

    @Override
    protected void onPostExecute(Void ResultP) {
        super.onPostExecute(ResultP);
        TheProgressBarG.setVisibility(View.INVISIBLE);
        anImageAdapterG.notifyDataSetChanged();

    }
}

公共类 ImageAdapter 扩展 BaseAdapter { 私有上下文 mContextL;

    public ImageAdapter(Context contextP) {
        mContextL = contextP;
    }

    public int getCount() {
        return CategoryItemListG.GetLength();
        // return mImageIds.length;
    }

    public Object getItem(int PositionP) {
        return PositionP;
    }

    public long getItemId(int PositionP) {
        return PositionP;
    }

    public View getView(int PositionP, View ConvertViewP, ViewGroup ParentP) {

        ObjectInformation zItemInfoL = (ObjectInformation)CategoryItemListG.GetObject(PositionP);
        String zItemNameL = (String)zItemInfoL.GetValue("ITEMNAME");
        String zItemPriceL = (String)zItemInfoL.GetValue("SalesPrice");
        String zItemImageBytesL = (String)zItemInfoL.GetValue("aPictureByteArrayP");

        Bitmap ItemImageBitMapL =GetItemImageBitMap(zItemImageBytesL);

         RelativeLayout TheRelativelayoutL = new RelativeLayout(getApplicationContext());

         ImageView TheItemImageL = new ImageView(mContextL);
         TheItemImageL.setImageBitmap(ItemImageBitMapL);
         TheItemImageL.setScaleType(ImageView.ScaleType.FIT_XY);
         TheItemImageL.setLayoutParams(new ListView.LayoutParams(100,100));
         TheItemImageL.setPadding(1, 0, 0, 0);
         TheItemImageL.setId(1);

         TextView tvItemNameL = new TextView(mContextL);
         tvItemNameL.setText(zItemNameL);
         tvItemNameL.setGravity(Gravity.CENTER);
         tvItemNameL.setTextSize(10);
         tvItemNameL.setTextColor(Color.parseColor("#000000"));
         tvItemNameL.setPadding(15, 0, 0, 0);
         tvItemNameL.setId(2);

         TextView tvItemPriceL = new TextView(mContextL);
         tvItemPriceL.setText("Rs. "+zItemPriceL);
         tvItemPriceL.setGravity(Gravity.CENTER);
         tvItemPriceL.setTextSize(10);
         tvItemPriceL.setTextColor(Color.parseColor("#A30000"));
         tvItemPriceL.setId(3);
         tvItemPriceL.setPadding(15, 0, 0, 20);

         TheRelativelayoutL.addView(TheItemImageL);

         RelativeLayout.LayoutParams zRelativeLayoutParamsL = new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));
         zRelativeLayoutParamsL.addRule(RelativeLayout.RIGHT_OF,1);
         zRelativeLayoutParamsL.addRule(RelativeLayout.CENTER_IN_PARENT);

         TheRelativelayoutL.addView(tvItemNameL, zRelativeLayoutParamsL);

         zRelativeLayoutParamsL = new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));
         zRelativeLayoutParamsL.addRule(RelativeLayout.RIGHT_OF,1);
         zRelativeLayoutParamsL.addRule(RelativeLayout.BELOW,2);
         zRelativeLayoutParamsL.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

         TheRelativelayoutL.addView(tvItemPriceL, zRelativeLayoutParamsL);

         return TheRelativelayoutL;
    }
4

1 回答 1

0
I made the below changes to the UI and it works fine for me

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relaGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/Master"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/activity_master_page" />

    <ListView
        android:id="@+id/ItemView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Master" >
    </ListView>

    <ProgressBar
        android:id="@+id/ItemsProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ItemView"
        android:layout_centerHorizontal="true" />


</RelativeLayout>
于 2013-02-26T04:56:49.550 回答