【问题标题】:How to make ListView with CheckBox and Image?如何用 CheckBox 和 Image 制作 ListView?
【发布时间】:2013-01-03 15:00:12
【问题描述】:

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

但是我需要在 THIS ListView 的每一行中添加 Image 和 CheckBox,有人能给我一个很好的例子吗?

【问题讨论】:

标签: android image listview checkbox


【解决方案1】:

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

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

How to use checkbox in listview

【讨论】:

    【解决方案2】:

    我更喜欢使用 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>
    

    【讨论】:

    • 注意:我给出了一个对我有用的旧代码,除了我提到的回收问题。如果此代码不起作用,请告诉我。
    【解决方案3】:

    感谢@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>
    

    并在您的 Activity 中以 onCreate 为例(例如)添加以下代码:

    SharedPreferences myPrefsApps = AppsActivity.this.getSharedPreferences("myPrefsApps", MODE_PRIVATE);
    String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
    
                AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"));
                setListAdapter(adapter);
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多