从包含图像标签和开关列表的列表视图中获取开关ID

时间:2019-02-12 00:07:54

标签: java android android-studio

您好,我有一个列表视图,其中显示了设备上已安装应用程序的列表,其中包含图标和标题,还有一个SwitchCompact,我需要获取每个选定行的SwitchCompact的ID才能执行所需的代码

  <ImageView
    android:id="@+id/app_icon"
    android:layout_width="50dp"
    android:layout_height="70dp"
    android:gravity="center"
    android:layout_marginTop="2dp"
    android:layout_marginLeft="25dp"
    android:contentDescription="@null"
    android:scaleType="center"/>

<TextView
    android:id="@+id/list_app_name"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignTop="@+id/app_icon"
    android:layout_marginTop="8dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_weight="85.6"
    android:gravity="left"
    android:paddingRight="10dp"
    android:text="test1"
    android:textColor="@color/colorPrimary"
    android:textSize="20dp"
    android:textStyle="bold" />

    <android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:layout_marginTop="10dp"
        android:padding="15dp"
        android:thumb="@drawable/swithc_thumb"
        app:track="@drawable/switch_track"
        android:id="@+id/Swtch"/>

我尝试了通常的setOnCheckedChangeListener方法,但是它返回null,在这种情况下switchcompact的默认ID对我不起作用。

这是我的适配器类

public class AppAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<AppList> appInList;

    public AppAdapter(Context context, List<AppList> customizedListView) {
        layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        appInList = customizedListView;
    }
    @Override
    public int getCount() {
        return appInList.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.app_item_layout, parent, false);

            listViewHolder.appNameText = (TextView)convertView.findViewById(R.id.list_app_name);
            listViewHolder.appIcon = (ImageView)convertView.findViewById(R.id.app_icon);
            convertView.setTag(listViewHolder);
        }
        else{
            listViewHolder = (ViewHolder)convertView.getTag();
        }
        listViewHolder.appNameText.setText(appInList.get(position).getName());
        listViewHolder.appIcon.setImageDrawable(appInList.get(position).getIcon());

        return convertView;
    }
}
static class ViewHolder{
    TextView appNameText;
    ImageView appIcon;
}

public class AppList {

    private String name;
    Drawable icon;

    public AppList(String name, Drawable icon) {
        this.name = name;
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public Drawable getIcon() {
        return icon;
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚将switchCompat添加到您的viewHolder中,以便您可以使用switchCompat

public class AppAdapter extends BaseAdapter {    
    private LayoutInflater layoutInflater;
    private List<AppList> appInList;

    public AppAdapter(Context context, List<AppList> customizedListView) {
        layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        appInList = customizedListView;
    }
    @Override
    public int getCount() {
        return appInList.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.app_item_layout, parent, false);

            listViewHolder.appNameText = (TextView)convertView.findViewById(R.id.list_app_name);
            listViewHolder.appIcon = (ImageView)convertView.findViewById(R.id.app_icon);
            listViewHolder.appSwitch = (SwitchCompat) convertView.findViewById(R.id.Swtch); //added switchCompat here.
            convertView.setTag(listViewHolder);
        }
        else{
            listViewHolder = (ViewHolder)convertView.getTag();
        }
        listViewHolder.appNameText.setText(appInList.get(position).getName());
        listViewHolder.appIcon.setImageDrawable(appInList.get(position).getIcon());

        /* you can now use your appSwitch here. 
        listViewHolder.appSwitch 
        */

        return convertView;
    }
}
static class ViewHolder {

    TextView appNameText;
    ImageView appIcon;
    SwitchCompat appSwitch; // added switch compat to your viewHolder.
}