获取选定的项目列表视图中使用复选框。 [英] Get Selected Item Using Checkbox in Listview.

查看:289
本文介绍了获取选定的项目列表视图中使用复选框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android的应用程序,我有一个ListView和我有显示所有已安装在我的手机应用程序的

我的列表视图定制,它包含一个图标,TextView的和复选框,使用图标是显示应用程序的图标,TextView的是显示应用程序的名称,使用复选框是确定哪些项目在ListView,我选择了,

我怎么能确定的是,我在ListView行选择时,我点击我的应用程序中的按钮,复选框?我是新的机器人,所以我不知道什么是我应该做的方式。

这是我的codeS

 公共类了AppInfo {
    公众可绘制图标;
    公共字符串的applicationName;

    公开了AppInfo(){
        超();
    }

    公开了AppInfo(可绘制图标,字符串的applicationName){
        超();
        this.icon =图标;
        this.applicationName =的applicationName;
    }


}
 


 进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.app.Activity;
进口android.widget.CheckBox;

公共类AppInfoAdapter扩展ArrayAdapter<了AppInfo> {

    上下文语境;
    INT layoutResourceId;
    了AppInfo数据[] = NULL;

    公共AppInfoAdapter(上下文的背景下,INT layoutResourceId,了AppInfo []数据){
        超级(上下文,layoutResourceId,数据);
        this.layoutResourceId = layoutResourceId;
        this.context =背景;
        this.data =数据;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        查看排= convertView;
        AppInfoHolder支架=无效;

        如果(行== NULL){

            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);

            持有人=新AppInfoHolder();

            holder.imgIcon =(ImageView的)row.findViewById(R.id.imgPackageIcon);
            holder.txtTitle =(TextView中)row.findViewById(R.id.txtApplicationName);
            holder.chkSelect =(复选框)row.findViewById(R.id.chkSelect);

            row.setTag(保持器);

        }
        其他{
            支架=(AppInfoHolder)row.getTag();
        }


        了AppInfo APPINFO =数据[位置]
        holder.txtTitle.setText(appinfo.applicationName);
        holder.imgIcon.setImageDrawable(appinfo.icon);
        holder.chkSelect.setChecked(真正的);

        返回行;

    }



}
 


 进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.CheckBox;

公共类AppInfoHolder {

    ImageView的imgIcon;
    TextView的txtTitle;
    复选框chkSelect;
}
 


 进口android.app.Activity;
进口android.content.pm.ApplicationInfo;
进口android.content.pm.PackageManager;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;
进口android.widget.BaseAdapter;
进口android.widget.ListView;
进口android.widget.TextView;

进口的java.util.ArrayList;
进口的java.util.List;
进口android.content.pm.PackageInfo;

公共类CacheActivity延伸活动{

        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_cache);


            最终的ListView listApplication =(ListView控件)findViewById(R.id.listApplication);

            ApplicationInfo applicationInfo = getApplicationInfo();
            PackageManager下午= getPackageManager();
            名单< PackageInfo> PINFO =新的ArrayList< PackageInfo>();
            pInfo.addAll(pm.getInstalledPackages(0));
            了AppInfo app_info [] =新的了AppInfo [pInfo.size()];

            INT计数器= 0;
            对于(PackageInfo项目:PINFO){
                尝试{

                    applicationInfo = pm.getApplicationInfo(item.packageName,1);

                    app_info [计数器] =新的了AppInfo(pm.getApplicationIcon(applicationInfo)
                            将String.valueOf(pm.getApplicationLabel(applicationInfo)));

                    的System.out.println(柜);

                }
                赶上(例外五){
                     的System.out.println(e.getMessage());
                }

                反++;
            }

            AppInfoAdapter适配器=新AppInfoAdapter(这一点,R.layout.listview_item_row,app_info);
            listApplication.setAdapter(适配器);

        }


}
 

解决方案

假设你想获得行的复选框,在按一下按钮被选中的项目。假设根据你的标题获取选定的项目列表视图中使用复选框,当我点击一个按钮。

请尝试以下。只作如下变化。保持休息一样。

解读与探讨的话题@

<一个href="https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M">https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

