Populate Tablelayout programatically through String Array - java

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:

Related

Android (Java): Show Progress-bar while Loading table view data [duplicate]

This question already has answers here:
The application may be doing too much work on its main thread
(21 answers)
Closed 1 year ago.
I have a block of code to load a table view. But for large data, my app is crashing. I want to fix that and also want to show a progress bar while loading the table view data. I tried with the AsyncTask but it is showing Skipped 98 frames! The application may be doing too much work on its main thread.
My onCreate code (Updated): -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheet);
myDb = new DBHelper(this);
loadIntentInputs();
setToolbar();
new MyWorker(this).execute();
}
My AsyncTask code (Updated): -
public class MyWorker extends AsyncTask < String , Context , Void > {
private Context context ;
private ProgressDialog progressDialog;
public MyWorker (Context context) {
this.context = context ;
progressDialog = new ProgressDialog ( context ) ;
progressDialog.setCancelable ( false ) ;
progressDialog.setMessage ( "Retrieving data..." ) ;
progressDialog.setTitle ( "Please wait" ) ;
progressDialog.setIndeterminate ( true ) ;
}
# Override
protected void onPreExecute ( ) {
progressDialog.show () ;
}
# Override
protected Void doInBackground ( String ... params ) {
runOnUiThread(new Runnable() {
#Override
public void run() {
showAttendanceTable();
}
});
return null ;
}
# Override
protected void onPostExecute ( Void result ) {
if(progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss ( ) ;
}
}
}
showAttendanceTable method code (Updated): -
private void showAttendanceTable() {
TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}
// for excel file
HSSFSheet hssfSheet = null;
try {
hssfSheet = hssfWorkbook.createSheet(month_year);
} catch (IllegalArgumentException e){
e.printStackTrace();
}
HSSFRow hssfRow;
// setting 1st row
rolls_tv[0].setText("Roll");
names_tv[0].setText("Name");
// setting excel file
hssfRow = hssfSheet.createRow(0);
hssfRow.createCell(0).setCellValue("Roll");
hssfRow.createCell(1).setCellValue("Name");
rolls_tv[0].setTextSize(1,22);
names_tv[0].setTextSize(1,22);
rolls_tv[0].setTypeface(rolls_tv[0].getTypeface(), Typeface.BOLD);
names_tv[0].setTypeface(names_tv[0].getTypeface(), Typeface.BOLD);
rolls_tv[0].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
names_tv[0].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
rolls_tv[0].setTextColor(Color.parseColor("#FF000000"));
names_tv[0].setTextColor(Color.parseColor("#FF000000"));
for (int i = 1; i <=day_in_month ; i++) {
String date = String.valueOf(i);
if(i<10) date = "0"+date;
status_tv[0][i].setText(date);
// setting excel file
hssfRow.createCell(i+1).setCellValue(date);
status_tv[0][i].setTextSize(1,22);
status_tv[0][i].setTypeface(status_tv[0][i].getTypeface(), Typeface.BOLD);
status_tv[0][i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
status_tv[0][i].setTextColor(Color.parseColor("#FF000000"));
}
// setting rows after 1st row
for (int i = 1; i < row_size ; i++) {
rolls_tv[i].setText(roll_array[i-1]);
names_tv[i].setText(name_array[i-1]);
// setting excel file
hssfRow = hssfSheet.createRow(i);
hssfRow.createCell(0).setCellValue(roll_array[i-1]);
hssfRow.createCell(1).setCellValue(name_array[i-1]);
rolls_tv[i].setTextSize(1,20);
names_tv[i].setTextSize(1,20);
rolls_tv[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
names_tv[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
rolls_tv[i].setTextColor(Color.parseColor("#FF000000"));
names_tv[i].setTextColor(Color.parseColor("#FF000000"));
int count=0;
for (int j = 1; j <=day_in_month ; j++) {
String day = String.valueOf(j);
String month = month_year.substring(0,3);
String year = month_year.substring(4,8);
String status = myDb.getStatus(sid_array[i-1],day+" "+month+" "+year);
if (status!=null && status.equals("A")) {
status_tv[i][j].setText(status);
status_tv[i][j].setBackgroundColor(Color.parseColor("#EF9A9A"));
// setting excel file
hssfRow.createCell(j+1).setCellValue(status);
} else if (status!=null && status.equals("P")) {
status_tv[i][j].setText(String.valueOf(++count));
status_tv[i][j].setBackgroundColor(Color.parseColor("#A5D6A7"));
// setting excel file
hssfRow.createCell(j+1).setCellValue(String.valueOf(count));
}
status_tv[i][j].setTextSize(1,20);
status_tv[i][j].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
status_tv[i][j].setTextColor(Color.parseColor("#FF000000"));
}
}
for (int i = 0; i < row_size; i++) {
rows[i] = new TableRow(this);
rolls_tv[i].setPadding(20,12,20,12);
names_tv[i].setPadding(20,12,20,12);
if (i==0) {
rows[i].setBackgroundColor(Color.parseColor("#FFD400"));
} else if(i%2==0) {
rows[i].setBackgroundColor(Color.parseColor("#EEEEEE"));
} else {
rows[i].setBackgroundColor(Color.parseColor("#E4E4E4"));
}
rows[i].addView(rolls_tv[i]);
rows[i].addView(names_tv[i]);
for (int j = 1; j <=day_in_month ; j++) {
status_tv[i][j].setPadding(20,12,20,12);
rows[i].addView(status_tv[i][j]);
}
tableLayout.addView(rows[i]);
}
tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_MIDDLE);
}
The problem is you are running new thread on your doInBackground method and you have placed your showAttendanceTable() code inside runOnUiThread() which is causing too many work to consume main thread.Just copy paste the below code, replace it with your doInBackground() method and everything will work fine.
protected Void doInBackground ( String ... params ) {
showAttendanceTable();
}
return null ;
}
Copy this much part of your code from showAttendanceTable() to onPreExecute() method:
TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}

