Get Table Row data in the ContextMenu

我们两清 提交于 2021-01-29 08:28:35

问题


Well so I've been struggling for a while now to figure this out browsing through posts but I haven't yet come across anything viable.

This is a part of my code for the table:

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

    }

And the context menu:

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

So basically I need to get the values in textviews in the selected (long clicked & context menu opened) table row.

Any ideas and suggestions?

Help is much appreciated!


回答1:


I figured it out actually, so here's the new code for the table:

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

}

& then to retrieve the intent from where ever you want of course, just the usual basic way to retrieve any intent:

Intent intent = getIntent();

     final String flightNumber = intent.getStringExtra("flightNumber");
     final String flightInfo = intent.getStringExtra("flightInfo");


来源:https://stackoverflow.com/questions/17335293/get-table-row-data-in-the-contextmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!