MainActivity.java

 公共类MainActivity延伸活动{
     AppInfoAdapter适配器;
     了AppInfo app_info [];
        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);


            最终的ListView listApplication =(ListView控件)findViewById(R.id.listApplication);
            按钮B =(按钮)findViewById(R.id.button1);
            b.setOnClickListener(新OnClickListener()
            {

                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根

                    StringBuilder的结果=新的StringBuilder();
                    的for(int i = 0; I&LT; adapter.mCheckStates.size();我++)
                    {
                        如果(adapter.mCheckStates.get(一)==真)
                        {

                                           result.append(app_info [I] .applicationName);
                            result.append(\ N);
                        }

                    }
                    Toast.makeText(MainActivity.this,结果,1000).show();
                }

            });

            ApplicationInfo applicationInfo = getApplicationInfo();
            PackageManager下午= getPackageManager();
            名单&LT; PackageInfo&GT; PINFO =新的ArrayList&LT; PackageInfo&GT;();
            pInfo.addAll(pm.getInstalledPackages(0));
            app_info =新了AppInfo [pInfo.size()];

            INT计数器= 0;
            对于(PackageInfo项目:PINFO){
                尝试{

                    applicationInfo = pm.getApplicationInfo(item.packageName,1);

                    app_info [计数器] =新的了AppInfo(pm.getApplicationIcon(applicationInfo)
                            将String.valueOf(pm.getApplicationLabel(applicationInfo)));

                    的System.out.println(柜);

                }
                赶上(例外五){
                     的System.out.println(e.getMessage());
                }

                反++;
            }

           适配器=新AppInfoAdapter(这一点,R.layout.listview_item_row,app_info);
            listApplication.setAdapter(适配器);

        }
}
 

activity_main.xml ListView控件与按钮布敦

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。&GT;

    &LT;的ListView
        机器人:layout_width =FILL_PARENT
        机器人:ID =@ + ID / listApplication
        机器人:layout_height =FILL_PARENT
        机器人:layout_above =@ + ID /按钮1
        机器人:文本=@字符串/参考hello world/&GT;

    &LT;按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:文本=按钮/&GT;

&LT; / RelativeLayout的&GT;
 

AppInfoAdapter

 公共类AppInfoAdapter扩展ArrayAdapter&LT;了AppInfo&GT;实现CompoundButton.OnCheckedChangeListener
{SparseBooleanArray mCheckStates;

    上下文语境;
    INT layoutResourceId;
    了AppInfo数据[] = NULL;

    公共AppInfoAdapter(上下文的背景下,INT layoutResourceId,了AppInfo []数据){
        超级(上下文,layoutResourceId,数据);
        this.layoutResourceId = layoutResourceId;
        this.context =背景;
        this.data =数据;
        mCheckStates =新SparseBooleanArray(data.length);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        查看排= convertView;
        AppInfoHolder支架=无效;

        如果(行== NULL){

            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);

            持有人=新AppInfoHolder();

            holder.imgIcon =(ImageView的)row.findViewById(R.id.imageView1);
            holder.txtTitle =(TextView中)row.findViewById(R.id.textView1);
            holder.chkSelect =(复选框)row.findViewById(R.id.checkBox1);

            row.setTag(保持器);

        }
        其他{
            支架=(AppInfoHolder)row.getTag();
        }


        了AppInfo APPINFO =数据[位置]
        holder.txtTitle.setText(appinfo.applicationName);
        holder.imgIcon.setImageDrawable(appinfo.icon);
       // holder.chkSelect.setChecked(真正的);
        holder.chkSelect.setTag(位置);
        holder.chkSelect.setChecked(mCheckStates.get(位置,FALSE));
        holder.chkSelect.setOnCheckedChangeListener(本);
        返回行;

    }
    公共布尔器isChecked(INT位置){
        返回mCheckStates.get(位置,假);
    }

    公共无效setChecked(INT位置,布尔器isChecked){
        mCheckStates.put(位置,器isChecked);

    }

    公共无效切换(INT位置){
        setChecked(位置,器isChecked(位置)!);

    }
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,
        布尔器isChecked){

     mCheckStates.put((整数)buttonView.getTag(),器isChecked);

}
静态类AppInfoHolder
{
    ImageView的imgIcon;
    TextView的txtTitle;
    复选框chkSelect;

}
}
 

这里的快照

I am creating a android application where I have a listview and I display there all of the application that was installed in my mobile phone

my Listview is customized, it is contains a Icon, Textview and Checkbox, the use of the icon is to display the icon of the application, Textview is to display the name of the application, the use of the checkbox is to determine what Item in the Listview that I selected,

how can I determine what is the checkbox that I selected in the listview rows when I click a button in my application? I'm new in android so I don't know what is the approach that I should do

here is my codes

public class AppInfo {
    public Drawable icon;
    public String applicationName;

    public AppInfo(){
        super();
    }

    public AppInfo(Drawable icon, String applicationName){
        super();
        this.icon = icon;
        this.applicationName = applicationName;
    }


}


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.widget.CheckBox;

public class AppInfoAdapter extends ArrayAdapter<AppInfo> {

    Context context;
    int layoutResourceId;
    AppInfo  data[] = null;

