繁体   English   中英

Android提供线性布局标签

[英]Android give Linear Layout tag

我试图从我的数据库中获取一些数据,并为每一行数据创建一个新的线性布局。 唯一的问题是,我无法弄清楚如何给每个创建的LinearLayouts一个id,并使用该id,以便在单击布局时,我知道它是哪一个,例如第四个或第一个。 当我尝试这个时,我得到一个IndexOutOfBoundException。 我正在使用set和getTag,我正在使用id的计数器变量:

// Fill data
    db.open();
    // Get all of the rows from the database and create the item list
    Cursor mNotesCursor = db.fetchAllNotes();
    this.startManagingCursor(mNotesCursor);

    int counter = 0;

    while (mNotesCursor.moveToNext()) {
        String titleText = (mNotesCursor.getString(mNotesCursor
                .getColumnIndex("title")));

        String bodyText = mNotesCursor.getString(mNotesCursor
                .getColumnIndex("body"));

        String dateText = mNotesCursor.getString(mNotesCursor
                .getColumnIndex("date"));

        final LinearLayout cardsLayout = new LinearLayout(this);
        cardsLayout.setOrientation(LinearLayout.VERTICAL);
        cardsLayout.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.card_container_background));
        cardsLayout.setTag(counter);

        LinearLayout.LayoutParams layouLinearParams = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        RelativeLayout titleLayout = new RelativeLayout(this);
        titleLayout.setPadding(4, 4, 4, 4);

        // Title text
        TextView tvTitle = new TextView(this);
        tvTitle.setText(titleText);

        // Text Font Attributes
        tvTitle.setTextColor(getResources().getColor(R.color.textColor));
        tvTitle.setTypeface(robotoLight);
        tvTitle.setTextSize(25);

        titleLayout.addView(tvTitle);
        titleLayout.setLayoutParams(layouLinearParams);

        RelativeLayout.LayoutParams titleRelativeParams = (RelativeLayout.LayoutParams) tvTitle
                .getLayoutParams();
        titleRelativeParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        tvTitle.setLayoutParams(titleRelativeParams);

        // Date text
        TextView tvDate = new TextView(this);
        tvDate.setText(dateText);

        // Text Font Attributes
        tvDate.setTextColor(getResources().getColor(R.color.textColor));
        tvDate.setTypeface(robotoLight);
        tvDate.setTextSize(15);

        titleLayout.addView(tvDate);

        RelativeLayout.LayoutParams dateRelativeParams = (RelativeLayout.LayoutParams) tvDate
                .getLayoutParams();
        dateRelativeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        tvDate.setLayoutParams(dateRelativeParams);

        // Body Text
        TextView tvBody = new TextView(this);
        tvBody.setText(bodyText);

        // Text Font Attributes
        tvBody.setTypeface(robotoLight);
        tvTitle.setTextColor(getResources().getColor(R.color.textColor));
        tvBody.setTextSize(20);

        tvBody.setPadding(4, 4, 4, 4);

        cardsLayout.addView(titleLayout);
        cardsLayout.addView(tvBody);
        root.addView(cardsLayout);

        cardsLayout.setOnClickListener( new View.OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(NotesListActivity.this, NoteEdit.class);
                i.putExtra(NotesDbAdapter.KEY_ROWID, (Integer)cardsLayout.getTag());
                startActivityForResult(i, ACTIVITY_EDIT);
            }
        });

    }
    this.stopManagingCursor(mNotesCursor);
    db.close();

}

在将元素从xml布局映射到Java对象(即使用findViewById)时,布局/复合/视图ID非常有用,但是当您使用Java创建对象时,我会坚持使用Java构造而不是Android布局特定的概念。 一种方法是实现OnClickListener并添加您希望在用户单击时可用的数据。

例如,创建此类:(可以是活动内部的内部类或静态类)

private class CardClickListener implements View.OnClickListener {
    private int id;
    CardClickListener(int id) {
        this.id = id;
    }
    public void onClick(View v) {
        Log.d("XYZ", "My id is: " + id);

        Intent i = new Intent(NotesListActivity.this, NoteEdit.class);
        // i.putExtra(NotesDbAdapter.KEY_ROWID, (Integer)cardsLayout.getTag());
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }
}

然后在设置onClickListener时,请执行以下操作:

cardsLayout.setOnClickListener(new CardClickListener(counter) );

CardClickListener在其私有成员变量id中跟踪您的id。 然后,您可以在onClick方法中使用它。

增加将标记设置为线性布局的计数器值。 从游标中获取coloum索引导致异常的索引限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM