OnClickListener not working android - android

I have programmatically create the table in android.
In that table, each row has 2 text views, 1 edit text and 1 button.
I have to set the OnClickListner to the button to remove that particular row.
But OnClickListner is not working. Already I have implemented this process without edit text.It gives below warning message. how to solve this ? W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#133707c3 attribute=null, token = android.os.BinderProxy#30f60fac*****
public void addRows(List MenuItem rowdata, boolean toggleColor) {
Log.i("addrows", "CP1" + rowdata);
tr = new TableRow(this);
TableLayout.LayoutParams lp =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView Name = new TextView(this);
Name.setText(rowdata.getDishname());
Name.setTextSize(18);
Name.setTextColor(Color.parseColor("#222C31"));
Name.setTypeface(custom);
Name.setPadding(0,10,0,10);
if(toggleColor) {
Name.setBackground(getDrawable(R.drawable.border2));
}else {
Name.setBackground(getDrawable(R.drawable.border));
}
TableRow.LayoutParams nameparams =
new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 3.2f);
Name.setLayoutParams(dishnameparams);
Name.setGravity(Gravity.CENTER);
tr.addView(Name); // Adding textView to tablerow.
TextView JobInfo = new TextView(this);
JobInfo.setText(rowdata.getDishname());
JobInfo.setTextSize(18);
JobInfo.setTextColor(Color.parseColor("#222C31"));
JobInfo.setTypeface(custom);
JobInfo.setPadding(0,10,0,10);
if(toggleColor) {
JobInfo.setBackground(getDrawable(R.drawable.border2));
}else {
JobInfo.setBackground(getDrawable(R.drawable.border));
}
TableRow.LayoutParams nameparams =
new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 3.2f);
JobInfo.setLayoutParams(dishnameparams);
JobInfo.setGravity(Gravity.CENTER);
tr.addView(JobInfo); // Adding textView to tablerow.
EditText SplComment = new EditText(this);
SplComment.setText(rowdata.getSplComment());
SplComment.setTextSize(18);
SplComment.setTextColor(Color.parseColor("#222C31"));
SplComment.setTypeface(custom);
SplComment.setPadding(0, 10, 0, 10);
if(toggleColor) {
SplComment.setBackground(getDrawable(R.drawable.border2));
}else {
SplComment.setBackground(getDrawable(R.drawable.border));
}
TableRow.LayoutParams SplCommentparams =
new TableRow.LayoutParams(3, TableRow.LayoutParams.MATCH_PARENT, 2.65f);
// SplCommentparams.setMargins(0, 0, 10, 0);
SplComment.setLayoutParams(SplCommentparams);
SplComment.setGravity(Gravity.CENTER);
tr.addView(SplComment); // Adding Edit text to tablerow.
LinearLayout LL3Setup = new LinearLayout(this);
LL3Setup.setOrientation(LinearLayout.VERTICAL);
LL3Setup.setLayoutParams(new TableRow.LayoutParams(5, TableRow.LayoutParams.MATCH_PARENT, 0.63f));
//LL1Setup.setPadding(20, 20, 20, 20);
LL3Setup.setGravity(Gravity.CENTER);
if(toggleColor) {
LL3Setup.setBackground(getDrawable(R.drawable.border2));
}else {
LL3Setup.setBackground(getDrawable(R.drawable.border));
}
Button DeleteBtn = new Button(this);
DeleteBtn.setText("D");
DeleteBtn.setTextColor(Color.WHITE);
DeleteBtn.setLayoutParams(new TableRow.LayoutParams(50, 40));
DeleteBtn.setTypeface(custom);
DeleteBtn.setAllCaps(false);
DeleteBtn.setTextSize(12);
DeleteBtn.setBackground(getDrawable(R.drawable.btn_vmd));
DeleteBtn.setOnClickListener(DeleteListener);
DeleteBtn.setClickable(true);
DeleteBtn.setGravity(Gravity.CENTER);
LL3Setup.addView(DeleteBtn);
tr.addView(LL3Setup); // Adding button to tablerow.
// Add the TableRow to the TableLayout
MainTableFinish.addView(tr, new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
}
Button Listner code:
DeleteListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Delete Row", "Yes");
UpdateTable();
}
};
Declaration:
View.OnClickListener DeleteListener;

I Just found the possible solution from a old stackoverflow thread.
Please Try
Delete row dynamically from table layout in android

Related

Having difficulty in getting dynamically added table rows to show up

I am working on a screen that basically shows questions and answers in a grid format and I am putting them all into table rows dynamically. The problem I am having is that once I put in the first row (the header), the rest of the rows in the table do not show up. I first add the headers. This shows up.
protected void setupTableHeaders() {
TableRow tableRow = getNewTableRow();
tableRow.setTag(header);
TextView textView = new TextView(this);
GlobalVars.setupText(this,textView,32320); //Event
textView.setTextColor(R.color.black);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.setMarginStart(50);
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Iterator it = mapQuestions.entrySet().iterator();
//first get all the answers
if (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
HolderQuestion holderQuestion = mapQuestions.get(pair.getKey());
//assume all answers sets for all questions on this screen are the same.
int i = 0;
for (HolderAnswer answer : holderQuestion.getListAnswers()) {
TextView textViewHeader = new TextView(this);
textViewHeader.setTextColor(R.color.black);
GlobalVars.setupText(this, textViewHeader, answer.getTextID());
tableRow.addView(textViewHeader, (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .5f)));
if (i < holderQuestion.getListAnswers().size() - 1) {
tableRow.addView(getNewDivider(), (new LayoutParams(0, LayoutParams.WRAP_CONTENT, .1f)));
}
i++;
}
}
table1.addView(tableRow);
}
Then I add the questions and answers, which do not show up:
protected void setupRadioButtonAnswers() {
for (int i=0;i<holderQuestionMultis.size();i++) { //assume this is populated correctly
TableRow tableRow = getNewTableRow();
tableRow.setTag(answerRow);
TextView textView = new TextView(this);
textView.setTextColor(R.color.black);
GlobalVars.setupText(this,textView,holderQuestionMultis.get(i).getHolderQuestion().getTextID());
textView.setTag(textCell);
textView.setVisibility(View.VISIBLE);
LayoutParams params = new LayoutParams(0,LayoutParams.WRAP_CONTENT,3.0f);
params.leftMargin = 50;
tableRow.addView(textView,params);
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
for (int j=0;j<holderQuestionMultis.get(i).getRadioButtons().size();j++) {
RadioButton radioButton = holderQuestionMultis.get(i).getRadioButtons().get(j);
radioButton.setVisibility(View.VISIBLE);
tableRow.addView(radioButton,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.5f)));
if (j< holderQuestionMultis.size()-1) {
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
}
}
tableRow.setVisibility(View.VISIBLE);
table1.addView(tableRow);
}
}
And the two methods which get the radioButtons and table rows:
// table elements
protected TableRow getNewTableRow() {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tlparams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setBackground(getResources().getDrawable(R.drawable.border_table_multi_grid));
tableRow.setLayoutParams(tlparams);
return tableRow;
}
protected RadioButton getRadioButton() {
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(getResources().getDrawable(R.drawable.radio_button_multigrid));
//radioButton.setButtonDrawable(R.color.transparent);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
radioButton.setLayoutParams(params);
return radioButton;
}
The tablelayout xml:
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_above="#+id/viewSpacer"
android:layout_below="#+id/lblSubQuestionText"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/border_table_multi_grid">
What am I missing? Is the first table header row covering up the other rows? Why do they not appear?
It turns out that this code:
tableRow.addView(getNewDivider(),(new LayoutParams(0,LayoutParams.WRAP_CONTENT,.1f)));
Was the culprit which consisted of adding a LinearLayout containing a view. Just adding the view alone solved the problem.

How to save data from dynamically created edittext at runtime to sqlite database?

