List view implementation with text view and toggle button i=

試著忘記壹切 提交于 2019-12-04 15:58:06

问题


Here in my application I want to create the list view with toggle button and text view. when I am clicking on the list item I want to go next Activity. But when I am clicking on the Toogle button I need to display it is in off mode or on mode. below I add my code.

$ This is my main activity

public class MainActivity extends Activity {

    int startminute;
    int endminute;

    Date date;
    ToggleButton togg;
    ListView lv;
    String[] days = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
            "FRIDAY", "SATURDAY" };
    boolean[] onOff = new boolean[] { false, false, false, false, false, false,
            false };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        if (savedInstanceState != null) {
            onOff = savedInstanceState.getBooleanArray("status");
        }
        lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(new MyAdapter());
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                Intent in = new Intent(MainActivity.this, StartAndEndTime.class);
                in.putExtra("position", position);
                startActivity(in);
            }
        });
    }

    public class MyAdapter extends BaseAdapter {

        public int getCount() {
            return days.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {

            return position;
        }

        @Override
        public int getViewTypeCount() {
            return days.length;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View v = null;
            TextView arryText;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.inflate, null);

                arryText = (TextView) v.findViewById(R.id.inflateText);
                togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
                v.setTag(new ViewHolder(arryText, togg));

                togg.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {

                        if (togg.isChecked()) {
                            togg.setChecked(false);
                            onOff[position] = false;

                            Toast.makeText(MainActivity.this, "is off",
                                    Toast.LENGTH_SHORT).show();

                        } else {

                            onOff[position] = true;
                            togg.setChecked(true);
                            Toast.makeText(MainActivity.this, "is on",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                arryText.setText(days[position]);
                togg.setChecked(onOff[position]);
            }

            return v;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBooleanArray("status", onOff);
}

回答1:


change your getView method in this way...

public View getView(final int position, View convertView,
        ViewGroup parent) {
    View v = null;
    TextView arryText;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.inflate, null);
    }
        arryText = (TextView) v.findViewById(R.id.inflateText);
        togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
        v.setTag(new ViewHolder(arryText, togg));

        togg.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    if (onOff[position]) {
                        togg.setChecked(false);
                        onOff[position] = false;

                        Toast.makeText(MainActivity.this, "is off",
                                Toast.LENGTH_SHORT).show();

                    } else {

                        onOff[position] = true;
                        togg.setChecked(true);
                        Toast.makeText(MainActivity.this, "is on",
                                Toast.LENGTH_SHORT).show();

                    }

                }
            });

        arryText.setText(days[position]);
        togg.setChecked(onOff[position]);



    return v;
}



回答2:


Do like this:

In constructor: pass the context from activity to the Adapter class and use the same context for LayoutInflater:

mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

In getView():

View v = null;

if (convertView == null) {
    v = mInflater.inflate(R.layout.demo_list, parent, false);
} else {
    v = convertView;
}

You got the view(v) now, add views/listeners to it and then return!

return v;




回答3:


Problem can be with position. Try this code:

public View getView(final int position, View convertView,
            ViewGroup parent) {
        View v = null;
        TextView arryText;
        final int rowPosition = position;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.inflate, null);

            arryText = (TextView) v.findViewById(R.id.inflateText);
            togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
            v.setTag(new ViewHolder(arryText, togg));


            togg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Save the state here
            onOff[rowPosition]= isChecked;
              Toast.makeText(MainActivity.this, isChecked ? "is on" : "is off",
                                Toast.LENGTH_SHORT).show();
            }
            });


            arryText.setText(days[rowPosition]);
            togg.setChecked(onOff[rowPosition]);

        }

        return v;
    }


来源:https://stackoverflow.com/questions/13116425/list-view-implementation-with-text-view-and-toggle-button-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!