在ContextMenu中获取表行数据

时间:2013-06-27 05:39:35

标签: java android eclipse android-tablelayout android-contextmenu

那么我现在已经挣扎了一段时间才想通过帖子来浏览这些内容,但我还没有看到任何可行的事情。

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

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);
    }
}

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

有任何想法和建议吗?

非常感谢帮助!

1 个答案:

答案 0 :(得分: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");