I have table row inside table layout, I need to read all row and move data to sql. How i read table by row next row need to read row one and go to row 2 and read
code for insert into table row :
TableLayout l1 = (TableLayout) findViewById(R.id.table1);
l1.setStretchAllColumns(true);
l1.bringToFront();
TableRow tr0 = new TableRow(getBaseContext());
TextView tv1 = new TextView(getBaseContext());
TextView tv2 = new TextView(getBaseContext());
TextView tv3 = new TextView(getBaseContext());
TextView tv4 = new TextView(getBaseContext());
TextView tv5 = new TextView(getBaseContext());
tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.BLACK);
tv4.setTextColor(Color.BLACK);
tv5.setTextColor(Color.BLACK);
tv1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv3.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv4.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv5.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv1.setText(Integer.toString(id_row=id_row+1));
tv2.setText(e_entrbond_calcname.getText().toString());
tv3.setText(e_entrybonds_mdin1.getText().toString());
tv4.setText(e_entrybonds_dain1.getText().toString());
tv5.setText(e_entrybonds_details2.getText().toString());
tr0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr0.addView(tv1);
tr0.addView(tv2);
tr0.addView(tv3);
tr0.addView(tv4);
tr0.addView(tv5);
l1.addView(tr0);
try this code for Reading values from TableRow;
TableLayout l1 = (TableLayout) findViewById(R.id.table1);
for (int i = 0; i < l1.getChildCount(); i++) {
View child = l1.getChildAt(i);
if (child instanceof TableRow) {
TableRow row = (TableRow) child;
for (int x = 0; x < row.getChildCount(); x++) {
//View view = row.getChildAt(x);
TextView text = (TextView)row.getChildAt(x); // get child index on particular row
String title = text.getText().toString();
Log.i("Value", title);
}
}
}
Related
I am trying a add rows to my table by pressing a button, I can add the rows but I don't know how to add layout to the added row.
Here is my code:
public void testRow(View view) {
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.accountTable);
// create a new TableRow
TableRow row = new TableRow(this);
TextView x = new TextView(this);
x.setText("text ");
row.addView(x);
TextView y = new TextView(this);
y.setText("text ");
row.addView(y);
TextView z = new TextView(this);
z.setText("text ");
row.addView(z);
// add the TableRow to the TableLayout
table.addView(row);
}
You should add TableRow to TableLayout with LayoutParams, then you can also set the margins with it:
LayoutParams params = table.generateDefaultLayoutParams();
params.leftMargin = ...
params.rightMargin = ...
table.addView(row, params);
Hello fellow stackoverflowers,
i just wrote this code to populate a tablelayout inside my activity_customer layout. For some reason in runs through without throwing the slightest error, but at the end of "populateView" the application just stops/crashes.
I tried some many things I found via google/stackoverflow, but none seems to work. I hope someone can help me find why the app stops.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
populateView(DataClass.getReturnData());
}
private void populateView(String[] Array){
int len = Array.length;
TableLayout tab = (TableLayout) findViewById(R.id.table);
if (len != 0){
for (int i = 0; i <= len - 1; i++) {
TableRow row = new TableRow(this);
TextView tvName = new TextView(this);
tvName.setText("" + Array[i]);
System.out.println(Array[i]);
tab.addView(row);
}
}
}
Thanks in advance,
Soulrox
Here is a complete example:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
"Row 7"
};
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
"COLUMN5", "COLUMN6"
};
int rl=row.length;
int cl=column.length;
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount)
{
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++)
{
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j= 0; j < columnCount; j++)
{
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>"+id);
if (i ==0 && j==0)
{
textView.setText("0==0");
}
else if(i==0)
{
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j-1]);
}
else if( j==0)
{
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i-1]);
}
else
{
textView.setText(""+id);
// check id=23
if(id==23)
{
textView.setText("ID=23");
}
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
Output:
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
lll.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
lll.addView(group);
}
}
}
ll.addView(lll);
}
This is my code for dynamically adding radio button.
Problem is when I select one option then select anther it does not deselect the previous one. Why doesn't it deselect the the first one selecting another when they are both added to same radio group. Also it is not placing the radio buttons next to each other but on top of each other when the orientation is set to horizontal
UPDATE
I moved my declaration of
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
out side parent for loop now i am getting
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
How should i deal with it?
you are added only one RadioButton in RadioGroup that's why you got many selected radio button. RadioGroup need minimum 2 RadioButton for check or Uncheck effect.
try this,I hope it may work.
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
LinearLayout lll = new LinearLayout(Questionnaire.this);
lll.setOrientation(LinearLayout.VERTICAL);
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
lll.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
}
}
}
lll.addView(group);
ll.addView(lll);
}
try like this i have make code as you want.
ScrollView scrollView = (ScrollView) findViewById(R.id.templayout);
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(mLinearLayout);
JSONArray jsonQuestion = new JSONArray(obj.get("question").toString());
for (int iii = 0; iii < jsonQuestion.length(); iii++) {
JSONObject obj2 = (JSONObject) jsonQuestion.get(iii);
System.out.println(obj2.get("question"));
TextView tv = new TextView(Questionnaire.this);
tv.setText(obj2.get("question").toString());
mLinearLayout.addView(tv);
JSONArray jsonAnswer = new JSONArray(obj.get("answer").toString());
Log.d("Reading Answer: ", jsonAnswer + "");
RadioGroup group = new RadioGroup(Questionnaire.this);
group.setOrientation(RadioGroup.HORIZONTAL); // RadioGroup.HORIZONTAL or RadioGroup.VERTICAL
for (int iiii = 0; iiii < jsonAnswer.length(); iiii++) {
JSONObject obj3 = (JSONObject) jsonAnswer.get(iiii);
System.out.println(obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString()));
if (obj2.get("questiosysid").toString().matches(obj3.get("questionid").toString())) {
TextView tv1 = new TextView(Questionnaire.this);
tv1.setText(obj3.get("inputname").toString());
if (obj3.get("inputtype").toString().matches("radio")) {
RadioButton newRadioButton = new RadioButton(Questionnaire.this);
newRadioButton.setId(Integer.parseInt(obj3.get("answerid").toString()));
newRadioButton.setText(obj3.get("inputname").toString());
group.addView(newRadioButton);
}
}
}
mLinearLayout.addView(group);
}
}
I need to get the value of an EditText inside a row of a tablelayout. I create the table, the row and the edittext dynamically. I had help getting the childcount, but I still can't seem to get the text that is inputted by the user into the edittext. Here's the xml code creating the base layout.
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = "com.example.neil.hvacbuilder.MainActivity" >
<ScrollView
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:id = "#+id/scrollView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_below = "#+id/imgPartPicked" >
<TableLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:stretchColumns="*"
android:id = "#+id/tblLayoutContent"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentBottom = "true" >
</TableLayout >
</ScrollView >
</RelativeLayout >
Here's the code that generates the row and the edittexts inside the row.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.part_detail);
Bundle bundle = getIntent().getExtras();
String btnName = bundle.getString("btnNameStored");
String btnOrig = bundle.getString("btnOrig");
TextView textView = (TextView) findViewById(R.id.txtBtnPushed);
textView.setText(btnOrig);
BufferedReader reader;
InputStream is = null;
// Get the name of the part picked and then grab the dimensions that are needed for that
// part.
try {
is = getAssets().open(btnName);
reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
int lineLength = (line.length());
TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);
while (line != null){
TableRow tblRow = new TableRow(this);
tblRow.setPadding(5, 30, 5, 5);
table.addView(tblRow);
line = line.toUpperCase();
// sets the max number of columns to 2 and iterates through the number of lines.
// filling each cell with a Text Box with the name of each dimension of the part
// that was picked. And a EditView for the user to put in the dimensions.
for (int col = 0; col < NUM_COL; col++) {
//This is the label of what measurement needs to be enter.
TextView lblName = new TextView(this);
// This is the number you enter for the measurement.
EditText txtPartMeasurement = new EditText(this);
txtPartMeasurement.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL);
// Set all the input attributes for the text boxes.
txtPartMeasurement.setTextSize(14);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
txtPartMeasurement.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
}
txtPartMeasurement.setEnabled(true);
// Set all the input attributes for the labels of what needs to be entered.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
lblName.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
lblName.setBackgroundColor(getResources().getColor(R.color.colorPartMeasurements));
lblName.setFocusable(true);
lblName.setText(line);
lblName.setTextSize(14);
txtPartMeasurement.setTag(line);
// Add the labels and text boxes to the grid.
tblRow.addView(lblName);
tblRow.addView(txtPartMeasurement);
// Get the next line in the file if there is one.
line = reader.readLine();
}
};
}
catch (IOException e) {
e.printStackTrace();
}
}
Here's the code that gives me the values entered by thee user, but it not worky right.
#Override
public void onClick(View v) {
Button button = (Button) v;
String btnText = button.getText().toString();
switch (v.getId()) {
//This is the Save and Continue button. It saves the info entered and goes back to
// pick the next part needed.
case R.id.btnNextPartDetail:
TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
int childParts = PartDetailLayout.getChildCount();
if (PartDetailLayout != null) {
for (int i = 0; i < PartDetailLayout.getChildCount(); i++) {
View viewChild = PartDetailLayout.getChildAt(i);
if (viewChild instanceof EditText) {
// get text from edit text
String text = ((EditText) viewChild).getText().toString();
}
else if (viewChild instanceof TextView) {
// get text from text view
String text = ((TextView) viewChild).getText().toString();
//TODO: add rest of the logic
}
}
}
Toast.makeText(getApplicationContext(),
"Button Save/Continue Selected",
Toast.LENGTH_LONG).show();
break;
It gets me the count, but not the value.
I don't know what the id will be of each edittext so I add a tag as each is created. But the tag is also dynamic as it comes from a file.
So, my question is what am I doing wrong and how do I get the value of each edittext. Mind you, there can be 10 or 14 or more edittexts in the table.
Sorry for the long post, but I wanted to be as complete as possible.
Thanks.
Okay, Here's the working code. Again thanks
TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
int childParts = PartDetailLayout.getChildCount();
if (PartDetailLayout != null) {
for (int i = 0; i < childParts; i++) {
View viewChild = PartDetailLayout.getChildAt(i);
if (viewChild instanceof TableRow) {
int rowChildParts = ((TableRow) viewChild).getChildCount();
for (int j = 0; j < rowChildParts; j++) {
View viewChild2 = ((TableRow) viewChild).getChildAt(j);
if (viewChild2 instanceof EditText) {
// get text from edit text
String txtEdit = ((EditText) viewChild2).getText()
.toString();
} else if (viewChild2 instanceof TextView) {
// get text from text view
String txttext = ((TextView) viewChild2).getText().toString();
//TODO: add rest of the logic
}
}
}
}
}
The possible problem is that you check childs of TableLayout, but your EditText and TextView are in TableRow, so you need to check childs of childs.
I assume, that would be like that:
TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
iTableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
int childParts = PartDetailLayout.getChildCount();
if (PartDetailLayout != null) {
for (int i = 0; i < childParts; i++) {
View viewChild = PartDetailLayout.getChildAt(i);
if (viewChild instanceof TableRow) {
int rowChildParts = ((TableRow) viewChild).getChildCount();
for (int j = 0; j < rowChildParts; j++) {
View viewChild2 = ((TableRow) viewChild).getChildAt(j);
if (viewChild2 instanceof EditText) {
// get text from edit text
String text = ((EditText) viewChild2).getText().toString();
} else if (viewChild2 instanceof TextView) {
// get text from text view
String text = ((TextView) viewChild2).getText().toString();
//TODO: add rest of the logic
}
}
}
}
}
I have created dynamically edittext on my android application like below:
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.linearLayout1);
final EditText[] Et = new EditText[10];
int prevTextViewId = 0;
int add = 0;
int add1 = 0;
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
(int) LayoutParams.WRAP_CONTENT,
(int) LayoutParams.WRAP_CONTENT);
param.leftMargin = 25;
param.topMargin = (a + 1) * (100 + add1);
Et[a].setPadding(5, 23, 5, 5);
Et[a].setLayoutParams(param);
Et[a].setSingleLine(false);
Et[a].setHorizontalScrollBarEnabled(false);
Et[a].setWidth(280);
Et[a].getText().toString();
layout.addView(Et[a]);
add = add + 25;
add1 = add1 + 15;
}
}
And I want to get text from dynamically created edittext into a button like below:
btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
btn_doneAnswer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
String text += Et[a].getText().toString();
}
Bundle bundle = new Bundle();
Intent i_backToAddItem = new Intent(
AnswerActivityMultiText.this,
QuestionActivityDynamic.class);
bundle.putString("text", text);
bundle.putString("MultiQues", MultiQues);
i_backToAddItem.putExtras(bundle);
startActivity(i_backToAddItem);
}
});
But I am getting error below in the button onclick method at this line Et[a] = new EditText(this);:
The constructor EditText(new View.OnClickListener(){}) is undefined
another below error at this line += in the button onclick method:
Syntax error on token "+=", = expected
Kindly suggest me how I am getting text from dynamically created edittext into a button.
Waiting for reply.
Thanks
you dont have to initialize the EditText again in the onClick. You should directly use the EditText object to get the text. So the line
Et[a] = new EditText(this);
is not required at all!
And for the error with '+=' operator, you are declaring the string and using the operator(+=) in one line. you have to split up the the declaration part out of the for loop and then use '+='. Here's the complete code :
btn_doneAnswer = (Button) findViewById(R.id.btn_doneAnswer);
btn_doneAnswer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text;
for (int a = 0; a < Arr.length; a++) {
text += Et[a].getText().toString();
}
Bundle bundle = new Bundle();
Intent i_backToAddItem = new Intent(
AnswerActivityMultiText.this,
QuestionActivityDynamic.class);
bundle.putString("text", text);
bundle.putString("MultiQues", MultiQues);
i_backToAddItem.putExtras(bundle);
startActivity(i_backToAddItem);
}
});
Your problem is in this lines:
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(this);
String text += Et[a].getText().toString();
}
In this line:
Et[a] = new EditText(this);
this is the onClickListener object, you must have class.this (where class is the class name of file that this code is created).
In this other line:
String text += Et[a].getText().toString();
You always create a new String, and instead of created, you make an add. It is a Java concept fail.
You must do this:
String text = "";
for (int a = 0; a < Arr.length; a++) {
Et[a] = new EditText(class_name.this);
text += Et[a].getText().toString();
}