0

我为具有复选框和文本项的列表视图实现了一个自定义适配器。我可以通过我的覆盖参数获得位置。但是如何获取我的列表行的 id 呢?

以下是自定义适配器的代码 -

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.reminder_row, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) rowView.findViewById(R.id.reminderRowTextId);
            viewHolder.reminderCheckBox = (CheckBox) rowView.findViewById(R.id.CheckBoxId);
            rowView.setTag(viewHolder);
        }

        final int pos = position;
        final ViewHolder holder = (ViewHolder) rowView.getTag();
        int idRow = holder.text.getId();
        Log.i(TAG, "id of item selected-->" + idRow); <<<<<<------- IT IS GIVING SOME VALUE LIKE 2030771-------->>>
        String s = names[position];
        holder.text.setText(s);

        holder.reminderCheckBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.reminderCheckBox.isChecked()) {
                    Toast.makeText(getContext(), "pos-->chkd" + pos, Toast.LENGTH_SHORT).show();
                    long longPos = (long)pos;
                    dbHelper.completeTask(longPos + 1);

                } else {
                    Toast.makeText(getContext(), "pos-->un--chkd" + pos, Toast.LENGTH_SHORT).show();
                }
            }
        });

        holder.text.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "TEXT CLICKED" + pos , Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(context,ReminderModificationActivity.class);
                long longPos = (long)pos;
                intent.putExtra(TasksDBAdapter.KEY_ROWID, longPos + 1);
                Log.i(TAG, "row clickd --> " + longPos);
                ((Activity) context).startActivityForResult(intent, ACTIVITY_EDIT);
            }
        });
        return rowView;
    }

编辑 1.1

public static final String KEY_TITLE = "title";                  
    public static final String KEY_BODY = "body";
    public static final String KEY_DATE_TIME = "reminder_date_time"; 
    public static final String KEY_ROWID = "_id";                
    public static final String KEY_IS_COMPLETE = "is_complete";
    private static final String TAG = "TasksDBAdapter";

private static final String DATABASE_CREATE =                        
           "create table " + DATABASE_TABLE + " ("
                   + KEY_ROWID + " integer primary key autoincrement, "
                   + KEY_IS_COMPLETE + " boolean default 'false', "
                   + KEY_TITLE + " text not null, " 
                   + KEY_BODY + " text , " 
                   + KEY_DATE_TIME + " text);"; 

请看一下标记线。任何人都可以帮助我如何获得身份证。我正在学习android,请详细说明。在此先感谢,雷

4

1 回答 1

1

您正在调用getId()a TextView根据文档返回与属性关联的值android:id。该值是自动生成的,可能与R.id.reminderRowTextId.

基本上,您实际上并没有在ViewHolder课堂上设置有意义的 ID。只需添加另一个成员变量,然后存储您需要的任何内容。

详细地说,适配器用于描述后备列表/数组的大小,并为每个元素提供一个视图。要正确执行此操作,您需要实现getView()and getItem()

getItem()接受一个位置,它是 的一个参数getView()。传递positiongetItem(),这将为您提供 SQLite 中的项目。获取该对象的 ID,并在ViewHolder.

于 2012-11-16T16:28:34.737 回答