0

我在我的表中动态地从 mssql 获取数据。我想将选定行中的值作为字符串获取。例如,我想为此屏幕获取 "99" 、 "Yonca" 、 "Lodi" 值。我怎样才能做到这一点?这是我的创建表代码:

public void addData()
{
    for(int i = 0 ; i < protokolNo.length ; i++)
    {
        /**
         * Creating table row here
         */
        tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        tVProtokolNo = new TextView (this);
        tVProtokolNo.setText(protokolNo[i]);
        tVProtokolNo.setTextColor(Color.RED);
        tVProtokolNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tVProtokolNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tVProtokolNo.setPadding(5, 5, 5, 5);
        tr.addView(tVProtokolNo);

        TextView tVAdi = new TextView(this);
        tVAdi.setText(adi[i]);
        tVAdi.setTextColor(Color.RED);
        tVAdi.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tVAdi.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tVAdi.setPadding(5, 5, 5, 5);
        tr.addView(tVAdi);

        TextView tVSoyadi = new TextView(this);
        tVSoyadi.setText(soyadi[i]);
        tVSoyadi.setTextColor(Color.RED);
        tVSoyadi.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tVSoyadi.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tVSoyadi.setPadding(5, 5, 5, 5);
        tr.addView(tVSoyadi);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        /**
         *  Colouring selected row
         */

        tr.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    v.setBackgroundColor(Color.DKGRAY);
                    v.showContextMenu();


                }
            });
            registerForContextMenu(tr);
    }
}
4

2 回答 2

1

最好使用带有自定义适配器的 Android ListView,参考下面的链接.... http://www.vogella.de/articles/AndroidListView/article.html

于 2012-01-30T14:25:54.030 回答
0

这很脏但有效。在调用的 Activity 类中创建一个全局私有变量,TableRow tr1=null; 然后您可以在调用addData()之前在方法 中使用以下代码registerForContextMenu(tr);

   tr.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        view.setBackgroundColor(Color.DKGRAY);
    }
    });

    tr.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                tr1=(TableRow)v; //assign selected TableRow gobally
                openContextMenu(v);
                return true;
            }
        });

然后在同一个活动中使用以下内容,使其与上下文菜单一起工作,就我从你的代码中得到的而言:

  @Override
     public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
         menu.add(0, v.getId(), 0, "Do YourStuff");

     }
    @Override
       public boolean onContextItemSelected(MenuItem item) {
        int ccount= (tr1).getChildCount();
                          String[] str =new String[ccount];
                          for(int i=0;i<ccount;i++)
                          {
                              TextView tv =   (TextView)(((TableRow)tr1)).getChildAt(i);
                              str[i]=tv.getText().toString(); //set selected text data into the String array
                          }
                          Toast.makeText(ListActivity2.this,Arrays.toString(str), 2).show();
        return true;
    }

所以基本上 tr1 变量将托管您选择的 TableRow。

但是,正如每个人所说的那样,将 ListView 用于像这样的任务,而对行或任何东西使用 RelativeLayouts。

希望它能澄清它。

于 2012-02-06T14:17:37.507 回答