0

好吧,所以我一直在努力通过浏览帖子来解决这个问题,但我还没有遇到任何可行的东西。

这是我的表格代码的一部分:

TableLayout flightInfoTable = (TableLayout) findViewById(R.id.flightInfoTable);
    flightInfoTable.setStretchAllColumns(true);
    flightInfoTable.setShrinkAllColumns(true);

for (int i = 16; i < flightInfoArrayLenght - 1; i++) {

TableRow rowFlightInfo = new TableRow(this);
        rowFlightInfo.setGravity(Gravity.CENTER);
        rowFlightInfo.setPadding(5, 10, 5, 10);
        TableRow.LayoutParams params = new TableRow.LayoutParams();
        params.width = -2;
        rowFlightInfo.setLongClickable(true);
        registerForContextMenu(rowFlightInfo);

TextView tvTerminal = new TextView(this);
        tvTerminal.setGravity(Gravity.CENTER);
        tvTerminal.setText(flightInfoArray[i][6]);
        rowFlightInfo.addView(tvTerminal, params);

etc.. etc..

flightInfoTable.addView(rowFlightInfo);

    }

和上下文菜单:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
        menu.setHeaderTitle(".....");
        inflater.inflate(R.menu.context_startmenu, menu);
    }
}


@Override
public boolean onContextItemSelected(MenuItem item) {

    switch (item.getItemId()) {

case R.id.contextmenu_option1:
        //stuff
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

所以基本上我需要在选定的(长按并打开上下文菜单)表行中获取文本视图中的值。

有什么想法和建议吗?

非常感谢您的帮助!

4

1 回答 1

0

我实际上想通了,所以这是表格的新代码:

TableLayout flightInfoTable = (TableLayout) findViewById(R.id.flightInfoTable);
flightInfoTable.setStretchAllColumns(true);
flightInfoTable.setShrinkAllColumns(true);

for (int i = 16; i < flightInfoArrayLenght - 1; i++) {

TableRow rowFlightInfo = new TableRow(this);
    rowFlightInfo.setGravity(Gravity.CENTER);
    rowFlightInfo.setPadding(5, 10, 5, 10);
    TableRow.LayoutParams params = new TableRow.LayoutParams();
    params.width = -2;
    rowFlightInfo.setLongClickable(true);
    //registerForContextMenu(rowFlightInfo);

TextView tvTerminal = new TextView(this);
    tvTerminal.setGravity(Gravity.CENTER);
    tvTerminal.setText(flightInfoArray[i][6]);
    rowFlightInfo.addView(tvTerminal, params);

etc.. etc..

flightInfoTable.addView(rowFlightInfo);

rowFlightInfo.setOnLongClickListener(new OnLongClickListener() {

                 @Override
                        public boolean onLongClick(View v) {
                          String flightNumber = tvFlight.getText().toString();
                          flightNumber = flightNumber.replaceAll("\\s", "");
                          setIntent(getIntent().putExtra("flightNumber", flightNumber));
                          setIntent(getIntent().putExtra("flightInfo", "From " + tvFrom.getText() + " to Colombo"));
                          registerForContextMenu(v);
                          openContextMenu(v);
                          unregisterForContextMenu(v);
                          return true;
                        }
                      });

}

& 然后从你想要的任何地方检索意图,这只是检索任何意图的常用基本方法:

Intent intent = getIntent();

     final String flightNumber = intent.getStringExtra("flightNumber");
     final String flightInfo = intent.getStringExtra("flightInfo");
于 2013-07-05T18:49:10.367 回答