2012-02-17 63 views
1

我现在使用此代码为我简单的ListView:如何使用CheckBox和Image制作ListView?

final ListView lv = (ListView)findViewById(R.id.apps_list); 
     lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myString.split("\n"))); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

      } 
     }); 

和布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:id="@+id/separator_apps" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/apps" /> 


    <ListView 
     android:id="@+id/apps_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/separator_apps" > 

    </ListView> 

</RelativeLayout> 

但我需要的图像和CheckBox添加到每个行 ListView控件,可能有人给我一个很好的例子?

+0

http://www.vogella.de/articles/AndroidListView/article.html – 2012-02-17 12:24:28

+0

提到此:http://stackoverflow.com/questions/4797926/how-to-make-a-listactivity-with- custom-list-item-with-nested-clickable-button.It类似于您problem.you只需要通过复选框,并通过TextView的图像来替换按钮。 – Hiral 2012-02-17 12:25:11

+0

谢谢,我会看看它。 – Adam 2012-02-17 12:27:29

回答

1

您需要创建自己的自定义适配器来加载图像视图和复选框组件到每一个项目,学习关于An​​droid的创建自定义适配器的一些基础知识。

休息时,你需要处理复选框,看我以前的解决方案:

How to use checkbox in listview

1

我更喜欢使用TableLayout,而不是ListView控件用于显示复选框,因为检查框将回收每次用户滚动listview,所以你必须缓存复选框中的所有数据。但是,如果您想使用图像或其他静态视图,则可以使用ListView。

的代码看起来像这样的复选框和图片,如果你有一些方法从复选框缓存数据。

public class SDLibrary extends Activity { 
ArrayList<LinearLayout> layout; 
Context ctx; 
ListView sdlistv; 
private int[] bitmapArray; 
String[] mFiles; 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sdlay); 
    ctx=this; 
    ListView sdlistv=(ListView) findViewById(R.id.list); 
    layout=new ArrayList<LinearLayout>(); 

    for(int i=0;i<bitmapArray.length;i++){ 
     LinearLayout ll=new LinearLayout(this); 
     layout.add(ll); 
    } 
    CustomAdapter ca=new CustomAdapter(this, R.id.linear, layout); 
    sdlistv.setAdapter(ca); 
    sdlistv.setFadingEdgeLength(40); 
} 
private class CustomAdapter extends ArrayAdapter<LinearLayout>{ 

    public CustomAdapter(Context context, int resources, List<LinearLayout> objects){ 
     super(context, resources, objects); 
    } 
    public View getView(int position, View convertView, ViewGroup parent){ 
     LinearLayout row; 

     LayoutInflater mInflater = getLayoutInflater(); 
     if (convertView == null){ 
      row = getItem(position); 
     } 
     else{ 
      row = (LinearLayout) mInflater.inflate(R.layout.sdlay, null); 
     } 
     ImageView imageView =new ImageView(ctx); 
     CheckBox cb=new CheckBox(ctx); 
     imageView.setImageResource(bitmapArray[position]); //bitmapArray would be the array of images from the drawable 
     imgData.add(imageView); 
     row.addView(cb); 
     row.addView(imageView); 
     return row; 
    } 
} 
} 

sdlay.xml看起来就像这样: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<LinearLayout 
android:id="@+id/linear" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
> 
</LinearLayout> 
</LinearLayout> 
+0

注:我已经给了一个旧的代码,除了我提到的回收问题。如果此代码无效,请告诉我。 – noob 2012-02-17 12:43:28

1

感谢@Lalit Poptani的教程。

创建新类:

我用这个代码解决它

public class AppsArrayAdapter extends ArrayAdapter<String> { 
    private final Context context; 

    private final String DefaultApps = "def_apps"; 

    public AppsArrayAdapter(Context context, String[] values) { 
     super(context, R.layout.apps, values); 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE); 
     String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, ""); 
     String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, ""); 


     LayoutInflater inflater = (LayoutInflater)context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.apps, parent, false); 
     TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps); 
     CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps); 
     text_apps.setText(prefNameDefaultApps.split("\n")[position]); 

     return rowView; 
    } 
} 

然后创建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" > 

    <TextView 
     android:id="@+id/text_apps" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 


    <CheckBox 
     android:id="@+id/check_apps" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" /> 

</RelativeLayout> 

并在您的活动的onCreate(例如)添加以下代码:

SharedPreferences myPrefsApps = AppsActivity.this.getSharedPreferences("myPrefsApps", MODE_PRIVATE); 
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, ""); 

      AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n")); 
      setListAdapter(adapter); 
1

找到这个伟大的教程。尝试this

相关问题