【问题标题】:add a onClickListener within a listview在列表视图中添加一个 onClickListener
【发布时间】:2014-10-14 16:54:56
【问题描述】:

我想为 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();
        }
    } 
}

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

我猜这是因为当我点击开关时脚本没有重新运行。所以我想添加 一个 onClickListener,所以当一个开关被点击时,会发生一些事情(比如祝酒)。

有人可以帮我在我的列表视图中添加一个 onClickListener 吗?

【问题讨论】:

  • 检查onListItemClick
  • 我不想在单击列表项时显示 Toast,但是当列表项中的开关“切换”时

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


【解决方案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。

【讨论】:

  • 谢谢,这确实有效。但它不识别我点击的开关的位置,“声音”或“屏幕”开关
  • 在前面添加一个 if。 if (position ==1) ... else位置0是声音,1是屏幕
  • 是否可以通过一些小的改动将这段代码移动到我的 MainActivity 中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多