简体   繁体   中英

Listview with checkbox, imageview, textview and imageview filled with an arraylist

I want to make a listview that takes the data from an arraylist. This is an example of the single row

在此输入图像描述

The checkbox is disabled by default, it only gets activates in a longitemclicklistener(for multiple delete).

The last image is a menu button that opens a menu list for delete, share, etc.

I have an arraylist(myList) with 100 numbers(Strings), For each stored name, I want to show/hide the image according to certain condition

I have this row:

row.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>

This is the 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>

How can I do that?

I don´t know how make my customadapter. With a simplearrayadapter is easy but I can´t use a custom row. Any help? Kisses, Tatyana. Sorry for my English ^^

Here is a sample custom adapter implementation:

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

You can inflate your row.xml by replacing R.layout.item_user with R.layout.row and replace the User class by your own pojo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM