0

标题已经说了。
我有一个带有自定义适配器的列表视图,行只包含一个 TextView 和一个 CheckBox。
我需要确定用户是单击复选标记还是文本,所以我可以显示一个描述对话框来解释他将要检查的内容!

在我的适配器中,我正在设置一个 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。
在我的活动中,我有一个 OnItemClickListener,它适用于文本点击,我的复选框只需要同样的。

4

1 回答 1

0

但是您已经CheckBoxgetView方法中收到了位置,因此请使用:

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;
}
于 2013-03-24T12:38:23.287 回答