列表视图实现与文本视图和切换按钮i =

时间:2012-10-29 05:42:09

标签: android list android-layout android-listview listitem

在我的应用程序中,我想创建带有切换按钮和文本视图的列表视图。当我点击列表项时,我想去下一个Activity。但是,当我点击Toogle按钮时,我需要显示它处于关闭模式或模式。下面我添加我的代码。

$ 这是我的主要活动

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

3 个答案:

答案 0 :(得分:1)

以这种方式更改getView方法......

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

答案 1 :(得分:0)

这样做:

在构造函数中:将上下文从activity传递给Adapter类,并对LayoutInflater使用相同的上下文:

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

在getView()中:

View v = null;

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

你现在得到了视图(v),向它添加了视图/监听器,然后返回!

返回v;

答案 2 :(得分:0)

问题可以与位置有关。试试这段代码:

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