简体   繁体   中英

add a onClickListener within a listview

I want to make a settings activity for an Android application. Therefor I want to make a custom listview with an image, a title and a switch (on/off). To make this happen I have made two xml layout files: activity_main.xml (includes listview) and mylist.xml (includes an ImageView (id=icon), TextView(id=item) and Switch(id=switch1))

Besides that, I have made a CustomListAdapter.java file:

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;

     };
}

and a MainActivity.java file:

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

When I change the Boolean: status, inside my script the value of the switch change. And at this moment, this code displays a toast, because the boolean of the switch in the second listview-item is: true. But when I tap the switch inside my application off and then on again, the toast doesn't display.

I guess it is because the script doesn't re-run again when I tap the switch. So I want to add an onClickListener, so when a switch is tapt, something happens (like a toast).

Can someone help me add a onClickListener within my listview?

On your getView, just bellow your Switch declaration add this:

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

This will display a Toast whenever you click on your switch.

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