Sunday, May 9, 2010

[android-developers] Re: Strange ListView divider behaviour

The code should be the same as in the mentioned tutorial:

***********
List8.java
***********
public class List8 extends ListActivity {

PhotoAdapter mAdapter;

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

setContentView(R.layout.list_8);

getListView().setEmptyView(findViewById(R.id.empty));

mAdapter = new PhotoAdapter(this);
setListAdapter(mAdapter);

Button clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
mAdapter.clearPhotos();
}
});

Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
mAdapter.addPhotos();
}
});
}

public class PhotoAdapter extends BaseAdapter {

private Integer[] mPhotoPool = {
R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
R.drawable.sample_thumb_2,
R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
R.drawable.sample_thumb_5,
R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
private ArrayList<Integer> mPhotos = new ArrayList<Integer>();

public PhotoAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mPhotos.size();
}

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

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

public View getView(int position, View convertView, ViewGroup
parent) {
ImageView i = new ImageView(mContext);

i.setImageResource(mPhotos.get(position));
i.setAdjustViewBounds(true);
i.setLayoutParams(new
AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;

public void clearPhotos() {
mPhotos.clear();
notifyDataSetChanged();
}

public void addPhotos() {
int whichPhoto = (int) Math.round(Math.random() *
(mPhotoPool.length - 1));
int newPhoto = mPhotoPool[whichPhoto];
mPhotos.add(newPhoto);
notifyDataSetChanged();
}
}
}

*********************
List_8.xml
*********************
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_8_new_photo"/>

<Button android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_8_clear_photos"/>

</LinearLayout>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>

<TextView android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/list_8_no_photos"/>

</FrameLayout>

</LinearLayout>

************************
AndroidManifest.xml
************************
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.me.imagelisttest">
<application>
<activity android:name=".List8" android:label="List8">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

***************
Plus those jpg photos in drawable directory.

Thanks in advance.

--
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