Android ListView确定Text或CheckMark点击

时间:2013-03-24 11:28:24

标签: android listview checkbox checkedtextview

标题已经说明了。
我有一个带有自定义适配器的列表视图,这些行只包含一个TextView和一个CheckBox 我需要确定用户是否点击了checkMark或文本,因此我可以显示描述对话框,说明他将要检查的内容!

在我的适配器中,我正在设置一个OnClickListener,如下所示:

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

    if(row==null){
        LayoutInflater inflater=getLayoutInflater();
        row=inflater.inflate(R.layout.entsorgung_list_item, parent, false);
    }

   // CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.ctvEntsorgung);
   // checkedTextView.setText(bedarfsList.get(position));

    TextView itemText = (TextView)row.findViewById(R.id.tvEntdorgItem);
    CheckBox itemCheck = (CheckBox)row.findViewById(R.id.cbEntsorgItem);

    itemCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("TRACE","check click pos "); 
            // here i need the ListView position thats been clicked
        }
    });

    itemText.setText(bedarfsList.get(position));

    Boolean checked = myChecked.get(position);
    if (checked != null) {
       itemCheck.setChecked(checked);
    }

    return row;
}

CheckBox的焦点设置为false 在我的Activity中,我有一个OnItemClickListener,它适用于文本点击,我只需要我的复选框。

1 个答案:

答案 0 :(得分:0)

但您已从CheckBox方法获得getView的位置,请使用:

position

从:

public View getView(int position, View convertView, ViewGroup parent)

像这样:

itemCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("TRACE","check click pos "); 
            Boolean checked = myChecked.get(position);
        }
    });

编辑:查看我用于非常类似情况的以下适配器:

    private class MySecondAdapter extends ArrayAdapter<MiniTask>
{   
    private ArrayList<MiniTask> list;

    public MySecondAdapter(Context context, int textViewResourceId, ArrayList<MiniTask> miniTaskList) 
    {
        super(context, textViewResourceId, miniTaskList);
         this.list = new ArrayList<MiniTask>();
         this.list.addAll(miniTaskList);
    }
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        miniTask = miniTaskList.get(position);
        ViewHolder holder = new ViewHolder();
        {
            LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.check_list_item_new, null);

            holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle);
            holder.commentsPicturesButton = (ImageView) convertView.findViewById(R.id.iAddCommetOrPicture);
            holder.commentsPicturesButton.setTag(position);
            holder.commentsPicturesButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) 
                {
                     Intent intent = new Intent(getApplicationContext(), PicturesAndCommentsActivity.class);
                     intent.putExtra(TasksListActivity.KEY_ID, task.getId());
                     intent.putExtra("mini_task_text", miniTask.getTitle());
                     startActivity(intent);
                }
            });
            holder.selected = (CheckBox) convertView.findViewById(R.id.cbCheckListItem);
            holder.selected.setTag(position);
            holder.selected.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v)
                {    
                    {                               
                        Log.d(TAG, "pressed the checkbox: " + v.getId() + " in position: " + position + " tag: " +v.getTag() +" and item from array: " + miniTaskList.get(position) );
                        CheckBox checkbox = (CheckBox) v;
                        miniTaskList.get(position).setSelected(checkbox.isChecked());   
                        numOfCheckedMiniTasks = 0;
                        for(int i=0;i<miniTaskList.size();i++)
                        {
                             miniTask = miniTaskList.get(i);
                             if(miniTask.isSelected())
                             {
                                numOfCheckedMiniTasks ++;
                             }
                        }
                        int percent = (int)(numOfCheckedMiniTasks * 100.0f) / miniTaskList.size();
                        Log.d(TAG, "the percentage is: " +percent);
                        tasksRepository.get(tasksRepository.indexOf(task)).setMiniTasksPercentageComplete(percent);
                    }
                }
            });

        }
        holder.title.setText(miniTask.getTitle());
        holder.selected.setChecked(miniTask.isSelected());
        return convertView;
    }
}

static class ViewHolder 
{
     TextView title;
     CheckBox selected;
     ImageView commentsPicturesButton;
}