i have table layout in which is as rows added diferent view (in which are buttons another table views and...) multiple times based on data which i input... but each row should be movable or deletable by buttons in that view.
so my question is: how can I reach these buttons and set on them onclick listeners which will know where this click came from for me to be able to do something with that row based on button from anodher xml
that table what i tallking about looks like this:
<ScrollView
android:id="@+id/main_orders_scroll_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toStartOf="@+id/middle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/header"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/middle"
app:layout_constraintVertical_bias="0.0" >
<TableLayout
android:id="@+id/main_orders_table"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="@+id/main_orders_row"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/myid1"
layout="@layout/order" />
</TableRow>
<TableRow
android:id="@+id/main_orders_rows"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
</TableLayout>
</ScrollView>
and it including another view where are buttons which are supposed to do something with that row
or is it cleaner to do this one diferently ?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.order_X_btn);
orderTable = (TableLayout) findViewById(R.id.main_orders_table);
order = (TableRow) findViewById(R.id.main_orders_rows);
//final ViewGroup orders = (ViewGroup) orderTable;
plus = (TextView) findViewById(R.id.plus);
plus.setOnClickListener(this);
button.setOnClickListener(this);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
getSupportActionBar().hide();
//orderTable.addView(order);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onBackPressed() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.order_X_btn:
deleteOrder(v);
break;
case R.id.plus:
addOrder();
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
private void addOrder() {
last++;
final View extend = LayoutInflater.from(this).inflate(R.layout.order,null);
extend.setTag(orderTable.getChildAt(last));
button.setTag(extend);
button.setOnClickListener(this);
//deleteButton.setOnClickListener(new View.OnClickListener);
orderTable.addView(extend);
}
private void deleteOrder(View v) {
last--;
//final View extend = LayoutInflater.from(this).inflate(R.layout.order,null);
orderTable.removeView(v);
}
Note: the code below is untested, just giving you a hint. Assuming you have some sort of plus button to add a new row and every single row has its own delete button.
On create method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//no idea what is order_X_btn, but I assume that's delete row button
// if so then you don't need it here
//button = (Button) findViewById(R.id.order_X_btn);
orderTable = (TableLayout) findViewById(R.id.main_orders_table);
order = (TableRow) findViewById(R.id.main_orders_rows);
//final ViewGroup orders = (ViewGroup) orderTable;
plus = (TextView) findViewById(R.id.plus);
plus.setOnClickListener(this);
//this is not needed here either
//button.setOnClickListener(this);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
getSupportActionBar().hide();
//orderTable.addView(order);
}
Then onClick:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.plus:
addOrder();
break;
}
}
Add order:
private void addOrder() {
last++;
final View extend = LayoutInflater.from(this).inflate(R.layout.order,null);
extend.setTag(orderTable.getChildAt(last));
Button deleteButton = (Button) extend.findViewById(R.id.order_X_btn);
deleteButton.setTag(extend);
// set anonymous on click listener which triggers deleteOrder method
deleteButton.setOnClickListener(v -> deleteOrder(v));
orderTable.addView(extend);
}
Delete order:
private void deleteOrder(View v) {
last--;
orderTable.removeView((View) v.getTag());
}
There might be some mistakes as I don't have IDE atm, but I hope you got the idea.