I am building a program that creates 6 edit Text at run-time and I want to save the data input by the user in those edittexts into sqlite Database but I'm getting an error. and i provided the code that is creating my edit texts i hope i can get some help please. the error is in the second case.
the edittext is may have not been initialized.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.buttonAddIntervention:
final TableLayout table = (TableLayout)findViewById(R.id.tableinterventions);
EditText ed = new EditText(this);
TextView tv = new TextView(this);
final TableRow tabr = new TableRow(this);
tabr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setText("Code");
ed.setHint("Code");
ed.setText("", null);
ed.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr.addView(tv);
tabr.addView(ed);
table.addView(tabr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed1 = new EditText(this);
TextView tv1 = new TextView(this);
Button b1 = new Button(this);
final TableRow tabr1 = new TableRow(this);
tabr1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setText("-");
tv1.setText("Start Time");
ed1.setHint("Start Time");
ed1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr1.addView(tv1);
tabr1.addView(ed1);
tabr1.addView(b1);
table.addView(tabr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed2 = new EditText(this);
TextView tv2 = new TextView(this);
final TableRow tabr2 = new TableRow(this);
tabr2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv2.setText("End Time");
ed2.setHint("End Time");
ed2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr2.addView(tv2);
tabr2.addView(ed2);
table.addView(tabr2, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) );
EditText ed3 = new EditText(this);
TextView tv3 = new TextView(this);
final TableRow tabr3 = new TableRow(this);
tabr3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv3.setText("Description");
ed3.setHint("Description");
ed3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr3.addView(tv3);
tabr3.addView(ed3);
table.addView(tabr3,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed4 = new EditText(this);
TextView tv4 = new TextView(this);
final TableRow tabr4 = new TableRow(this);
tabr4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv4.setText("Product");
ed4.setHint("Product");
ed4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv4.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr4.addView(tv4);
tabr4.addView(ed4);
table.addView(tabr4,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
EditText ed5 = new EditText(this);
TextView tv5 = new TextView(this);
final TableRow tabr5 = new TableRow(this);
tabr5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv5.setText("Serial Number");
ed5.setHint("Serial Number");
ed5.setLayoutParams(new TableRow.LayoutParams(190,LayoutParams.WRAP_CONTENT));
tv5.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tabr5.addView(tv5);
tabr5.addView(ed5);
table.addView(tabr5,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v1) {
// TODO Auto-generated method stub
table.removeView(tabr);
table.removeView(tabr1);
table.removeView(tabr2);
table.removeView(tabr3);
table.removeView(tabr4);
table.removeView(tabr5);
}
});
break;
case R.id.BtnTestSaveAll:
InterventionsDatabase idb1 = new InterventionsDatabase();
//the error is here the ed,ed1,ed2,ed3,ed4 and ed5 may not have been initialized
String cod= String.valueOf(ed.getText());
String s_time = String.valueOf(ed1.getText().toString());
String e_time = String.valueOf(ed2.getText().toString());
String d = String.valueOf(ed3.getText().toString());
String p = String.valueOf(ed4.getText().toString());
String Sn = String.valueOf(ed5.getText().toString());
idb1.setEmp_code(Integer.parseInt(cod));
idb1.setEmp_startTime(Integer.parseInt(s_time));
idb1.setEmp_endTime(Integer.parseInt(e_time));
idb1.setDescription(d);
idb1.setProduct(p);
idb1.setSerialNumber(Integer.parseInt(Sn));
Dbhelper.createInterventionsDB(Integer.parseInt(cod),Integer.parseInt(s_time),Integer.parseInt(e_time), d, p,Integer.parseInt(Sn));
Toast.makeText(getApplicationContext(), "Yeah", Toast.LENGTH_SHORT).show();
break;
The "may have not been initialized" error appears when you want to access object which... may have not been initialized. It means that for example this code won't work:
int a = 5;
EditText et;
if(a == 5) {
et = new EditText();
}
et.setText("some text");
It won't work, because et is initialized inside if statement, which condition in this case of course is passed, but the compiler doesn't know that. It's just more safe to show you that you made a mistake here than eventually having your application crashed on run-time.
Edit:
You're initializing EditText in Button's onClick() (therefore when the Button is clicked) and you want to access it somewhere else (when the other Button is clicked). Compiler prevents this, because it must be sure that EditText is initialized, it can't rely on user to click the initializing button first.
In order to solve this problem, create those EditText's to be fields of your Activity class and initialize them in onCreate(), there's no need to do this when the Button is clicked. And that's it.
Construct like this:
switch (var) {
case 1:
SomeType foo = new SomeType();
// ...
break;
case 2:
foo.something();
// ...
break;
}
has the problem that foo is not initialized when case 2 is executing. Variables are visible in their declaring curly {} scope, i.e. the switch in this case. However, the code execution path, when case 2 is taken, doesn't include the initialization of the variable. The compiler is smart enough to notice this and gives you a warning.
In your case a good solution is to move the EditText variable declarations to class member level:
private EditText mEd1;
// ...
mEd1 = new EditText(this);
and when referencing them, check for non-null
if (mEd1 != null) {
// ...
String s_time = String.valueOf(mEd1.getText().toString());
You have to get view of all edittext first then do whatever you want something like this:
ViewGroup group = (ViewGroup)findViewById(R.id.your_layout);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}

Unable to pass correct table row's data to next activity

I'm fairly new to Android programming and have completed my first app except for one issue on passing data from a table row that is selected (onClick). I've researched out here and tried most of the recommendations but I'm obviously missing something critical. I am a little confused on the getTag/Id and setTag/Id so maybe that's where I'm going wrong.
A little background, the table can have 1 to N number of rows populated dynamically from database, each row being clickable. Once a row is clicked, I need to pass the unique data of that row to the next activity (using Bundle for that which works fine). What doesn't work is that no matter which row I click, I only get the last row's data passed to the next activity and not the data of the clicked row.
Here's some of my pertinent code for the data add to row/table and onclick... What am I doing wrong to only get the last row's data? Really appreciate anyone's help in pointing me in the right direction.
public void addData(ArrayList<Auctions> auctions) {
prevCity = "";
currCity = "";
prevDate = "";
currDate = "";
prevTime = "";
currTime = "";
prevUnit = "";
currUnit = "";
for (Iterator i = auctions.iterator(); i.hasNext();) {
Auctions p = (Auctions) i.next();
if (p.getCity().equals(prevCity) && p.getDate().equals(prevDate) && p.getTime().equals(prevTime) && p.getSunit().equals(prevUnit)) {
//skip to next unique record
} else {
/** Create a City TableRow dynamically **/
row2 = new TableRow(this);
row2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
tvcity = new TextView(this);
currCity = p.getCity();
if (currCity.toString().equals(prevCity)) {
tvcity.setText("");
} else {
//create a blank row to separate from previous city entries
row1 = new TableRow(this);
row1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tvdummy = new TextView(this);
tvdummy.setText("");
tvdummy.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row1.addView(tvdummy);
auctions_table.addView(row1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// work on the new city data
tvcity.setText(p.getCity());
}
tvcity.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tvcity.setTextColor(getResources().getColor(R.color.red));
//add TextView data to row
row2.addView(tvcity);
// Add the TableRow to the TableLayout
auctions_table.addView(row2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// Create date, time, and unit name row dynamically
row3 = new TableRow(this);
row3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// Create TextViews to add to the Row -- Date and Time
tvdate = new TextView(this);
tvtime = new TextView(this);
tvsunit = new TextView(this);
currDate = p.getDate();
currTime = p.getTime();
currUnit = p.getSunit();
if ((currDate.toString().equals(prevDate)) && (currTime.toString().equals(prevTime))) {
tvdate.setText("");
tvtime.setText("");
} else {
tvdate.setText(p.getDate());
tvtime.setText(p.getTime());
}
if (currUnit.toString().equals(prevUnit)) {
tvsunit.setText("");
} else {
tvsunit.setText("Dynamic UNIT");
tvsunit.setText(p.getSunit());
}
tvdate.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvdate.setTextColor(getResources().getColor(R.color.blue));
tvtime.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvtime.setTextColor(getResources().getColor(R.color.blue));
tvsunit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvsunit.setTextColor(getResources().getColor(R.color.blue));
//add TextView data to row
row3.addView(tvdate);
row3.addView(tvtime);
row3.addView(tvsunit);
row3.setId(t);
row3.setFocusable(false);
//row3.setClickable(true);
row3.setOnClickListener(new View.OnClickListener() {
//public OnClickListener tablerowOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// Get the selected row's data
//row3.getId();
Bundle basket2 = new Bundle();
basket2.putString("ustate", state.getText().toString());
basket2.putString("abbrev", abbrev);
basket2.putString("ucity", currCity);
basket2.putString("uname", currUnit);
basket2.putString("auctiondate", currDate);
basket2.putString("auctiontime", currTime);
Intent StAuctions = new Intent(getApplicationContext(), StorageAuctionDetails.class);
StAuctions.putExtras(basket2);
StAuctions.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(StAuctions);
// Closing screen
//finish();
}
});
row3.setBackgroundResource(drawable.list_selector_background);
// Add the TableRow to the TableLayout
auctions_table.addView(row3, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
prevCity = currCity;
prevDate = currDate;
prevTime = currTime;
prevUnit = currUnit;
t++;
}
};
You are going to get these variables based on what they are when onClick is activated:
basket2.putString("ustate", state.getText().toString());
basket2.putString("abbrev", abbrev);
basket2.putString("ucity", currCity);
basket2.putString("uname", currUnit);
basket2.putString("auctiondate", currDate);
basket2.putString("auctiontime", currTime);
Those valued will be for whatever the last time you ran through your iteration.
You need to create a new "basket" for each row entry. Not in the click listener, but in the iteration loop, and make that basket the tag for that row, with something like this:
row3.setTag(basket2);
Can't decrypt your code sufficiently enough. But what I would suggest is that in your onClick(), put in Log.v("Row id is: ", Integer.toString(v.getId())) and see in LogCat if every time you click, it returns the same value.
Also, if I'm writing similar features, I might want to perform the extraction of row information in the onClick(), so information gets extracted from row and put into bundle on the fly, rather than passing the values into bundle during the iterator. Something like:
public void onClick(View v) {
String a; //extras for bundle
Bundle basket2 = new Bundle();
//iterate row's children and assign values to string here
for(int i = 0; i < v.getChildSize(); i++){
TextView t = (TextView)v.getChildAt(i);
//assignment
basket2.putString("someKey", a);
}
//send your intent
}

How to add multiple columns in a row of an android table layout

I have a custom list with name,age and grade .I want to loop through the list and show the contents in a table layout.Each row will have following attributes.Row will have some kind of styling
Name,
Age,
Age_Value,
Grade,
Grade_Value
Here Age and Grades are just a label.Given below is a sample table
Dolly
Age
23
Grade
A+
Roman
Age
22
Grade
C
Ben
Age
23
Grade
B+
I want to add these details to the Table layout in my layout dynamically.
Here is the code im trying
for(int j=0;j<_attributeList.size();j++)
{
TableRow tr = new TableRow(MyActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tvName = new TextView(MyActivity.this);
tvName.setPadding(3,0,0,0);
tvName.setText(_attributeList.get(j).getName() +" : ");
tr.addView(tvName);
TextView tvAgeL = new TextView(MyActivity.this);
tvAgeL .setText("Age");
tr.addView(tvAgeL );
TextView tvAgeValue = new TextView(MyActivity.this);
tvAgeValue .setPadding(3,0,0,0);
tvAgeValue .setText(_attributeList.get(j).getValue() +" : ");
tr.addView(tvAgeValue );
attributeTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Since these items are adding to the same row im getting results in a single line. How can i show them in seperate rows
You should try like this
Code
for(int i = 0; i < _attributeList.size();i++){
TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
newCheckBox = new CheckBox(getApplicationContext());
TextView feeTypeText = new TextView(getApplicationContext());
feeTypeText.setText(jsonObj.getString("feeType"));
feeTypeText.setTextColor(Color.BLACK);
feeTypeText.setTextSize(16f);
feeTypeText.setTypeface(tf);
TextView dueAmountText = new TextView(getApplicationContext());
dueAmountText.setText(jsonObj.getString("dueAmount"));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(16f);
dueAmountText.setTypeface(tf);
TextView dueDateText = new TextView(getApplicationContext());
dueDateText.setText(jsonObj.getString("dueDate"));
dueDateText.setTextColor(Color.BLACK);
dueDateText.setTextSize(16f);
dueDateText.setTypeface(tf);
newRow.addView(newCheckBox,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(dueAmountText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
newRow.addView(dueDateText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
attributeTable.addView(newRow);
}
you can add like this way...here i added TableRow Programatically in which i added textView.
addtree.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
tr1 = new TableRow(YourActivity.this);
tr1.setClickable(true);
tr1.setId(jj);
tr1.setPadding(0, 5, 0, 5);
Log.d("id", "id is......"+tr1.getId());
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView textview = new TextView(New_Quote_Activity.this);
textview.setText("Job "+jj);
textview.setTextColor(Color.BLACK);
textview.setTypeface(null, Typeface.BOLD);
textview.setTextSize(14);
textview.setPadding(5, 0, 0, 0);
View vv=new View(New_Quote_Activity.this);
vv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv.setBackgroundResource(R.color.viewcolor);
View vv1=new View(New_Quote_Activity.this);
vv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv1.setBackgroundResource(R.color.viewcolor);
tr1.addView(textview);
table_crete_invoice.addView(vv1);
table_crete_invoice.addView(tr1,layoutParams);
table_crete_invoice.addView(vv);
}

Extending TableLayout class - adding rows

I am trying to extend the TableLayout class, such that rows will be populated automatically by the class. The issue I am having is the rows that I add inside the custom class do not display.
Ex. within the Activity:
private TextView getTableCell(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatasetTableLayout table = (DatasetTableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
tr.addView(getTableCell("activity1"));
tr.addView(getTableCell("activity2"));
tr.addView(getTableCell("activity3"));
table.addView(tr);
This successfully adds a row to the table. However, within my custom class:
private TextView getTableCell(String text) {
TextView tv = new TextView(context);
tv.setText(text);
tv.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return tv;
}
private void update() {
TableRow tr = new TableRow(context);
tr.addView(getTableCell("class1"));
tr.addView(getTableCell("class2"));
tr.addView(getTableCell("class3"));
addView(tr);
}
does not successfully add a row. Well, it does add a row, as getChildCount(), and getChildAt() do return this row - but it does not get displayed.
Am I missing something?
#theresia (Can't seem to format text if I add a comment):
It does appear that it adds the TableRow:
for(int i = 0; i < getChildCount(); i++) {
TableRow row = (TableRow) getChildAt(i);
for(int j = 0; j < row.getChildCount(); j++) {
TextView tv = (TextView) row.getChildAt(j);
Log.e("DatasetTableLayout-data", tv.getText().toString() + " ");
}
}
gives me:
01-24 11:03:31.668: ERROR/DatasetTableLayout-data(1624): activity1
01-24 11:03:31.677: ERROR/DatasetTableLayout-data(1624): activity2
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): activity3
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class1
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class2
01-24 11:03:31.727: ERROR/DatasetTableLayout-data(1624): class3
My guess would be something along this line: addView(tr); Does it successfully add your TableRow to TableLayout?
Edit:
TableLayout tb = new TableLayout(this);
TableRow tr = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("row");
tr.addView(tv);
tb.addView(tr); // this is what I mean
setContentView(tb);
Your code for creating rows works fine, but if you haven't add it to the table, and later on, add the table to the parent view as well, your rows wouldn't get displayed.

Categories

Resources