【问题标题】:to get one value of the column from the table layout after the user clicks on the row在用户单击该行后从表格布局中获取该列的一个值
【发布时间】:2013-05-01 16:28:36
【问题描述】:

我正在创建一个 android 应用程序,其中 m 在表格布局中显示数据库中的行。 在这些行中,我显示了数据库中的行 ID 和两列。 我在行上放置了一个点击事件。 我现在想要的是获取用户单击的特定行的 id 值,然后根据该行 id 显示数据库中的所有详细信息... 我知道通过 select 语句从数据库中显示的部分。 m 无法在 onclick 事件中捕获行 ID... 我怎么做???任何人有任何想法请帮助! 这是在行中显示详细信息的代码...

Cursor cursor=database.rawQuery("select * from "+AllValuesTable+" ", null);
        Integer index0=cursor.getColumnIndex("ROWID");
        Integer index1 = cursor.getColumnIndex("appln_no"); 
        Integer index2 = cursor.getColumnIndex("app_fname");

        if(cursor.getCount()>0)
        {
            cursor.moveToFirst();
            do
            {
                row=new TableRow(this);
                row.setId(100);

                row.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

                //setting the first column
                firstCol=new TextView(this);
                firstCol.setText(cursor.getString(index0));
                rowid=cursor.getInt(index0);
                firstCol.setTextSize(16);
                firstCol.setTextColor(Color.BLACK);
                firstCol.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                row.addView(firstCol); //adding coloumn to row

            //  Setting up the second coloumn parameters            
                secondCol=new TextView(this);
                secondCol.setText(cursor.getString(index1));
                secondCol.setTextColor(Color.BLACK);
                secondCol.setTextSize(16);
                secondCol.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                row.addView(secondCol); //adding coloumn to row

            //  Setting up the third coloumn parameters
                thirdCol=new TextView(this);
                thirdCol.setText(cursor.getString(index2));
                thirdCol.setTextColor(Color.BLACK);
                thirdCol.setTextSize(16);
                thirdCol.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                row.addView(thirdCol);

【问题讨论】:

    标签: android onclick rows tablelayout


    【解决方案1】:

    你可以设置row_id的标签

    row.setTag(your row id);
    

    然后在表格行上设置点击监听,在onClick()方法中获取选中行的标签:

    row.setOnClickListener(new OnClickListener()
    {
        int row_id = (Integer) row.getTag();
        Toast.makeText(getBaseContext(), row_id+"", Toast.LENGTH_SHORT).show();
    });
    

    试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多