【问题标题】:Listview with checkbox, imageview, textview and imageview filled with an arraylist带有复选框、图像视图、文本视图和图像视图的列表视图填充了一个数组列表
【发布时间】:2015-02-18 14:23:40
【问题描述】:

我想制作一个从数组列表中获取数据的列表视图。 这是单行的例子

该复选框默认禁用,仅在 longitemclicklistener 中激活(用于多次删除)。

最后一张图片是一个菜单按钮,用于打开删除、共享等菜单列表。

我有一个包含 100 个数字(字符串)的数组列表(myList),对于每个存储的名称,我想根据特定条件显示/隐藏图像

我有这一行:

行.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="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1">

    <CheckBox
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:text=" "
        android:id="@+id/checkBox"
        android:checked="false" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/im_buho"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="                  TEXTVIEW"
        android:id="@+id/textView"
        android:layout_weight="0.66" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:src="@android:drawable/ic_menu_add"/>

</LinearLayout>

这是activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我该怎么做?

我不知道如何制作我的自定义适配器。使用 simplearrayadapter 很容易,但我不能使用自定义行。有什么帮助吗? 亲吻,塔季扬娜。对不起我的英语^^

【问题讨论】:

    标签: java android listview arraylist


    【解决方案1】:

    这是一个示例自定义适配器实现:

    public class CustomUsersAdapter extends ArrayAdapter<User> {
        public CustomUsersAdapter(Context context, ArrayList<User> users) {
            super(context, 0, users);
         }
    
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            User user = getItem(position);    
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
               convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
            }
            // Lookup view for data population
            TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
            TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
            // Populate the data into the template view using the data object
            tvName.setText(user.name);
            tvHome.setText(user.hometown);
            // Return the completed view to render on screen
            return convertView;
        }
    }
    

    Source

    您可以通过将 R.layout.item_user 替换为 R.layout.row 来扩充您的 row.xml,并用您自己的 pojo 替换 User

    【讨论】:

    • 谢谢,戈尔辛。我怎样才能拥有 MainActivity.java?这是我的数组列表 ArrayList myList = new ArrayList(); myList .add("900120120") 等直到 100 部电话。 myListView = (ListView)findViewById(R.id.list);但我不知道怎么跟...亲亲^^
    • 你可以查看源链接,这是一个完整的例子
    • 对不起,我没看到。我会检查链接。亲吻^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多