how to add dynamic grid layout in android by only programmatically?

I want to add layout dynamically in my android app using java code only, not using XML code. Kindly help me with this. Advance thank you.
with using this code I had done this :
public void makeSeat()
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(3,3,3,3);
GridLayout gl = (GridLayout) findViewById(R.id.grid_main);
int column = 4;
int row = 5;
int i_total = 4*5;
int i_busSeatNo = 1;
gl.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gl.setColumnCount(column);
gl.setRowCount(row + 1);
CheckBox btn;
for(int i =0, c = 0, r = 1; i < i_total; i++, c++)
{
if(c == column)
{
c = 0;
r++;
i_busSeatNo++;
}
if (r == c+1 ){
i_busSeatNo = r;
}
btn = new CheckBox(this);
btn.setGravity(Gravity.CENTER);
btn.setBackgroundResource(R.drawable.chbox);
btn.setButtonDrawable(R.drawable.chbox);
btn.setHeight(val_seat);
btn.setWidth(val_seat);
btn.setText(""+i_busSeatNo); //(r+" "+c); //r+" "+c
int cnt = Integer.parseInt(r+""+c);
btn.setId(cnt);
final CheckBox finalBtn = btn;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(DynamicBus.this,
"id: " + finalBtn.getId() + "\nNo: " + finalBtn.getText(),
Toast.LENGTH_SHORT).show();
}
});
if(c != column-1) {
i_busSeatNo = i_busSeatNo+5;
}
gl.addView(btn, i);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = val_seat;
param.width = val_seat;
param.rightMargin = 7;
param.topMargin = 7;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
if (r==1)
param.rowSpec = GridLayout.spec(r,1);
btn.setLayoutParams (param);
}
}
Output :
what I get :
https://i.stack.imgur.com/FqynC.png
what I want :
https://i.stack.imgur.com/QZCTI.png

android Table layout wiht table read rows

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);
}
}
}

How do I get the value of the contents inside a row of a tablelayout

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
}
}
}
}
}

getting text from dynamically created edittext into a button android java

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();
}

Categories