在listview中添加onClickListener

时间:2014-10-14 16:54:56

标签: java android listview android-listview switch-statement

我想为Android应用程序进行设置活动。 因此,我想制作一个带有图像,标题和开关(开/关)的自定义列表视图。 为了实现这一点,我制作了两个xml布局文件:activity_main.xml(包括listview)和mylist.xml(包括ImageView(id = icon),TextView(id = item)和Switch(id = switch1))

除此之外,我还制作了一个CustomListAdapter.java文件:

public class CustomListAdapter extends ArrayAdapter<String> {

     private final Activity context;
     private final String[] itemname;
     private final Integer[] imgid;
     private final Boolean[] status;

     public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, Boolean[] status) {
         super(context, R.layout.mylist, itemname);
         // TODO Auto-generated constructor stub

         this.context=context;
         this.itemname=itemname;
         this.imgid=imgid;
         this.status=status;
     }

     public View getView(int position,View view,ViewGroup parent) {
         LayoutInflater inflater=context.getLayoutInflater();
         View rowView=inflater.inflate(R.layout.mylist, null,true);

         ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
         TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
         Switch switchView = (Switch) rowView.findViewById(R.id.switch1);

         imageView.setImageResource(imgid[position]);
         txtTitle.setText(itemname[position]);
         switchView.setChecked(status[position]);

         return rowView;

     };
}

和MainActivity.java文件:

public class MainActivity extends ListActivity {

     String[] itemname ={
             "Sound",
             "Screen"
             };

     Integer[] imgid ={
        R.drawable.sound,
        R.drawable.screen,
     };

     Boolean[] status ={
            false,
            true,
     };

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

        CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, status);
        setListAdapter(adapter);  

        if (status[1] == true) {
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    } 
}

当我更改布尔:状态时,在我的脚本中,交换机的值会发生变化。 此时,此代码显示一个toast,因为第二个listview-item中的开关的布尔值为:true。 但是,当我关闭我的应用程序中的开关然后再打开时,吐司不会显示。

我想这是因为当我点击开关时脚本不再重新运行。所以我想补充一下 一个onClickListener,所以当一个开关是tap时,会发生一些事情(比如吐司)。

有人可以帮我在listview中添加onClickListener吗?

1 个答案:

答案 0 :(得分:1)

在你的getView上,在你的Switch声明下面添加:

    Switch switchView = (Switch) rowView.findViewById(R.id.switch1);
    switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b)
        {
            Toast.makeText(getContext(),"Switch is" + (b?" ":" not ") + "checked",Toast.LENGTH_LONG).show();
        }
    });

只要您点击开关,就会显示Toast。