2013-02-26 50 views
1

我是新来的android,现在正在尝试做一个小项目。它包括listviewtextviews和线性progressbar(它的值不需要改变)。 Simple Adapter被用于填充数据到textviews。 但要设置progressbar值,应使用哪个适配器。 我无法想象如何将值设置为progressbar。 请帮忙。Listview with linear straight progressbars

这里是我的代码:

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); 
+0

什么用,你有相同的适配器的问题? – Prateek 2013-02-26 06:17:27

+0

错误是“ProgressBar不是可以被此SimpleAdapter限制的视图”。 – 2013-02-26 06:47:24

+0

它不是一个视图,但它显示在列表视图的所有行内?显然你会有不同的数据来显示不同的进度值,所以这些值将来自一些“数据集合”。把它放在那里,并把适配器的数据收集。 – Prateek 2013-02-26 06:51:48

回答

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

});