Monday, 29 September 2014

Initialising GridView with progress bars



I have two async tasks one of which finds URLs of images and the other one downloads them. I also have a gridview where I want to display images. I have written a custom adapter. Here is the code:



public class ImageGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<Picture> pictures = new ArrayList<Picture>();

public ImageGridAdapter(Context context) {
this.context = context;
}

public void addPicture(Picture picture) {
pictures.add(picture);
notifyDataSetChanged();
}

@Override
public int getCount() {
return pictures.size();
}

@Override
public Object getItem(int i) {
return pictures.get(i);
}

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

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view;
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
view = inflater.inflate(R.layout.pic_and_info, null);

} else {
view = convertView;
}
TextView textView = (TextView) view.findViewById(R.id.gridInfo);
textView.setText("@" + pictures.get(i).getAuthor());
ImageView imageView = (ImageView) view.findViewById(R.id.gridImage);
view.findViewById(R.id.pbar).setVisibility(View.GONE);
imageView.setImageDrawable(pictures.get(i).getDrawable());
return view;
}

}


My layout elements that are being drawn in the GridView look like this:



<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/picInfo"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/gridImage"
android:paddingLeft="15dp"
android:scaleType="centerCrop"
android:paddingRight="15dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"/>

<ProgressBar
android:id="@+id/pbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_centerInParent="true"
/>

<TextView
android:id="@+id/gridInfo"
android:layout_below="@+id/gridImage"
android:layout_marginTop="14dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#585858"/>

</RelativeLayout>


So the problem I have is really silly. I want to draw a progress bar for each link during postExecute of imageSearching task. But i don't know how to initiate the drawing of GridView. With my current code progress bars are drawn in getView and disapper before I can see them.


No comments:

Post a Comment