【问题标题】:How to Set ID's on programmatically created checkboxes?如何在以编程方式创建的复选框上设置 ID?
【发布时间】:2017-02-11 00:45:25
【问题描述】:

我正在开发一个帮助我跟踪营养的小型应用程序。现在,我使用适配器构建了一个布局。为此,我使用了 SimpleCursorAdapter,因为所有需要的数据都来自我的数据库。一切都很好,除了一件事:

所有复选框都有相同的 id。所以我无法检查它们中的每一个。有一个解决方案我知道如何解决,但在这种情况下,我不能使用 xml 文件来生成 id,因为我不知道下次运行应用程序时会有多少个复选框。

这是我的代码:

public void getIdAndDate()
{
    db = new DATABASE(this);

    SQLiteDatabase sqldb = db.getReadableDatabase();

    String[] Columns = {"_id", "date"};
    int[] toViews = {R.id.entry_id, R.id.entry_date};

    Cursor cursor1;

    cursor1 = sqldb.query(db.TABLE_DAILY_CONSUMPTION, Columns, null, null, null, null, null);

    numRows = cursor1.getCount();

    int id[] = new int[numRows];
    String date[] = new String[numRows];

    for(int i = 0; i<numRows; i++)
    {
        cursor1.moveToNext();
        id[i] = cursor1.getInt(0);
        date[i] = cursor1.getString(1);
    }

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.layout_entry, cursor1, Columns, toViews, 0);
    entries.setAdapter(adapter);
}

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/entry_id"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/entry_date"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="20sp"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/entry_selection"
    android:layout_gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    />

我真的很想弄清楚如何解决这个问题,但我什至无法想象我有哪些可能性。

【问题讨论】:

标签: android checkbox views simplecursoradapter


【解决方案1】:

为了将Id动态设置到任何视图,首先在你的资源文件中定义一个id:

<resources>
    <item name="custom_id" type="id" />
</resources>

然后在你的代码中:

view.setId(R.id.custom_id);

【讨论】:

  • 在某些情况下,此解决方案有效,但在这种情况下,我无法使用 xml 文件生成 id,因为我不知道下次运行应用程序时会有多少复选框。
【解决方案2】:

问题解决了。我不再使用复选框。相反,我正在使用我已经拥有的条目并使用 setOnItemClickListener 并创建一个新函数来处理选择。新代码:

public void getIdAndDate()
{
    db = new DATABASE(this);

    SQLiteDatabase sqldb = db.getReadableDatabase();

    String[] Columns = {"_id", "date"};
    int[] toViews = {R.id.entry_id, R.id.entry_date};

    Cursor cursor1;

    cursor1 = sqldb.query(db.TABLE_DAILY_CONSUMPTION, Columns, null, null, null, null, null);

    numRows = cursor1.getCount();

    int id[] = new int[numRows];
    String date[] = new String[numRows];

    for(int i = 0; i<numRows; i++)
    {
        cursor1.moveToNext();
        id[i] = cursor1.getInt(0);
        date[i] = cursor1.getString(1);
    }

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.layout_entry, cursor1, Columns, toViews, 0);
    entries.setAdapter(adapter);
    entries.setOnItemClickListener(mMessageClickedHandler);
}

private AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener()
{
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
        selections(v, (int)id);
    }
};

public void selections(View view, int id)
{
    if(Counter < 7)
    {
        if(selections[0] == id || selections[1] == id || selections[2] == id || selections[3] == id || selections[4] == id || selections[5] == id || selections[6] == id)
        {
            MESSAGE.message(db.context, "Diese Auswahl hast du bereits getroffen!");
        }
        else
        {
            selections[Counter] = id;
            view.setBackgroundColor(0xAACCCCCC);
            Counter++;
        }
    }
    else
    {
        MESSAGE.message(db.context, "Du hast die maximal mögliche Anzahl an Auswahl getroffen!");
    }
}

【讨论】:

    猜你喜欢
    • 2018-01-23
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2018-05-27
    • 2011-08-23
    相关资源
    最近更新 更多