列表视图与线性直进度栏

时间:2013-02-26 06:15:43

标签: android android-listview android-progressbar

我是android新手,现在正在尝试制作一个小项目。 它包含listview textviews和线性progressbar(其值不需要更改)。 Simple Adapter用于将数据填充到textviews。 但要设置progressbar值,应使用哪个适配器。 我无法想象如何将值设置为progressbar。 请帮助解决这个问题。

这是我的代码:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ListView
    android:id="@+id/dblist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />
</LinearLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >    
<!--  ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dip"      
            android:layout_alignParentLeft="true"
            android:background="@drawable/image_bg" 
        android:layout_marginRight="5dip">

    <ImageView     
        android:id="@+id/list_image"   
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/ic_launcher"/>

</LinearLayout>

<!-- Title Of Song-->
<TextView
    android:id="@+id/col1tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Rihanna Love the way lie"
    android:textColor="#040404"
    android:typeface="sans" 
    android:textSize="15dip"
    android:textStyle="bold"/>

<!-- Artist Name -->
<TextView
    android:id="@+id/col2tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/col1tv"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail"
    android:text="Just gona stand there and ..." />

<TextView 
    android:id="@+id/package_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/col1tv"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/col2tv"
    android:text=" puzzles, 0 solved" />

<!-- Rightend Duration -->


 <!-- Rightend Arrow -->    
 <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"/>

</RelativeLayout>

main.java

    String[] from = new String[] {"id", "name", "size"};
    int[] to = new int[] { R.id.id, R.id.name, R.id.size };

    // prepare the list of all records
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < pkg_id_col.size(); i++) {
         HashMap<String, String> map = new HashMap<String, String>();
         map.put("id", id_col.get(i));
         map.put("name", name_col.get(i));
         map.put("size", size.get(i));

         fillMaps.add(map);
     }

    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
    lv_add_new.setAdapter(adapter);

1 个答案:

答案 0 :(得分:2)

SimpleAdapter知道如何仅为TextViewsImageView绑定数据。要绑定您的ProgressBar,请使用SimpleAdapter.ViewBinder

String[] from = new String[] {"id", "name", "size", "progress"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size, R.id.theIdOfTheProgressBar}; // it seems that you don't have a `ProgressBar` in the layout

// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
     HashMap<String, String> map = new HashMap<String, String>();
     map.put("id", id_col.get(i));
     map.put("name", name_col.get(i));
     map.put("size", size.get(i));
     map.put("progress", valueForProgressBar); // progress will hold the progress for  your ProgressBar
     fillMaps.add(map);
 }

SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {

      public boolean setViewValue(View view, Object data, String textRepresentation) {
           if (view.getId == R.id.theIdOfTheProgressBar) {
               ((ProgressBar) view).setProgress(Integer.parseInt(data));
               return true;
           }
           return false; 
      }

});