I am creating a table with dynamic custom table rows. I need to get the id in the activity class.
main_Activity–>MyTableLayoutView–>MyTableRow
My Question is, how do i get the id of the cell that was clicked in the table (TextViews) to the main_Activiy.
MyTableRowView:
public void addRow(String[] data, int[] rowId) {
for (int i = 0; i < data.length; i++) {
TextView tv = parseTextView(data[i]);
tv.setId(rowId[i]);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv2 = (TextView) v;
clickedText = tv2.getText().toString();
Debug.debugMsg(clickedText + " " + tv2.getId());
}
});
this.addView(tv);
}
}
As far as I can get is getting the id from the TablerowView class, but I need it to be in Activity class, Please if anyone can enlighten me. Thanks in advance.
Activity class:
private void showTables() {
db.open();
db.importDb();// TODO
table = new TableLayoutView(this,Converter.toArrayListStringArray(db.getDbTablesForChoose()));
table.addDataListArray(Converter.toArrayListStringArray(db.getDbTablesForChoose()),true);
llChooseSQLTable.addView(table);
}
TableLayoutView:
public void addDataListArray(ArrayList<String[]> data, boolean header) {
for (int i = 0; i < ROW_NUMBERS; i++) {
TableRowView tableRow = new TableRowView(context);
tableRow.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tableRow.addRow(data.get(i),idHandler.getNextIdsRow());//idHandles passes unique id.
this.addView(tableRow);
}
}
It seems like you are able to get the ID from your code above. If your primary concern is propagating that id from the view back up to the parent class then you could do it in multiple ways. One way is to get the context of the view then cast it to the specific activity and call a function within that activity, e.g. within the onClick() operation:
Where your activity is called MyActivity and you implement a method called insertNotificationmethod which takes an integer. This would work only if this view is always within this function, and even then it is a rather crude way to do it.
You could also just directly call it within the onClick() method using:
However, this way of doing things would limit this view to only be useful within this particular activity.
For a more generic way, you could BroadcastReceivers which may be a little more complicated that you would like. For more information about those you could check out the Android documentation here: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Hopefully that helps.