当前位置: 代码迷 >> Android >> Android Tutorial(一)Simple Cursor Adapter and ListView
  详细解决方案

Android Tutorial(一)Simple Cursor Adapter and ListView

热度:373   发布时间:2016-05-01 11:07:07.0
Android Tutorial(1)Simple Cursor Adapter and ListView
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/
  相关解决方案