Saturday, September 25, 2010

[android-developers] Imageadapter problems

Hi,
Having a problem with an image adapter. What I am trying to do is
get a list
of all the installed apps on my phone, display their icons and name,
all in a grid
with 4 columns.

On first look all seems well but I see duplicates in apparently
random locations
and when I scroll back to the beginning the content of the rows has
changed! I cannot
ascertain any specific pattern. Either whole rows have moved, or
the order of a
specific row has changed, or there are single duplicates on the
screen.

Here is the XML and code:

layout_main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:layout_height="fill_parent">

<GridView android:id="@+id/GridView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:numColumns="4">
</GridView>

</LinearLayout>


layout_row.xml

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

<ImageView android:id="@+id/icon_image" android:layout_width="40dp"
android:layout_marginLeft="20dp"
android:paddingLeft="1px"
android:gravity="right"
android:layout_height="40dp">
</ImageView>

<TextView android:id="@+id/icon_text" android:layout_width="78dp"
android:layout_marginBottom="5dp"
android:layout_height="32dp" android:text="TextView"
android:gravity="center|top"
android:textColorHighlight="#ffffff">
</TextView>
</LinearLayout>


Code

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mPackageManager = getPackageManager();

setContentView(R.layout.layout_main);

List<PackageInfo> installedPackages =
mPackageManager.getInstalledPackages(0);

grid = (GridView) findViewById(R.id.GridView);
grid.setAdapter(new ImageAdapter(mainActivity.this,
installedPackages, installedPackages.size()));
}

public class ImageAdapter extends BaseAdapter {
Context context;
List<PackageInfo> list;
int maxsize;

public ImageAdapter(Context argContext, List<PackageInfo>
argPackageInfo, int argMaxsize) {
context = argContext;
list = argPackageInfo;
maxsize = argMaxsize;
}

@Override
public int getCount() {
return maxsize;
}

@Override
public View getView(final int position, View convertView, ViewGroup
parent) {
View view;

try {
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.layout_row, null);

TextView textview = (TextView) view.findViewById(R.id.icon_text);
textview.setTextSize(13f);
textview.setTextColor(0xffffffff);

textview.setText(mPackageManager.getApplicationLabel(list.get(position).applicationInfo));

ImageView imageview = (ImageView)
view.findViewById(R.id.icon_image);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);

imageview.setImageDrawable(mPackageManager.getApplicationIcon((list.get(position).applicationInfo)));

} else {

view = convertView;
}

return view;

} catch (Exception e) {
}

return view = convertView;
}

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

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


Any ideas or thoughts would be appreciated.

Kent

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment