Listview与Checkbox,RadioButton,Textview和按钮在android中无法正常工作

前端之家收集整理的这篇文章主要介绍了Listview与Checkbox,RadioButton,Textview和按钮在android中无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个 Android应用程序,我的应用程序的UI在下面给出.

单击提交按钮,我需要选中复选框和单选按钮值.

未检查示例Linux,检查cc(单选按钮).

记录在列表视图中动态填充,但我无法使其正常工作.许多
有问题.

>当我滚动列表视图时,单选按钮会自动选中或取消选择,无法维持单选按钮的状态.
>单击按钮不获取所选单选按钮以及复选框.

下面是我的布局以及java程序.建议我得到正确的值.

main.xml中

<ListView
    android:id="@+id/my_list"
    android:layout_width="fill_parent"
    android:layout_height="199dp" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    />

row.xml

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:layout_toRightOf="@+id/check"
    android:textSize="20sp" >
</TextView>

<CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="4dip"
    android:layout_marginRight="10dip"
    android:focusable="false"

    android:focusableInTouchMode="false" >
</CheckBox>

 <RadioGroup
     android:id="@+id/radioSex"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:layout_alignParentRight="true"
     android:layout_alignParentTop="true"
     android:orientation="horizontal" >

     <RadioButton
         android:id="@+id/to"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="10dip"
         android:checked="true"
         android:text="To" />

     <RadioButton
         android:id="@+id/cc"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="10dip"
         android:text="CC" />
 </RadioGroup>

MyAdaptor.java

public class MyAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;

    public MyAdapter(Activity context,List<Model> list) {
        super(context,R.layout.row,list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkBox;

    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.row,null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.label);
            viewHolder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.check);

            viewHolder.checkBox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                            int getPosition = (Integer) buttonView.getTag();
                            list.get(getPosition).setSelected(
                                    buttonView.isChecked());
                        }
                    });
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.label,viewHolder.text);
            convertView.setTag(R.id.check,viewHolder.checkBox);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.checkBox.setTag(position);

        viewHolder.text.setText(list.get(position).getName());
        viewHolder.checkBox.setChecked(list.get(position).isSelected());

        return convertView;
    }

MainActivity.java

public class MainActivity extends Activity implements OnItemClickListener {

    ListView listView;
    ArrayAdapter<Model> adapter;
    List<Model> list = new ArrayList<Model>();

    private RadioGroup radioCcToGroup;
    private RadioButton radioTypeButton;
    private Button btn;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.my_list);

        btn = (Button) findViewById(R.id.submit);

        btn.setOnClickListener(new View.OnClickListener() {
            int count = 0;

            @Override
            public void onClick(View view) {


                count = listView.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    // here i am not able to get the records as getting on onItemClick of the listview
                }
            }
        });

        adapter = new MyAdapter(this,getModel());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0,View v,int position,long arg3) {
        TextView label = (TextView) v.getTag(R.id.label);
        CheckBox checkBox = (CheckBox) v.getTag(R.id.check);
        Toast.makeText(v.getContext(),label.getText().toString() + " " + isCheckedOrNot(checkBox),Toast.LENGTH_LONG).show();

    }

    private String isCheckedOrNot(CheckBox checkBox) {
        if (checkBox.isChecked())
            return "is checked";
        else
            return "is not checked";
    }

    private List<Model> getModel() {
        list.add(new Model("Linux"));
        list.add(new Model("Windows7"));
        list.add(new Model("Suse"));
        list.add(new Model("Eclipse"));
        list.add(new Model("Ubuntu"));
        list.add(new Model("Solaris"));
        list.add(new Model("Android"));
        list.add(new Model("iPhone"));
        list.add(new Model("Java"));
        list.add(new Model(".Net"));
        list.add(new Model("PHP"));
        return list;
    }

Model.java

private String name;
    private boolean selected;

    public Model(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

解决方法

你在MyAdaptor.java上有两个错误

>当您附加onCheckedChangeListener时,只有在需要创建新视图并且忘记列表视图重用视图的情况时才执行此操作.你应该在setOnCheckedChangeListener之外if(convertView == null).
>看来onCheckedChangeListener也在滚动完成时被调用(因为viewHolder.checkBox.setChecked(list.get(position).isSelected());).您可以通过在ViewHolder.checkBox上使用OnClickListener或不调用viewHolder.checkBox.setChecked()来避免这种情况.

这是我的代码

private class ViewHolder {
    protected TextView text;
    protected CheckBox checkBox;
    protected RadioGroup radioGroup;
}

public class MyAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;

    public MyAdapter(Activity context,list);
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position,null);

            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.label);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.check);
            viewHolder.checkBox.setTag(position);
            viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioSex);
            viewHolder.radioGroup.setTag(position);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.text.setText(list.get(position).getName());
        viewHolder.checkBox.setChecked(list.get(position).isSelected());
        viewHolder.checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                int getPosition = (Integer) checkBox.getTag();
                list.get(getPosition).setSelected(checkBox.isChecked());
            }
        });

        viewHolder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group,int checkedId) {
                boolean isCcOrIsTo = (checkedId == R.id.cc);
                int getPosition = (Integer) group.getTag();
                list.get(getPosition).setCcOrIsTo(isCcOrIsTo);
            }
        });

        return convertView;
    }
}

注意我也添加了对radiogroup的一些控制,因此Model.java已经改变:

public class Model {

    private String name;
    private boolean selected;
    private boolean isCcOrIsTo;

    public Model(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public boolean isCcOrIsTo() {
        return isCcOrIsTo;
    }

    public void setCcOrIsTo(boolean isCcOrIsTo) {
        this.isCcOrIsTo = isCcOrIsTo;
    }

    @Override
    public String toString() {
        String selectedString = selected ? "selected" : "not selected";
        String value = isCcOrIsTo ? "CC" : "To";
        return name+" -> "+selectedString+ " with value "+value;
    }
}

最后,您不需要在MainActivity.java上使用onItemClick,以便在单击峰值按钮时检查值是否正确:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.my_list);

    btn = (Button) findViewById(R.id.submit);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            for (Model m : list) {
                Log.i("Stack1",m.toString());
            }
        }
    });

    ArrayAdapter<Model> adapter = new MyAdapter(this,getModel());
    listView.setAdapter(adapter);
}

希望能帮助到你

猜你在找的HTML相关文章