单击列表视图项的项目时如何启用单项复选框

时间:2015-10-21 07:31:19

标签: java android

public class SingleListAdapter extends BaseAdapter {
    public Context ctx;
    LayoutInflater lInflater;
    List<String> data;

    public SingleListAdapter(Context context, List<String> data) {
        ctx = context;
        this.data = data;
        lInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.single_choice_items, parent, false);
        }

        ((TextView) view.findViewById(R.id.singleitemId)).setText(data.get(position));


        return view;
    }
}

这是我的适配器类:

String[] data = {"10-11 Am", "11-12Am", "12-2Pm", "2-4PM", "4-6Pm"};


List<String> list = new ArrayList<String>();

        for (int i = 0; i < data.length; i++) {
            list.add(data[i]);
        }

        SingleListAdapter     adapter = new SingleListAdapter(this, list);


        listview.setAdapter(adapter);



        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                valueoflist=   data[i];            



            }
        });

这是我的lsitview itemclick功能我在listview适配器中有textview和复选框我在textview中打印值,我能够获取listview项目上的特定项目的数据点击我想要点击项目列表然后该特定检查框应该启用请建议我如何做到这一点,我想要单项选中项目点击请帮我建议我们如何做到这一点。

1 个答案:

答案 0 :(得分:0)

在onItemClick方法中,您可以调用checkBox对象,然后就可以使用它了。

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            valueoflist=   data[i];            
            CheckBox chk = (CheckBox)view.findViewById(R.id.your_check_box_id);
            chk.setChecked(true);
        }
    });

我希望它能帮到你

试试这个:

listview.setOnItemClickListener(new OnMyItemClickListener());

并声明:

private class OnMyItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
            valueoflist=   data[i];            
            CheckBox chk = (CheckBox)view.findViewById(R.id.your_check_box_id);
            chk.setChecked(true);
    }
}

但是..一个问题..你能显示listview初始化吗?