如何使用CheckBox和Image制作ListView?

时间:2012-02-17 12:22:30

标签: android image listview checkbox

我现在将此代码用于我的简单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>

但是我需要将Image和CheckBox添加到这个 ListView的每一行,有人可以给我一个很好的例子吗?

4 个答案:

答案 0 :(得分:1)

您需要创建自己的自定义适配器以将图像视图和复选框组件加载到每个项目,了解有关在Android中创建自定义适配器的一些基础知识。

休息,您需要处理复选框,请参阅我之前的解决方案:

How to use checkbox in listview

答案 1 :(得分:1)

我更喜欢使用TableLayout而不是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>

答案 2 :(得分: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);

答案 3 :(得分:1)

找到这个很棒的教程。试试this