getting data from dynamically created table row containing many textviews - android

i created a table row dynamically, That contains three textviews.now i want to get the value of three textview's value on click on tablerow(tr).means i want to get the companyTV,valueTV & YearTV's value.
Thanks.
for (int i = 0; i < match.length; i++) {
/** Create a TableRow dynamically **/
tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(match[i]);
companyTV.isClickable();
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 5);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
valueTV = new TextView(this);
valueTV.setText(kanr[i]);
valueTV.isClickable();
valueTV.setTextColor(Color.RED);
valueTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
YearTV = new TextView(this);
YearTV.setText(ort[i]);
YearTV.setTextColor(Color.RED);
YearTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
YearTV.setPadding(5, 5, 5, 5);
YearTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(YearTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}

First you have one error in your code when you are setting background of table row before you have created it. You should first create new table row, and then set its background:
for (int i = 0; i < match.length; i++) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
You can access table row children views by index in your OnClickListener
tr.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableRow tr = (TableRow)v;
TextView companyTV, valueTV, yearTV;
String company, value, year;
companyTV = (TextView) tr.getChildAt(0);
valueTV = (TextView) tr.getChildAt(1);
yearTV = (TextView) tr.getChildAt(2);
company = companyTV.getText().toString();
value = valueTV.getText().toString();
year = yearTV.getText().toString();
}
});
If you want to track which table row is currently selected, you can create global variable for your activity
TableRow selectedTR;
and then set that inside OnClickListener
selectedTR = (TableRow)v;
When you use that selectedTR variable make sure that you check it for null value in case there is no selection made yet.

Related

The specified child already has a parent - TableLayout

I'm getting data from JSON to my object BalanceSheet and want to display the data in a Table layout but Im getting the following error,
The specified child already has a parent. You must call removeView() on the child's parent first.
// on this line
tableLayout.addView(data_row);
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/balance_table_SV"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.BalanceTable">
</ScrollView>
and Activity :
public class BalanceTable extends AppCompatActivity {
ScrollView balance_table_SV;
ArrayList<BalanceSheet> bsList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance_table);
balance_table_SV = findViewById(R.id.balance_table_SV);
bsList = (ArrayList<BalanceSheet>) getIntent().getSerializableExtra("list");
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams( new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableLayout.setStretchAllColumns(true);
tableLayout.removeAllViews();
int rows = bsList.size();
TableRow headers_row = new TableRow(this);
TableRow data_row = new TableRow(this);
for(int i = -1; i < rows; i ++) {
if (i == -1) {
// need check
headers_row.setLayoutParams( new TableLayout.LayoutParams());
TextView headers = new TextView(this);
headers.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
headers.setGravity(Gravity.LEFT);
headers.setPadding(5, 15, 0, 15);
headers.setText("Inv.#");
headers.setBackgroundColor(Color.parseColor("#f0f0f0"));
headers_row.addView(headers);
tableLayout.addView(headers_row);
}
else {
data_row.setLayoutParams( new TableRow.LayoutParams());
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setText(String.valueOf(bsList.get(i).getName()));
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setText(String.valueOf(bsList.get(i).getCredit()));
TextView tv3 = new TextView(this);
tv3.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3.setText(String.valueOf(bsList.get(i).getDebt()));
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setText(String.valueOf(bsList.get(i).getCredit()));
data_row.addView(tv1);
data_row.addView(tv2);
data_row.addView(tv3);
data_row.addView(tv4);
}
tableLayout.addView(data_row);
}
balance_table_SV.addView(tableLayout);
}
}
This happen cause you are trying to add the same view multiple times in TableLayout. as you are looping through all of your element you have to recreate the data_row again and again for adding it into the table layout.
else{
TableRow data_row = new TableRow(this);
data_row.setLayoutParams( new TableRow.LayoutParams());
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setText(String.valueOf(bsList.get(i).getName()));
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setText(String.valueOf(bsList.get(i).getCredit()));
TextView tv3= new TextView(this);
tv3.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3.setText(String.valueOf(bsList.get(i).getDebt()));
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setText(String.valueOf(bsList.get(i).getCredit()));
data_row.addView(tv1);
data_row.addView(tv2);
data_row.addView(tv3);
data_row.addView(tv4);
tableLayout.addView(data_row);
}
the above block will solve the problem as it will create the new object of TableRow every time and add it to TableLayout Object
Explanation:
while adding the table row second time it will generate the error cause we are trying to add the same table row which being attached to a parent in this case we can consider it as table layout which shows the row.
So Why we require to Create the new TableRow object:
A simple answer will be the same view item can not be at the multiple places.