    public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){
        super(context, layoutResourceId,data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View row = convertView;
        AppInfoHolder holder= null;

        if (row == null){

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new AppInfoHolder();

            holder.imgIcon = (ImageView) row.findViewById(R.id.imgPackageIcon);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtApplicationName);
            holder.chkSelect = (CheckBox) row.findViewById(R.id.chkSelect);

            row.setTag(holder);

        }
        else{
            holder = (AppInfoHolder)row.getTag();
        }


        AppInfo appinfo = data[position];
        holder.txtTitle.setText(appinfo.applicationName);
        holder.imgIcon.setImageDrawable(appinfo.icon);
        holder.chkSelect.setChecked(true);

        return row;

    }



}


import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckBox;

public class AppInfoHolder {

    ImageView imgIcon;
    TextView txtTitle;
    CheckBox chkSelect;
}


import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter; 
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import android.content.pm.PackageInfo;

public class CacheActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cache);


            final ListView listApplication = (ListView)findViewById(R.id.listApplication);

            ApplicationInfo applicationInfo = getApplicationInfo();
            PackageManager pm = getPackageManager();
            List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
            pInfo.addAll(pm.getInstalledPackages(0));
            AppInfo app_info[] = new AppInfo[pInfo.size()];

            int counter = 0;
            for(PackageInfo item: pInfo){
                try{

                    applicationInfo = pm.getApplicationInfo(item.packageName, 1);

                    app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo), 
                            String.valueOf(pm.getApplicationLabel(applicationInfo)));

                    System.out.println(counter);

                }
                catch(Exception e){
                     System.out.println(e.getMessage());
                }

                counter++;
            }

            AppInfoAdapter adapter = new AppInfoAdapter(this, R.layout.listview_item_row, app_info);
            listApplication.setAdapter(adapter);

        }


}

解决方案

Assuming you want to get items of row whose check boxes are checked at the click of a button. Assumption based on your title "Get Selected Item Using Checkbox in Listview when I click a Button".

Try the below. Make only changes as below. Keep the rest the same.

Explanation and discussion on the topic @

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

MainActivity.java

public class MainActivity extends Activity {
     AppInfoAdapter adapter ;
     AppInfo app_info[] ;
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            final ListView listApplication = (ListView)findViewById(R.id.listApplication);
            Button b= (Button) findViewById(R.id.button1);
            b.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    StringBuilder result = new StringBuilder();
                    for(int i=0;i<adapter.mCheckStates.size();i++)
                    {
                        if(adapter.mCheckStates.get(i)==true)
                        {

                                           result.append(app_info[i].applicationName);
                            result.append("\n");
                        }

                    }
                    Toast.makeText(MainActivity.this, result, 1000).show();
                }

            });

            ApplicationInfo applicationInfo = getApplicationInfo();
            PackageManager pm = getPackageManager();
            List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
            pInfo.addAll(pm.getInstalledPackages(0));
            app_info = new AppInfo[pInfo.size()];

            int counter = 0;
            for(PackageInfo item: pInfo){
                try{

                    applicationInfo = pm.getApplicationInfo(item.packageName, 1);

                    app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo), 
                            String.valueOf(pm.getApplicationLabel(applicationInfo)));

                    System.out.println(counter);

                }
                catch(Exception e){
                     System.out.println(e.getMessage());
                }

                counter++;
            }

           adapter = new AppInfoAdapter(this, R.layout.listview_item_row, app_info);
            listApplication.setAdapter(adapter);

        }
}

activity_main.xml ListView with button at the buton

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:layout_width="fill_parent"
        android:id="@+id/listApplication"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button1"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

AppInfoAdapter

public class AppInfoAdapter extends ArrayAdapter<AppInfo> implements CompoundButton.OnCheckedChangeListener
{  SparseBooleanArray mCheckStates; 

    Context context;
    int layoutResourceId;
    AppInfo  data[] = null;

    public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){
        super(context, layoutResourceId,data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        mCheckStates = new SparseBooleanArray(data.length);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View row = convertView;
        AppInfoHolder holder= null;

        if (row == null){

            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new AppInfoHolder();

            holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
            holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
            holder.chkSelect = (CheckBox) row.findViewById(R.id.checkBox1);

            row.setTag(holder);

        }
        else{
            holder = (AppInfoHolder)row.getTag();
        }


        AppInfo appinfo = data[position];
        holder.txtTitle.setText(appinfo.applicationName);
        holder.imgIcon.setImageDrawable(appinfo.icon);
       // holder.chkSelect.setChecked(true);
        holder.chkSelect.setTag(position);
        holder.chkSelect.setChecked(mCheckStates.get(position, false));
        holder.chkSelect.setOnCheckedChangeListener(this);
        return row;

    }
    public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));

    }
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {

     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}
static class AppInfoHolder
{
    ImageView imgIcon;
    TextView txtTitle;
    CheckBox chkSelect;

}
}

Here's the snap shot

这篇关于获取选定的项目列表视图中使用复选框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