javaandroideclipseandroid-tablelayoutandroid-contextmenu

Get Table Row data in the ContextMenu


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!


Solution

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