How to set Id's for list of EditText within the dynamically created TableLayout in android

I am programmatically creating a dynamic TableLayout with a mixture of TextView and EditText. This TextViews are fetched from the sqlite database of application. The last column of the TableLayout consists of the EditText whose state is set to Invisible. I want to set the state of the EditText of the particular row of TableLayout to Visible based on the onClick over a row of TableLayout. After onClick, EditText should be visible and I want to store the entry made in that editText within the database.
My code of attempt made for this is below:
List<Details> customerDetailList = db.getAllCustomers(routeId);
Details customerDetails;
TableLayout stk = (TableLayout) findViewById(R.id.tableLayout1);
stk.removeAllViews();
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("Sl.No.");
tv0.setTextColor(Color.BLACK);
tv0.setTypeface(null, Typeface.BOLD);
//tv0.setTextSize(20);
tv0.setPadding(5, 5, 5, 5);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Patron ID ");
tv1.setTextColor(Color.BLACK);
tv1.setTypeface(null, Typeface.BOLD);
//tv1.setTextSize(20);
tv1.setPadding(5, 5, 5, 5);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Name ");
tv2.setTypeface(null, Typeface.BOLD);
//tv2.setTextSize(20);
tv2.setTextColor(Color.BLACK);
tv2.setPadding(5, 5, 5, 5);
tv2.setGravity(Gravity.CENTER);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Requested ");
tv3.setTextColor(Color.BLACK);
tv3.setTypeface(null, Typeface.BOLD);
//tv3.setTextSize(20);
tv3.setPadding(5, 5, 5, 5);
tv3.setGravity(Gravity.CENTER);
tbrow0.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText(" Supplied ");
tv4.setTypeface(null, Typeface.BOLD);
//tv4.setTextSize(20);
tv4.setTextColor(Color.BLACK);
tv4.setPadding(5, 5, 5, 5);
tbrow0.addView(tv4);
tbrow0.setBackgroundResource(R.color.colorLightGray);
stk.addView(tbrow0);
for (int row = 0, j = 1; row < customerDetailList.size(); row++, j++) {
customerDetails = customerDetailList.get(row);
TableRow tbrow = new TableRow(this);
tbrow.setClickable(true);
final TextView t1v = new TextView(this);
t1v.setText("" + j);
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
t1v.setPadding(5, 5, 5, 5);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("" + customerDetails.getcId());
t2v.setTextColor(Color.BLACK);
t2v.setGravity(Gravity.CENTER);
t2v.setPadding(5, 5, 5, 5);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("" + customerDetails.getName());
t3v.setTextColor(Color.BLACK);
t3v.setGravity(Gravity.CENTER);
t3v.setPadding(5, 5, 5, 5);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + customerDetails.getRequested());
t4v.setTextColor(Color.BLACK);
t4v.setGravity(Gravity.CENTER);
t4v.setPadding(5, 5, 5, 5);
tbrow.addView(t4v);
final EditText t5v = new EditText(this);
// t5v.setHint(0);
t5v.setId(j);
t5v.setVisibility(View.INVISIBLE);
t5v.setTextColor(Color.BLACK);
t5v.setGravity(Gravity.CENTER);
t5v.setPadding(5, 5, 5, 5);
tbrow.addView(t5v);
tbrow.setBackgroundResource(R.color.colorWhite);
tbrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr1=(TableRow)v;
TextView tv1= (TextView)tr1.getChildAt(0);
//Get the integer value of tv1 to make the corresponding edittext to be visible
t5v.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),tv1.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
stk.addView(tbrow);
}
I found similar kind of questions being asked in How to Set Id in EditText programmatically in Android
and Android: Set Edit text or text view id Programmatically wherein, solution provided to make use of editText.setId(i) where i is any integer value. but it did not help me with my situation.
You can create a custom id in an xml file and set that id using setId() method. Here are the steps to do that:
1) Create the file ids.xml under res/values directory.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="my_custom_id" type="id" />
</resources>
2) Then create your view programmatically and set its id:
EditText et = new EditText(getContext());
et.setId(R.id.my_custom_id);
That should do the trick.
EDIT: You can also do the following if you do not know the number of views to be created and cannot use id's from xml file:
et.setId(View.generateViewId());
The generateViewId() method creates a random unique id for you.
I followed the below steps to set the Id's for the programmatically created EditText and to retrieve data entered in the edittext.
Step 1: Make global declaration for edittext and int variable as
int idOfEditText;
EditText t5v;
Step 2: Setup the Id for editText as
t5v = new EditText(this);
t5v.setId(Integer.valueOf(j)); //I tried with just j being declared as t5v.setId(j); but it was showing up error with the suggestion as Expected resource of type id in android studio. I have made int variable **j** to loop till list.size()
Step 3: Get the id of the row onclick over the table using
tbrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr1=(TableRow)v;
TextView tv1= (TextView)tr1.getChildAt(0);
//Get the integer value of tv1 to make the corresponding edittext to be visible
idOfEditText = Integer.valueOf(tv1.getText().toString());
t5v = (EditText) findViewById(idOfEditText);
}
});
Step 4: The data entered in the EditText can be retrieved by
t5v = (EditText) findViewById(idOfEditText);
String value = t5v.getText().toString();
Toast.makeText(this, "You entered: "+value, Toast.LENGTH_SHORT).show();

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code
table = (TableLayout) findViewById(R.id.goodsTable);
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(getApplicationContext());
atName.setText(attrName[i]);
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
table.requestLayout();
Try This it may be help to you
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(this);
atName.setText(attrName[i]);
atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

