Android Tutorial(1)Simple Cursor Adapter and ListView

本文通过实例详细介绍了如何在Android应用中使用SimpleCursorAdapter及ListView组件,并展示了如何从URL加载图片到ImageView,适用于初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android Tutorial(1)Simple Cursor Adapter and ListView

ListActivity is actually a subclass of the Activity class that includes strategies where you can attach ListAdapters.

1. Prepare the files
First Create a XML file from Android ---> Android XML Layout File
Place Layouts ---> RelativeLayout and change the right id.

Forget of the first steps, I should start from creating the activity class that will automatically create the XML.
Create the activity class file from Android ----> Android Activity

Create a Android -----> Android XML File

2. Using the Graphical Layout
Draw the List page with these XML
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".activity.impl.ProductsListActivity"]]>


<ListView
android:id="@+id/products_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"]]>
</ListView]]>


</RelativeLayout]]>

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"]]>


<ImageView
android:id="@+id/product_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:layout_marginTop="19dp"
android:src="@drawable/ic_launcher"/>


<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/product_image"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/product_image"
android:text="Product Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>


<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/product_image"
android:layout_alignLeft="@+id/product_name"
android:text="$1.55"
android:textAppearance="?android:attr/textAppearanceMedium"/>


<TextView
android:id="@+id/product_desn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/product_price"
android:layout_below="@+id/product_price"
android:text="This product is really great, you need to buy this, good choice."
android:textAppearance="?android:attr/textAppearanceSmall"/>


<Button
android:id="@+id/button_add"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/product_desn"
android:layout_below="@+id/product_desn"
android:text="Add"/>


</RelativeLayout]]>

I feel good to directly use the Graphical tools. It works. All the source codes are in EasyRestClientAndroid.

Next steps, I am going to figure out how to deal with pagination and load picture from URL.

3. Load the ImageView from URL
Here is the easiest way to get ImageView from URL, maybe in the future I will think about how to cache them.
ImageView t4 = (ImageView) view.findViewById(R.id.product_image);
URL url = null;
Bitmap bmp = null;
try {
url = new URL(item.getProductImageURL());
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
t4.setImageBitmap(bmp);
return view;

References:
http://vimaltuts.com/android-tutorials/simple-cursor-adapter-and-listview
http://www.javasrilankansupport.com/2012/05/android-listview-example-with-image-and.html
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

Complex way to load the URL and place in Cache
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/

Pagination
http://p-xr.com/android-tutorial-dynamicaly-load-more-items-to-the-listview-never-ending-list/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值