work with dynamically created checkboxes

I create tablerows and checkboxes dynamically with a code like this:
for (Iterator i = users.iterator(); i.hasNext();) {
Users p = (Users) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a Checkbox to add to the row **/
CheckBox cb = new CheckBox(this);
cb.setText(p.getEan());
LinearLayout Ll1 = new LinearLayout(this);
Ll1.addView(cb);
System.out.println(j);
tr.addView((View)Ll1); // Adding CheckBox to tablerow.
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getName());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
After its created the user can check or uncheck.
With the check, I need to do run another command for every checkbox.
My problem is, how do I use the checkboxes? How i can "speak" with so i can work with it?
Call generateViewId() (or leave setId(i)) and use generated id in setId() for your programatically generated checkboxes. Save id's locally during your activity life and use this id's in same way as static id's from R. in setOnCheckedChangeListener.
You gave the rows and checkboxes the same id's. Don't give the rows id's. Or different ones.
You can place following code in on check change listeners.
LinearLayout my_layout = (LinearLayout)findViewById(R.id.my_layout);
for (int i = 0; i < Array_Count; i++)
{
CheckBox checkBox = (CheckBox)my_layout.findViewById(i);
if ( checkBox == null )
continue;
Toast.makeText(context, checkBox.getText(), Toast.LENGTH_SHORT).show();
checkBox.setText("my id is: " + i);
}

how to put a custom view in a table row in android?

I am creating table rows programmatically and i want to put a view that i created before in a row. Is there any way to do it?
for ( current = 0; current < rapor1.length; current++)
{
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView f1 = new TextView(this);
f1.setId(200+current);
f1.setText(rapor1[current]);
f1.setTextColor(Color.BLACK);
f1.setWidth(100);
tr.addView(f1);
DrawView dv = new DrawView(this);
tr.addView(dv);
it should be working but something is wrong.
I think it may helps. Just change this string:
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
And of course you need to set LayoutParams to any view that you want to show on your screen

Categories

Resources